@@ -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 );
0 commit comments