Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 74c5a15

Browse files
author
Jens Schulze
committed
feat(Client): add possibility for additional headers when executing request
1 parent 5687380 commit 74c5a15

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/Client.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,17 @@ protected function getBaseUrl()
224224
* Executes an API request synchronously
225225
*
226226
* @param ClientRequestInterface $request
227+
* @param array $headers
227228
* @return ApiResponseInterface
228-
* @throws InvalidTokenException
229229
* @throws ApiException
230-
* @throws \Exception
230+
* @throws InvalidTokenException
231231
*/
232-
public function execute(ClientRequestInterface $request)
232+
public function execute(ClientRequestInterface $request, array $headers = null)
233233
{
234234
if ($request instanceof ContextAwareInterface) {
235235
$request->setContextIfNull($this->getConfig()->getContext());
236236
}
237-
$httpRequest = $this->createHttpRequest($request);
237+
$httpRequest = $this->createHttpRequest($request, $headers);
238238

239239
try {
240240
$httpResponse = $this->getHttpClient()->execute($httpRequest);
@@ -263,12 +263,12 @@ public function execute(ClientRequestInterface $request)
263263
* @param ClientRequestInterface $request
264264
* @return ApiResponseInterface
265265
*/
266-
public function executeAsync(ClientRequestInterface $request)
266+
public function executeAsync(ClientRequestInterface $request, array $headers = null)
267267
{
268268
if ($request instanceof ContextAwareInterface) {
269269
$request->setContextIfNull($this->getConfig()->getContext());
270270
}
271-
$httpRequest = $this->createHttpRequest($request);
271+
$httpRequest = $this->createHttpRequest($request, $headers);
272272
$response = $request->buildResponse($this->getHttpClient()->executeAsync($httpRequest));
273273

274274
$response = $response->then(
@@ -283,27 +283,36 @@ function ($httpResponse) use ($httpRequest) {
283283

284284
/**
285285
* @param ClientRequestInterface $request
286+
* @param array $headers
286287
* @return RequestInterface
287288
*/
288-
protected function createHttpRequest(ClientRequestInterface $request)
289+
protected function createHttpRequest(ClientRequestInterface $request, array $headers = null)
289290
{
290291
$token = $this->getOauthManager()->getToken();
291-
292292
$httpRequest = $request->httpRequest();
293293
$httpRequest = $httpRequest
294294
->withHeader('Authorization', 'Bearer ' . $token->getToken())
295295
;
296+
if (is_array($headers)) {
297+
foreach ($headers as $headerName => $headerValues) {
298+
$httpRequest = $httpRequest
299+
->withAddedHeader($headerName, $headerValues)
300+
;
301+
}
302+
}
303+
296304
return $httpRequest;
297305
}
298306

299307
/**
300308
* Executes API requests in batch
301-
* @return Response\ApiResponseInterface[]
309+
* @param array $headers
310+
* @return ApiResponseInterface[]
302311
* @throws ApiException
303312
*/
304-
public function executeBatch()
313+
public function executeBatch(array $headers = null)
305314
{
306-
$requests = $this->getBatchHttpRequests();
315+
$requests = $this->getBatchHttpRequests($headers);
307316
$httpResponses = $this->getHttpClient()->executeBatch($requests);
308317

309318
$responses = [];
@@ -398,13 +407,14 @@ protected function format(RequestInterface $request, ResponseInterface $response
398407
}
399408

400409
/**
410+
* @param array $headers
401411
* @return array
402412
*/
403-
protected function getBatchHttpRequests()
413+
protected function getBatchHttpRequests(array $headers = null)
404414
{
405415
$requests = array_map(
406-
function ($request) {
407-
return $this->createHttpRequest($request);
416+
function ($request) use ($headers) {
417+
return $this->createHttpRequest($request, $headers);
408418
},
409419
$this->batchRequests
410420
);

src/Request/AbstractApiRequest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,11 @@ public function map(array $data, Context $context = null, MapperInterface $mappe
228228

229229
/**
230230
* @param Client $client
231+
* @param array $headers
231232
* @return ApiResponseInterface
232233
*/
233-
public function executeWithClient(Client $client)
234+
public function executeWithClient(Client $client, array $headers = null)
234235
{
235-
return $client->execute($this);
236+
return $client->execute($this, $headers);
236237
}
237238
}

tests/integration/Cart/CartQueryRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testGetByCustomerId()
9696
$cart = $this->createCart($draft);
9797

9898
$request = CartByCustomerIdGetRequest::ofCustomerId($cart->getCustomerId());
99-
$response = $request->executeWithClient($this->getClient());
99+
$response = $request->executeWithClient($this->getClient(), ['X-Vrap-Disable-Validation' => 'response']);
100100
$result = $request->mapResponse($response);
101101

102102
$this->assertInstanceOf(Cart::class, $result);

tests/integration/ShippingMethod/ShippingMethodQueryRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testByLocation()
9292
$shippingMethod = $this->createShippingMethod($draft);
9393

9494
$request = ShippingMethodByLocationGetRequest::ofCountry('DE')->withState($this->getRegion());
95-
$response = $request->executeWithClient($this->getClient());
95+
$response = $request->executeWithClient($this->getClient(), ['X-Vrap-Disable-Validation' => 'response']);
9696
$result = $request->mapResponse($response);
9797

9898
$this->assertInstanceOf(ShippingMethodCollection::class, $result);

0 commit comments

Comments
 (0)