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

Commit 3a55d24

Browse files
committed
feat(ClientFactory): create HttpClient with CTP client compatible request signature
1 parent b3c44fe commit 3a55d24

File tree

3 files changed

+145
-13
lines changed

3 files changed

+145
-13
lines changed

src/Core/Client/ClientFactory.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Commercetools\Core\Client;
44

5-
use Cache\Adapter\Filesystem\FilesystemCachePool;
65
use Commercetools\Core\Cache\CacheAdapterFactory;
76
use Commercetools\Core\Client\OAuth\ClientCredentials;
87
use Commercetools\Core\Client\OAuth\CredentialTokenProvider;
@@ -11,19 +10,15 @@
1110
use Commercetools\Core\Config;
1211
use Commercetools\Core\Error\ApiException;
1312
use Commercetools\Core\Error\DeprecatedException;
14-
use Commercetools\Core\Error\InvalidCredentialsError;
1513
use Commercetools\Core\Error\InvalidTokenException;
16-
use Commercetools\Core\Error\Message;
1714
use Commercetools\Core\Helper\CorrelationIdProvider;
1815
use Commercetools\Core\Error\InvalidArgumentException;
1916
use Commercetools\Core\Response\AbstractApiResponse;
20-
use GuzzleHttp\Client as HttpClient;
17+
use GuzzleHttp\Client;
2118
use GuzzleHttp\Exception\RequestException;
2219
use GuzzleHttp\HandlerStack;
2320
use GuzzleHttp\MessageFormatter;
2421
use GuzzleHttp\Middleware;
25-
use League\Flysystem\Adapter\Local;
26-
use League\Flysystem\Filesystem;
2722
use Psr\Cache\CacheItemPoolInterface;
2823
use Psr\Http\Message\RequestInterface;
2924
use Psr\Http\Message\ResponseInterface;
@@ -50,14 +45,16 @@ public function __construct()
5045
}
5146

5247
/**
48+
* @param string $clientClass
5349
* @param Config|array $config
5450
* @param LoggerInterface $logger
5551
* @param CacheItemPoolInterface|CacheInterface $cache
5652
* @param TokenProvider $provider
5753
* @param CacheAdapterFactory $cacheAdapterFactory
58-
* @return HttpClient
54+
* @return Client
5955
*/
60-
public function createClient(
56+
public function createCustomClient(
57+
$clientClass,
6158
$config,
6259
LoggerInterface $logger = null,
6360
$cache = null,
@@ -84,7 +81,25 @@ public function createClient(
8481

8582
$options = $this->getDefaultOptions($config);
8683

87-
return $this->createGuzzle6Client($options, $oauthHandler, $logger, $config->getCorrelationIdProvider());
84+
return $this->createGuzzle6Client($clientClass, $options, $oauthHandler, $logger, $config->getCorrelationIdProvider());
85+
}
86+
87+
/**
88+
* @param Config|array $config
89+
* @param LoggerInterface $logger
90+
* @param CacheItemPoolInterface|CacheInterface $cache
91+
* @param TokenProvider $provider
92+
* @param CacheAdapterFactory $cacheAdapterFactory
93+
* @return HttpClient
94+
*/
95+
public function createClient(
96+
$config,
97+
LoggerInterface $logger = null,
98+
$cache = null,
99+
TokenProvider $provider = null,
100+
CacheAdapterFactory $cacheAdapterFactory = null
101+
) {
102+
return $this->createCustomClient(HttpClient::class, $config, $logger, $cache, $provider, $cacheAdapterFactory);
88103
}
89104

90105
private function getDefaultOptions(Config $config)
@@ -120,12 +135,15 @@ private function createConfig($config)
120135
}
121136

122137
/**
138+
* @param string $clientClass
123139
* @param array $options
124-
* @param LoggerInterface|null $logger
125140
* @param OAuth2Handler $oauthHandler
126-
* @return HttpClient
141+
* @param LoggerInterface|null $logger
142+
* @param CorrelationIdProvider|null $correlationIdProvider
143+
* @return Client
127144
*/
128145
private function createGuzzle6Client(
146+
$clientClass,
129147
array $options,
130148
OAuth2Handler $oauthHandler,
131149
LoggerInterface $logger = null,
@@ -174,7 +192,7 @@ private function createGuzzle6Client(
174192
}), 'ctp_correlation_id');
175193
}
176194

177-
$client = new HttpClient($options);
195+
$client = new $clientClass($options);
178196

179197
return $client;
180198
}

