1919use TK \API \Exception \RequestException ;
2020use TypeError ;
2121use GuzzleHttp \Exception \RequestException as GuzzleRequestException ;
22+ use Ulid \Ulid ;
2223
2324final 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}
0 commit comments