Skip to content

Commit

Permalink
new api structure applied
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Mar 25, 2019
1 parent 7d15adb commit 041b6d7
Show file tree
Hide file tree
Showing 24 changed files with 194 additions and 65 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"guzzlehttp/guzzle": "^6.3",
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"monolog/monolog": "^1.23"
"monolog/monolog": "^1.23",
"robinvdvleuten/ulid": "^3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.3",
Expand Down
76 changes: 61 additions & 15 deletions src/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
use TK\API\Exception\RequestException;
use TypeError;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use Ulid\Ulid;

final class Client
{

public const HTTP_POST = 'POST';
public const HTTP_GET = 'GET';
/**
* @var array
*/
Expand All @@ -40,18 +42,21 @@ final class Client


/**
* @var string
* @var Environment
*/
private $apiUrl;
private $environment;
/**
* @var GuzzleClient
*/
private $guzzleClient;

private $logger;
private $languageCode;
private $airlineCode;


private $headers = [
'User-Agent' => 'mkorkmaz/tk-api-php-client 1.0'
'User-Agent' => 'mkorkmaz/tk-api-php-client 2.0'
];

/**
Expand All @@ -62,13 +67,28 @@ final class Client
*/
public function __construct(Environment $environment, GuzzleClient $guzzleClient, LoggerInterface $logger)
{
$this->apiUrl = $environment->getApiUrl();
$this->environment = $environment;
$this->headers['apiKey'] = $environment->getApiKey();
$this->headers['apiSecret'] = $environment->getApiSecret();
$this->guzzleClient = $guzzleClient;
$this->logger = $logger;
$this->airlineCode = 'TK';
$this->languageCode = 'TR';
}

public function withAirlineCode(string $airlineCode) : self
{
$new = clone $this;
$new->airlineCode = $airlineCode;
return $new;
}

public function withLanguageCode(string $languageCode) : self
{
$new = clone $this;
$new->languageCode = $languageCode;
return $new;
}
/**
* @param $name
* @param $arguments
Expand Down Expand Up @@ -147,34 +167,60 @@ private function request(EndpointInterface $endpoint) : array
private function httpRequest(EndpointInterface $endpoint) : ResponseInterface
{
$this->headers = array_merge($this->headers, $endpoint->getHeaders());
$uri = $this->apiUrl . $endpoint->getEndpoint();
$queryParams = $this->getQueryParams($endpoint);

$uri = $this->environment->getApiUrl() . $endpoint->getEndpoint();
$options = [];
$httpRequestMethod = strtolower($endpoint->getHttpRequestMethod());
if ($httpRequestMethod === 'post') {
$httpRequestMethod = $endpoint->getHttpRequestMethod();
if ($httpRequestMethod === self::HTTP_POST) {
$this->headers['Content-Type'] = 'application/json';
$options['json'] = $endpoint->getQueryParams();
$options['json'] = $queryParams;
}
if ($httpRequestMethod === 'get') {
$uri .= '?' . http_build_query($endpoint->getQueryParams());
if ($httpRequestMethod === self::HTTP_GET) {
$uri .= '?' . http_build_query($queryParams);
}
$uri .= '?apikey='. $this->environment->getApiKey();
$options['headers'] = $this->headers;

$this->logger->debug(
'API call for :' . $endpoint->getEndpoint(),
[
'httpRequestMethod' => $httpRequestMethod,
'uri' => $uri,
'headers' => $this->headers,
'queryParams' => $endpoint->getQueryParams()
'queryParams' => $queryParams
]
);
try {
return $this->guzzleClient->{$httpRequestMethod}($uri, $options);
return $this->guzzleClient->{strtolower($httpRequestMethod)}($uri, $options);
} catch (GuzzleRequestException $e) {
$exceptionMessage = (string) $e->getResponse()->getBody()->getContents();
$exceptionMessage = (string) $e->getResponse()
->getBody()
->getContents();
$message = sprintf('TK API REQUEST ERROR: %s', $exceptionMessage);
$this->logger->error($message);
throw new RequestException($message);
}
}

private function getQueryParams(EndpointInterface $endpoint) : array
{
$requiresRequestHeaders = $endpoint->doesRequireRequestHeaders();
$queryParams = $endpoint->getQueryParams();
if ($requiresRequestHeaders) {
$queryParams['requestHeader'] = [
'clientUsername' => $this->environment->getClientUsername(),
'clientTransactionId' => (string) Ulid::generate(),
'channel' => $this->environment->getChannel(),
'languageCode' => $queryParams['languageCode'] ?? $this->languageCode,
'airlineCode' => $queryParams['airlineCode'] ?? $this->airlineCode
];
if (array_key_exists('languageCode', $queryParams)) {
unset($queryParams['languageCode']);
}
if (array_key_exists('airlineCode', $queryParams)) {
unset($queryParams['airlineCode']);
}
}
return $queryParams;
}
}
7 changes: 5 additions & 2 deletions src/API/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public static function create()
public function setEnvironment(
string $apiUrl,
string $apiKey,
string $apiSecret
string $apiSecret,
?string $clientUsername = null,
?string $appName = null,
?string $channel = null
) {
$this->environment = new Environment($apiUrl, $apiKey, $apiSecret);
$this->environment = new Environment($apiUrl, $apiKey, $apiSecret, $clientUsername, $appName, $channel);
return $this;
}

Expand Down
1 change: 1 addition & 0 deletions src/API/Endpoint/CalculateAwardMilesWithTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public function __construct(CalculateAwardMilesWithTaxParameters $queryParameter
$this->endpoint = '/calculateAwardMilesWithTax';
$this->responseRoot = 'return';
$this->queryParameters = $queryParameters->getValue();
$this->requestHeaderRequired = false;
}
}
1 change: 1 addition & 0 deletions src/API/Endpoint/CalculateFlightMiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public function __construct(CalculateFlightMilesParameters $queryParameters)
$this->endpoint = '/calculateFlightMiles';
$this->responseRoot = 'milesResponseDetail';
$this->queryParameters = $queryParameters->getValue();
$this->requestHeaderRequired = false;
}
}
11 changes: 10 additions & 1 deletion src/API/Endpoint/EndpointAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace TK\API\Endpoint;

use TK\API\Client;

abstract class EndpointAbstract
{

protected $endpoint;
protected $queryParameters = [];
protected $headers = [];
protected $httpRequestMethod = 'POST';
protected $httpRequestMethod = Client::HTTP_POST;
protected $responseRoot = '';
protected $requestHeaderRequired = true;

public function getEndpoint() : string
{
Expand All @@ -34,4 +38,9 @@ public function getResponseRoot() : string
{
return $this->responseRoot;
}

public function doesRequireRequestHeaders() : bool
{
return $this->requestHeaderRequired;
}
}
1 change: 1 addition & 0 deletions src/API/Endpoint/EndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public function getHeaders() : array;
public function getQueryParams() : array;
public function getHttpRequestMethod() : string;
public function getResponseRoot() : string;
public function doesRequireRequestHeaders() : bool;
}
1 change: 1 addition & 0 deletions src/API/Endpoint/GetFareFamilyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public function __construct(GetFareFamilyListParameters $queryParameters)
$this->endpoint = '/getFareFamilyList';
$this->responseRoot = 'fareFamilyOTAResponse';
$this->queryParameters = $queryParameters->getValue();
$this->requestHeaderRequired = false;
}
}
1 change: 0 additions & 1 deletion src/API/Endpoint/GetPortList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ final class GetPortList extends EndpointAbstract implements EndpointInterface
public function __construct(GetPortListParameters $queryParameters)
{
$this->endpoint = '/getPortList';
$this->httpRequestMethod = 'GET';
$this->responseRoot = 'Port';
$this->queryParameters = $queryParameters->getValue();
}
Expand Down
1 change: 0 additions & 1 deletion src/API/Endpoint/RetrieveReservationDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function __construct(RetrieveReservationDetailParameters $queryParameters
{
$this->endpoint = '/retrieveReservationDetail';
$this->responseRoot = 'retrieveReservationOTAResponse';
$this->httpRequestMethod = 'get';
$this->queryParameters = $queryParameters->getValue();
}
}
48 changes: 46 additions & 2 deletions src/API/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,44 @@ final class Environment
private $apiUrl;
private $apiKey;
private $apiSecret;
private $clientUsername;
private $channel;
private $appName;

public function __construct(string $apiUrl, string $apiKey, string $apiSecret)
{
public function __construct(
string $apiUrl,
string $apiKey,
string $apiSecret,
?string $clientUsername = null,
?string $appName = null,
?string $channel = 'WEB'
) {
$this->apiUrl = $apiUrl;
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->clientUsername = $clientUsername;
$this->channel = $channel;
$this->appName = $appName;
}

public static function createWithUserName (
string $apiUrl,
string $apiKey,
string $apiSecret,
?string $clientUsername = null,
?string $channel = null
) :self {
return new self($apiUrl, $apiKey, $apiSecret, $clientUsername, null, $channel);
}
public static function createWithAppName (
string $apiUrl,
string $apiKey,
string $apiSecret,
?string $appName = null,
?string $channel = null
) :self {
return new self($apiUrl, $apiKey, $apiSecret, null, $appName, $channel);
}
public function getApiUrl() :string
{
return $this->apiUrl;
Expand All @@ -29,4 +59,18 @@ public function getApiSecret() : string
{
return $this->apiSecret;
}

public function getClientUsername() : string
{
return $this->clientUsername;
}

public function getChannel() : string
{
return $this->channel ?? 'WEB';
}
public function getAppName() : string
{
return $this->appName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function createFromArray(array $parameters) : GetPortListParameter
if (array_key_exists('languageCode', $parameters)) {
$getPortListParameters = $getPortListParameters->withLanguageCode($parameters['languageCode']);
}
return$getPortListParameters;
return $getPortListParameters;
}


Expand Down
10 changes: 6 additions & 4 deletions src/API/ValueObject/GetPortListParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TK\API\ValueObject;

use TK\API\Exception\InvalidArgumentException;
use function in_array;

final class GetPortListParameters implements ValueObjectInterface
{
Expand All @@ -26,7 +27,7 @@ public function __construct(string $airlineCode)

private function setAirlineCode(?string $airlineCode) : void
{
if (!\in_array($airlineCode, self::$airlineCodeEnum, true)) {
if (! in_array($airlineCode, self::$airlineCodeEnum, true)) {
throw new InvalidArgumentException(
'Invalid AirlineCode provided. Must be one of these: "TK", "AJ"'
);
Expand All @@ -52,12 +53,13 @@ public function withLanguageCode(string $languageCode) : GetPortListParameters

public function getValue() : array
{
$getPortListParameters = [
'airlineCode' => $this->airlineCode
];
$getPortListParameters = [];
if ($this->languageCode !== null) {
$getPortListParameters['languageCode'] = $this->languageCode;
}
if ($this->airlineCode !== null) {
$getPortListParameters['airlineCode'] = $this->airlineCode;
}
return $getPortListParameters;
}
}
6 changes: 4 additions & 2 deletions src/API/ValueObject/RetrieveReservationDetailParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public function __construct(string $uniqueId, string $surname)
public function getValue() : array
{
return [
'UniqueId' => $this->uniqueId,
'Surname' => $this->surname
'retrieveReservationOTARequest' => [
'UniqueId' => $this->uniqueId,
'Surname' => $this->surname
]
];
}
}
Loading

0 comments on commit 041b6d7

Please sign in to comment.