src/Core/Client/HttpClient.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Client;
4+
5+
use Commercetools\Core\Request\ClientRequestInterface;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Promise\PromiseInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class HttpClient extends Client
12+
{
13+
/**
14+
* @param ClientRequestInterface $request
15+
* @param array|null $headers
16+
* @param array $options
17+
* @return mixed|ResponseInterface
18+
* @throws GuzzleException
19+
*/
20+
public function execute(ClientRequestInterface $request, array $headers = null, array $options = [])
21+
{
22+
$httpRequest = $request->httpRequest();
23+
if (is_array($headers)) {
24+
foreach ($headers as $headerName => $headerValues) {
25+
$httpRequest = $httpRequest
26+
->withAddedHeader($headerName, $headerValues)
27+
;
28+
}
29+
}
30+
return parent::send($httpRequest, $options);
31+
}
32+
33+
/**
34+
* @param ClientRequestInterface $request
35+
* @param array|null $headers
36+
* @param array $options
37+
* @return PromiseInterface
38+
*/
39+
public function executeAsync(ClientRequestInterface $request, array $headers = null, array $options = [])
40+
{
41+
$httpRequest = $request->httpRequest();
42+
if (is_array($headers)) {
43+
foreach ($headers as $headerName => $headerValues) {
44+
$httpRequest = $httpRequest
45+
->withAddedHeader($headerName, $headerValues)
46+
;
47+
}
48+
}
49+
return parent::sendAsync($httpRequest, $options);
50+
}
51+
}

tests/integration/ClientFactoryTest.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use Commercetools\Core\Request\Categories\CategoryQueryRequest;
1616
use Commercetools\Core\Request\Categories\CategoryUpdateRequest;
1717
use Commercetools\Core\Request\Categories\Command\CategoryChangeNameAction;
18-
use GuzzleHttp\Client as HttpClient;
18+
use Commercetools\Core\Client\HttpClient;
19+
use GuzzleHttp\Client;
1920
use GuzzleHttp\Psr7\Response as PsrResponse;
2021
use Monolog\Handler\TestHandler;
2122
use Monolog\Logger;
@@ -29,6 +30,16 @@ public function testOf()
2930
$this->assertInstanceOf(ClientFactory::class, ClientFactory::of());
3031
}
3132

33+
public function testCustomGuzzleClient()
34+
{
35+
$this->assertInstanceOf(Client::class, ClientFactory::of()->createCustomClient(Client::class, $this->getClientConfig('manage_project')));
36+
}
37+
38+
public function testCustomClient()
39+
{
40+
$this->assertInstanceOf(HttpClient::class, ClientFactory::of()->createCustomClient(HttpClient::class, $this->getClientConfig('manage_project')));
41+
}
42+
3243
public function testCreateClient()
3344
{
3445
$handler = new TestHandler();
@@ -55,6 +66,58 @@ public function testCreateClient()
5566
$this->assertContains((new UserAgentProvider())->getUserAgent(), $record['message']);
5667
}
5768

69+
public function testExecute()
70+
{
71+
$handler = new TestHandler();
72+
$logger = new Logger('test');
73+
$logger->pushHandler($handler);
74+
75+
$config = $this->getClientConfig('manage_project');
76+
$config->setOAuthClientOptions(['verify' => $this->getVerifySSL(), 'timeout' => '10']);
77+
$config->setClientOptions(['verify' => $this->getVerifySSL(), 'timeout' => '10']);
78+
79+
$client = ClientFactory::of()->createClient($config, $logger);
80+
$this->assertInstanceOf(HttpClient::class, $client);
81+
82+
$request = CategoryQueryRequest::of();
83+
$response = $client->execute($request);
84+
85+
$this->assertInstanceOf(PsrResponse::class, $response);
86+
87+
$categories = $request->mapFromResponse($response);
88+
$this->assertInstanceOf(CategoryCollection::class, $categories);
89+
90+
$record = current($handler->getRecords());
91+
$this->assertStringStartsWith($config->getProject(), $record['context']['X-Correlation-ID'][0]);
92+
$this->assertContains((new UserAgentProvider())->getUserAgent(), $record['message']);
93+
}
94+
95+
public function testExecuteAsync()
96+
{
97+
$handler = new TestHandler();
98+
$logger = new Logger('test');
99+
$logger->pushHandler($handler);
100+
101+
$config = $this->getClientConfig('manage_project');
102+
$config->setOAuthClientOptions(['verify' => $this->getVerifySSL(), 'timeout' => '10']);
103+
$config->setClientOptions(['verify' => $this->getVerifySSL(), 'timeout' => '10']);
104+
105+
$client = ClientFactory::of()->createClient($config, $logger);
106+
$this->assertInstanceOf(HttpClient::class, $client);
107+
108+
$request = CategoryQueryRequest::of();
109+
$response = $client->executeAsync($request)->wait();
110+
111+
$this->assertInstanceOf(PsrResponse::class, $response);
112+
113+
$categories = $request->mapFromResponse($response);
114+
$this->assertInstanceOf(CategoryCollection::class, $categories);
115+
116+
$record = current($handler->getRecords());
117+
$this->assertStringStartsWith($config->getProject(), $record['context']['X-Correlation-ID'][0]);
118+
$this->assertContains((new UserAgentProvider())->getUserAgent(), $record['message']);
119+
}
120+
58121
public function testClientNoException()
59122
{
60123
$handler = new TestHandler();

0 commit comments

Comments
 (0)