Skip to content

Commit 041b6d7

Browse files
committed
new api structure applied
1 parent 7d15adb commit 041b6d7

24 files changed

+194
-65
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"guzzlehttp/guzzle": "^6.3",
99
"psr/http-message": "^1.0",
1010
"psr/log": "^1.0",
11-
"monolog/monolog": "^1.23"
11+
"monolog/monolog": "^1.23",
12+
"robinvdvleuten/ulid": "^3.0"
1213
},
1314
"require-dev": {
1415
"squizlabs/php_codesniffer": "^3.3",

src/API/Client.php

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
use TK\API\Exception\RequestException;
2020
use TypeError;
2121
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
22+
use Ulid\Ulid;
2223

2324
final class Client
2425
{
25-
26+
public const HTTP_POST = 'POST';
27+
public const HTTP_GET = 'GET';
2628
/**
2729
* @var array
2830
*/
@@ -40,18 +42,21 @@ final class Client
4042

4143

4244
/**
43-
* @var string
45+
* @var Environment
4446
*/
45-
private $apiUrl;
47+
private $environment;
4648
/**
4749
* @var GuzzleClient
4850
*/
4951
private $guzzleClient;
5052

5153
private $logger;
54+
private $languageCode;
55+
private $airlineCode;
56+
5257

5358
private $headers = [
54-
'User-Agent' => 'mkorkmaz/tk-api-php-client 1.0'
59+
'User-Agent' => 'mkorkmaz/tk-api-php-client 2.0'
5560
];
5661

5762
/**
@@ -62,13 +67,28 @@ final class Client
6267
*/
6368
public function __construct(Environment $environment, GuzzleClient $guzzleClient, LoggerInterface $logger)
6469
{
65-
$this->apiUrl = $environment->getApiUrl();
70+
$this->environment = $environment;
6671
$this->headers['apiKey'] = $environment->getApiKey();
6772
$this->headers['apiSecret'] = $environment->getApiSecret();
6873
$this->guzzleClient = $guzzleClient;
6974
$this->logger = $logger;
75+
$this->airlineCode = 'TK';
76+
$this->languageCode = 'TR';
7077
}
7178

79+
public function withAirlineCode(string $airlineCode) : self
80+
{
81+
$new = clone $this;
82+
$new->airlineCode = $airlineCode;
83+
return $new;
84+
}
85+
86+
public function withLanguageCode(string $languageCode) : self
87+
{
88+
$new = clone $this;
89+
$new->languageCode = $languageCode;
90+
return $new;
91+
}
7292
/**
7393
* @param $name
7494
* @param $arguments
@@ -147,34 +167,60 @@ private function request(EndpointInterface $endpoint) : array
147167
private function httpRequest(EndpointInterface $endpoint) : ResponseInterface
148168
{
149169
$this->headers = array_merge($this->headers, $endpoint->getHeaders());
150-
$uri = $this->apiUrl . $endpoint->getEndpoint();
170+
$queryParams = $this->getQueryParams($endpoint);
171+
172+
$uri = $this->environment->getApiUrl() . $endpoint->getEndpoint();
151173
$options = [];
152-
$httpRequestMethod = strtolower($endpoint->getHttpRequestMethod());
153-
if ($httpRequestMethod === 'post') {
174+
$httpRequestMethod = $endpoint->getHttpRequestMethod();
175+
if ($httpRequestMethod === self::HTTP_POST) {
154176
$this->headers['Content-Type'] = 'application/json';
155-
$options['json'] = $endpoint->getQueryParams();
177+
$options['json'] = $queryParams;
156178
}
157-
if ($httpRequestMethod === 'get') {
158-
$uri .= '?' . http_build_query($endpoint->getQueryParams());
179+
if ($httpRequestMethod === self::HTTP_GET) {
180+
$uri .= '?' . http_build_query($queryParams);
159181
}
182+
$uri .= '?apikey='. $this->environment->getApiKey();
160183
$options['headers'] = $this->headers;
161-
162184
$this->logger->debug(
163185
'API call for :' . $endpoint->getEndpoint(),
164186
[
165187
'httpRequestMethod' => $httpRequestMethod,
166188
'uri' => $uri,
167189
'headers' => $this->headers,
168-
'queryParams' => $endpoint->getQueryParams()
190+
'queryParams' => $queryParams
169191
]
170192
);
171193
try {
172-
return $this->guzzleClient->{$httpRequestMethod}($uri, $options);
194+
return $this->guzzleClient->{strtolower($httpRequestMethod)}($uri, $options);
173195
} catch (GuzzleRequestException $e) {
174-
$exceptionMessage = (string) $e->getResponse()->getBody()->getContents();
196+
$exceptionMessage = (string) $e->getResponse()
197+
->getBody()
198+
->getContents();
175199
$message = sprintf('TK API REQUEST ERROR: %s', $exceptionMessage);
176200
$this->logger->error($message);
177201
throw new RequestException($message);
178202
}
179203
}
204+
205+
private function getQueryParams(EndpointInterface $endpoint) : array
206+
{
207+
$requiresRequestHeaders = $endpoint->doesRequireRequestHeaders();
208+
$queryParams = $endpoint->getQueryParams();
209+
if ($requiresRequestHeaders) {
210+
$queryParams['requestHeader'] = [
211+
'clientUsername' => $this->environment->getClientUsername(),
212+
'clientTransactionId' => (string) Ulid::generate(),
213+
'channel' => $this->environment->getChannel(),
214+
'languageCode' => $queryParams['languageCode'] ?? $this->languageCode,
215+
'airlineCode' => $queryParams['airlineCode'] ?? $this->airlineCode
216+
];
217+
if (array_key_exists('languageCode', $queryParams)) {
218+
unset($queryParams['languageCode']);
219+
}
220+
if (array_key_exists('airlineCode', $queryParams)) {
221+
unset($queryParams['airlineCode']);
222+
}
223+
}
224+
return $queryParams;
225+
}
180226
}

src/API/ClientBuilder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public static function create()
2121
public function setEnvironment(
2222
string $apiUrl,
2323
string $apiKey,
24-
string $apiSecret
24+
string $apiSecret,
25+
?string $clientUsername = null,
26+
?string $appName = null,
27+
?string $channel = null
2528
) {
26-
$this->environment = new Environment($apiUrl, $apiKey, $apiSecret);
29+
$this->environment = new Environment($apiUrl, $apiKey, $apiSecret, $clientUsername, $appName, $channel);
2730
return $this;
2831
}
2932

src/API/Endpoint/CalculateAwardMilesWithTax.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public function __construct(CalculateAwardMilesWithTaxParameters $queryParameter
1212
$this->endpoint = '/calculateAwardMilesWithTax';
1313
$this->responseRoot = 'return';
1414
$this->queryParameters = $queryParameters->getValue();
15+
$this->requestHeaderRequired = false;
1516
}
1617
}

src/API/Endpoint/CalculateFlightMiles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public function __construct(CalculateFlightMilesParameters $queryParameters)
1212
$this->endpoint = '/calculateFlightMiles';
1313
$this->responseRoot = 'milesResponseDetail';
1414
$this->queryParameters = $queryParameters->getValue();
15+
$this->requestHeaderRequired = false;
1516
}
1617
}

src/API/Endpoint/EndpointAbstract.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace TK\API\Endpoint;
44

5+
use TK\API\Client;
6+
57
abstract class EndpointAbstract
68
{
9+
710
protected $endpoint;
811
protected $queryParameters = [];
912
protected $headers = [];
10-
protected $httpRequestMethod = 'POST';
13+
protected $httpRequestMethod = Client::HTTP_POST;
1114
protected $responseRoot = '';
15+
protected $requestHeaderRequired = true;
1216

1317
public function getEndpoint() : string
1418
{
@@ -34,4 +38,9 @@ public function getResponseRoot() : string
3438
{
3539
return $this->responseRoot;
3640
}
41+
42+
public function doesRequireRequestHeaders() : bool
43+
{
44+
return $this->requestHeaderRequired;
45+
}
3746
}

src/API/Endpoint/EndpointInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public function getHeaders() : array;
1010
public function getQueryParams() : array;
1111
public function getHttpRequestMethod() : string;
1212
public function getResponseRoot() : string;
13+
public function doesRequireRequestHeaders() : bool;
1314
}

src/API/Endpoint/GetFareFamilyList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public function __construct(GetFareFamilyListParameters $queryParameters)
1212
$this->endpoint = '/getFareFamilyList';
1313
$this->responseRoot = 'fareFamilyOTAResponse';
1414
$this->queryParameters = $queryParameters->getValue();
15+
$this->requestHeaderRequired = false;
1516
}
1617
}

src/API/Endpoint/GetPortList.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ final class GetPortList extends EndpointAbstract implements EndpointInterface
1010
public function __construct(GetPortListParameters $queryParameters)
1111
{
1212
$this->endpoint = '/getPortList';
13-
$this->httpRequestMethod = 'GET';
1413
$this->responseRoot = 'Port';
1514
$this->queryParameters = $queryParameters->getValue();
1615
}

src/API/Endpoint/RetrieveReservationDetail.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public function __construct(RetrieveReservationDetailParameters $queryParameters
1111
{
1212
$this->endpoint = '/retrieveReservationDetail';
1313
$this->responseRoot = 'retrieveReservationOTAResponse';
14-
$this->httpRequestMethod = 'get';
1514
$this->queryParameters = $queryParameters->getValue();
1615
}
1716
}

0 commit comments

Comments
 (0)