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

Commit 18dbd00

Browse files
author
Jens Schulze
committed
feat(Client): add configuration options for guzzle
Closes #345
1 parent 58ad977 commit 18dbd00

File tree

6 files changed

+171
-26
lines changed

6 files changed

+171
-26
lines changed

src/Core/Client.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Commercetools\Core\Client\Adapter\CorrelationIdAware;
1010
use Commercetools\Core\Client\Adapter\TokenProviderAware;
1111
use Commercetools\Core\Helper\CorrelationIdProvider;
12+
use Commercetools\Core\Client\Adapter\AdapterOptionInterface;
1213
use Commercetools\Core\Response\ErrorResponse;
1314
use Psr\Http\Message\RequestInterface;
1415
use Psr\Http\Message\ResponseInterface;
@@ -201,6 +202,10 @@ public function setLogger(LoggerInterface $logger = null)
201202
public function getHttpClient($options = [])
202203
{
203204
if (is_null($this->httpClient)) {
205+
$clientOptions = $this->config->getClientOptions();
206+
if (count($clientOptions)) {
207+
$options = array_merge($clientOptions, $options);
208+
}
204209
$client = parent::getHttpClient($options);
205210
if ($client instanceof TokenProviderAware) {
206211
$client->setOAuthTokenProvider($this->getOauthManager());
@@ -237,19 +242,25 @@ protected function getBaseUrl()
237242
*
238243
* @param ClientRequestInterface $request
239244
* @param array $headers
245+
* @param array $clientOptions
240246
* @return ApiResponseInterface
241247
* @throws ApiException
242248
* @throws InvalidTokenException
243249
*/
244-
public function execute(ClientRequestInterface $request, array $headers = null)
250+
public function execute(ClientRequestInterface $request, array $headers = null, array $clientOptions = [])
245251
{
246252
if ($request instanceof ContextAwareInterface) {
247253
$request->setContextIfNull($this->getConfig()->getContext());
248254
}
249255
$httpRequest = $this->createHttpRequest($request, $headers);
250256

251257
try {
252-
$httpResponse = $this->getHttpClient()->execute($httpRequest);
258+
$client = $this->getHttpClient();
259+
if ($client instanceof AdapterOptionInterface) {
260+
$httpResponse = $client->execute($httpRequest, $clientOptions);
261+
} else {
262+
$httpResponse = $client->execute($httpRequest);
263+
}
253264
$response = $request->buildResponse($httpResponse);
254265
} catch (ApiException $exception) {
255266
if ($exception instanceof InvalidTokenException && !$this->tokenRefreshed) {
@@ -273,15 +284,21 @@ public function execute(ClientRequestInterface $request, array $headers = null)
273284
/**
274285
* Executes an API request asynchronously
275286
* @param ClientRequestInterface $request
287+
* @param array $clientOptions
276288
* @return ApiResponseInterface
277289
*/
278-
public function executeAsync(ClientRequestInterface $request, array $headers = null)
290+
public function executeAsync(ClientRequestInterface $request, array $headers = null, array $clientOptions = [])
279291
{
280292
if ($request instanceof ContextAwareInterface) {
281293
$request->setContextIfNull($this->getConfig()->getContext());
282294
}
283295
$httpRequest = $this->createHttpRequest($request, $headers);
284-
$response = $request->buildResponse($this->getHttpClient()->executeAsync($httpRequest));
296+
$client = $this->getHttpClient();
297+
if ($client instanceof AdapterOptionInterface) {
298+
$response = $request->buildResponse($client->executeAsync($httpRequest, $clientOptions));
299+
} else {
300+
$response = $request->buildResponse($client->executeAsync($httpRequest));
301+
}
285302

286303
$response = $response->then(
287304
function ($httpResponse) use ($httpRequest) {
@@ -321,13 +338,19 @@ protected function createHttpRequest(ClientRequestInterface $request, array $hea
321338
/**
322339
* Executes API requests in batch
323340
* @param array $headers
341+
* @param array $clientOptions
324342
* @return ApiResponseInterface[]
325343
* @throws ApiException
326344
*/
327-
public function executeBatch(array $headers = null)
345+
public function executeBatch(array $headers = null, array $clientOptions = [])
328346
{
329347
$requests = $this->getBatchHttpRequests($headers);
330-
$httpResponses = $this->getHttpClient()->executeBatch($requests);
348+
$client = $this->getHttpClient();
349+
if ($client instanceof AdapterOptionInterface) {
350+
$httpResponses = $client->executeBatch($requests, $clientOptions);
351+
} else {
352+
$httpResponses = $client->executeBatch($requests);
353+
}
331354

332355
$responses = [];
333356
foreach ($httpResponses as $key => $httpResponse) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Client\Adapter;
7+
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
interface AdapterOptionInterface extends AdapterInterface
12+
{
13+
/**
14+
* @param RequestInterface $request
15+
* @param array $clientOptions
16+
* @return ResponseInterface
17+
*/
18+
public function execute(RequestInterface $request, array $clientOptions = []);
19+
20+
/**
21+
* @param RequestInterface[] $requests
22+
* @param array $clientOptions
23+
* @return ResponseInterface[]
24+
*/
25+
public function executeBatch(array $requests, array $clientOptions = []);
26+
27+
/**
28+
* @param RequestInterface $request
29+
* @param array $clientOptions
30+
* @return AdapterPromiseInterface
31+
*/
32+
public function executeAsync(RequestInterface $request, array $clientOptions = []);
33+
}

src/Core/Client/Adapter/Guzzle5Adapter.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use Commercetools\Core\Error\ApiException;
2222
use Psr\Log\LogLevel;
2323

24-
class Guzzle5Adapter implements AdapterInterface, CorrelationIdAware, TokenProviderAware
24+
class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
2525
{
26+
const DEFAULT_CONCURRENCY = 25;
27+
2628
/**
2729
* @var Client
2830
*/
@@ -33,6 +35,8 @@ class Guzzle5Adapter implements AdapterInterface, CorrelationIdAware, TokenProvi
3335
*/
3436
protected $logger;
3537

38+
private $concurrency;
39+
3640
/**
3741
* @param array $options
3842
*/
@@ -42,6 +46,10 @@ public function __construct(array $options = [])
4246
$options['base_url'] = $options['base_uri'];
4347
unset($options['base_uri']);
4448
}
49+
if (isset($options['concurrency'])) {
50+
$options['pool_size'] = $options['concurrency'];
51+
unset($options['concurrency']);
52+
}
4553
if (isset($options['headers'])) {
4654
$options['defaults']['headers'] = $options['headers'];
4755
unset($options['headers']);
@@ -52,10 +60,12 @@ public function __construct(array $options = [])
5260
'verify' => true,
5361
'timeout' => 60,
5462
'connect_timeout' => 10,
55-
'pool_size' => 25
63+
'pool_size' => self::DEFAULT_CONCURRENCY
5664
],
5765
$options
5866
);
67+
$this->concurrency = $options['pool_size'];
68+
5969
$this->client = new Client($options);
6070
}
6171

@@ -93,6 +103,7 @@ public function getEmitter()
93103

94104
/**
95105
* @param RequestInterface $request
106+
* @param array $clientOptions
96107
* @return ResponseInterface
97108
* @throws \Commercetools\Core\Error\ApiException
98109
* @throws \Commercetools\Core\Error\BadGatewayException
@@ -104,12 +115,15 @@ public function getEmitter()
104115
* @throws \Commercetools\Core\Error\NotFoundException
105116
* @throws \Commercetools\Core\Error\ServiceUnavailableException
106117
*/
107-
public function execute(RequestInterface $request)
118+
public function execute(RequestInterface $request, array $clientOptions = [])
108119
{
109120
$options = [
110121
'headers' => $request->getHeaders(),
111122
'body' => (string)$request->getBody()
112123
];
124+
if (count($clientOptions)) {
125+
$options = array_merge($options, $clientOptions);
126+
}
113127

114128
try {
115129
$guzzleRequest = $this->client->createRequest($request->getMethod(), (string)$request->getUri(), $options);
@@ -137,6 +151,7 @@ protected function packResponse(\GuzzleHttp\Message\ResponseInterface $response
137151

138152
/**
139153
* @param RequestInterface[] $requests
154+
* @param array $clientOptions
140155
* @return \Psr\Http\Message\ResponseInterface[]
141156
* @throws \Commercetools\Core\Error\ApiException
142157
* @throws \Commercetools\Core\Error\BadGatewayException
@@ -148,11 +163,12 @@ protected function packResponse(\GuzzleHttp\Message\ResponseInterface $response
148163
* @throws \Commercetools\Core\Error\NotFoundException
149164
* @throws \Commercetools\Core\Error\ServiceUnavailableException
150165
*/
151-
public function executeBatch(array $requests)
166+
public function executeBatch(array $requests, array $clientOptions = [])
152167
{
153168
$results = Pool::batch(
154169
$this->client,
155-
$this->getBatchHttpRequests($requests)
170+
$this->getBatchHttpRequests($requests, $clientOptions),
171+
['pool_size' => $this->concurrency]
156172
);
157173

158174
$responses = [];
@@ -171,19 +187,26 @@ public function executeBatch(array $requests)
171187
}
172188

173189
/**
190+
* @param array $requests
191+
* @param array $clientOptions
174192
* @return array
175193
*/
176-
protected function getBatchHttpRequests(array $requests)
194+
protected function getBatchHttpRequests(array $requests, array $clientOptions = [])
177195
{
178196
$requests = array_map(
179-
function ($request) {
197+
function ($request) use ($clientOptions) {
198+
$options = ['headers' => $request->getHeaders()];
199+
if (count($clientOptions)) {
200+
$options = array_merge($options, $clientOptions);
201+
}
202+
180203
/**
181204
* @var RequestInterface $request
182205
*/
183206
return $this->client->createRequest(
184207
$request->getMethod(),
185208
(string)$request->getUri(),
186-
['headers' => $request->getHeaders()]
209+
$options
187210
);
188211
},
189212
$requests
@@ -224,15 +247,19 @@ public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
224247

225248
/**
226249
* @param RequestInterface $request
250+
* @param array $clientOptions
227251
* @return AdapterPromiseInterface
228252
*/
229-
public function executeAsync(RequestInterface $request)
253+
public function executeAsync(RequestInterface $request, array $clientOptions = [])
230254
{
231255
$options = [
232256
'future' => true,
233257
'exceptions' => false,
234258
'headers' => $request->getHeaders()
235259
];
260+
if (count($clientOptions)) {
261+
$options = array_merge($options, $clientOptions);
262+
}
236263
$request = $this->client->createRequest($request->getMethod(), (string)$request->getUri(), $options);
237264
$guzzlePromise = $this->client->send($request, $options);
238265

src/Core/Client/Adapter/Guzzle6Adapter.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@
1313
use GuzzleHttp\MessageFormatter;
1414
use GuzzleHttp\Middleware;
1515
use GuzzleHttp\Pool;
16-
use GuzzleHttp\Promise\PromiseInterface;
1716
use Psr\Http\Message\RequestInterface;
1817
use Psr\Http\Message\ResponseInterface;
1918
use Psr\Log\LoggerInterface;
20-
use Commercetools\Core\Error\Message;
2119
use Commercetools\Core\Error\ApiException;
2220
use Psr\Log\LogLevel;
2321

24-
class Guzzle6Adapter implements AdapterInterface, CorrelationIdAware, TokenProviderAware
22+
class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
2523
{
24+
const DEFAULT_CONCURRENCY = 25;
2625
/**
2726
* @var Client
2827
*/
2928
protected $client;
3029

3130
protected $logger;
3231

32+
private $concurrency;
33+
3334
public function __construct(array $options = [])
3435
{
3536
$options = array_merge(
@@ -38,10 +39,11 @@ public function __construct(array $options = [])
3839
'verify' => true,
3940
'timeout' => 60,
4041
'connect_timeout' => 10,
41-
'pool_size' => 25
42+
'concurrency' => self::DEFAULT_CONCURRENCY
4243
],
4344
$options
4445
);
46+
$this->concurrency = $options['concurrency'];
4547
$this->client = new Client($options);
4648
}
4749

@@ -124,12 +126,23 @@ public function addHandler($handler)
124126

125127
/**
126128
* @param RequestInterface $request
129+
* @param array $clientOptions
127130
* @return ResponseInterface
131+
* @throws ApiException
132+
* @throws \Commercetools\Core\Error\ApiException
133+
* @throws \Commercetools\Core\Error\BadGatewayException
134+
* @throws \Commercetools\Core\Error\ConcurrentModificationException
135+
* @throws \Commercetools\Core\Error\ErrorResponseException
136+
* @throws \Commercetools\Core\Error\GatewayTimeoutException
137+
* @throws \Commercetools\Core\Error\InternalServerErrorException
138+
* @throws \Commercetools\Core\Error\InvalidTokenException
139+
* @throws \Commercetools\Core\Error\NotFoundException
140+
* @throws \Commercetools\Core\Error\ServiceUnavailableException
128141
*/
129-
public function execute(RequestInterface $request)
142+
public function execute(RequestInterface $request, array $clientOptions = [])
130143
{
131144
try {
132-
$response = $this->client->send($request);
145+
$response = $this->client->send($request, $clientOptions);
133146
} catch (RequestException $exception) {
134147
$response = $exception->getResponse();
135148
throw ApiException::create($request, $response, $exception);
@@ -140,13 +153,18 @@ public function execute(RequestInterface $request)
140153

141154
/**
142155
* @param RequestInterface[] $requests
156+
* @param array $clientOptions
143157
* @return ResponseInterface[]
144158
*/
145-
public function executeBatch(array $requests)
159+
public function executeBatch(array $requests, array $clientOptions = [])
146160
{
147161
$results = Pool::batch(
148162
$this->client,
149-
$requests
163+
$requests,
164+
[
165+
'concurrency' => $this->concurrency,
166+
'options' => $clientOptions
167+
]
150168
);
151169

152170
$responses = [];
@@ -187,11 +205,12 @@ public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
187205

188206
/**
189207
* @param RequestInterface $request
208+
* @param array $clientOptions
190209
* @return AdapterPromiseInterface
191210
*/
192-
public function executeAsync(RequestInterface $request)
211+
public function executeAsync(RequestInterface $request, array $clientOptions = [])
193212
{
194-
$guzzlePromise = $this->client->sendAsync($request);
213+
$guzzlePromise = $this->client->sendAsync($request, $clientOptions);
195214

196215
return new Guzzle6Promise($guzzlePromise);
197216
}

0 commit comments

Comments
 (0)