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

Commit 5746c4a

Browse files
author
Jens Schulze
committed
feat(OAuthManager): add configuration options for internal HTTP client
Closes #395
1 parent 53aaa0e commit 5746c4a

File tree

8 files changed

+91
-3
lines changed

8 files changed

+91
-3
lines changed

src/Core/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function getHttpClient($options = [])
203203
{
204204
if (is_null($this->httpClient)) {
205205
$clientOptions = $this->config->getClientOptions();
206-
if (count($clientOptions)) {
206+
if (count($clientOptions) > 0) {
207207
$options = array_merge($clientOptions, $options);
208208
}
209209
$client = parent::getHttpClient($options);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @author @jenschude <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Client\Adapter;
7+
8+
interface ConfigAware
9+
{
10+
/**
11+
* @param string $option
12+
* @return mixed
13+
*/
14+
public function getConfig($option);
15+
}

src/Core/Client/Adapter/Guzzle5Adapter.php

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

24-
class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
24+
class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware
2525
{
2626
const DEFAULT_CONCURRENCY = 25;
2727

@@ -283,4 +283,12 @@ public static function getAdapterInfo()
283283
{
284284
return 'GuzzleHttp/' . Client::VERSION;
285285
}
286+
287+
/**
288+
* @inheritdoc
289+
*/
290+
public function getConfig($option)
291+
{
292+
return $this->client->getDefaultOption($option);
293+
}
286294
}

src/Core/Client/Adapter/Guzzle6Adapter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Commercetools\Core\Error\ApiException;
2020
use Psr\Log\LogLevel;
2121

22-
class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
22+
class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware
2323
{
2424
const DEFAULT_CONCURRENCY = 25;
2525
/**
@@ -221,4 +221,12 @@ public static function getAdapterInfo()
221221
{
222222
return 'GuzzleHttp/' . Client::VERSION;
223223
}
224+
225+
/**
226+
* @inheritdoc
227+
*/
228+
public function getConfig($option)
229+
{
230+
return $this->client->getConfig($option);
231+
}
224232
}

src/Core/Client/OAuth/Manager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ protected function getBearerToken(array $data)
251251
public function getHttpClient($options = [])
252252
{
253253
if (is_null($this->httpClient)) {
254+
$clientOptions = $this->config->getOAuthClientOptions();
255+
if (count($clientOptions) > 0) {
256+
$options = array_merge($clientOptions, $options);
257+
}
254258
$client = parent::getHttpClient($options);
255259
if ($this->getConfig()->getCorrelationIdProvider() instanceof CorrelationIdProvider
256260
&& $client instanceof CorrelationIdAware

src/Core/Config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ class Config implements ContextAwareInterface
174174

175175
protected $clientOptions = [];
176176

177+
protected $oauthClientOptions = [];
178+
177179
public function __construct()
178180
{
179181
$this->enableCorrelationId = Uuid::active();
@@ -682,6 +684,24 @@ public function setClientOptions(array $clientOptions)
682684
return $this;
683685
}
684686

687+
/**
688+
* @return array
689+
*/
690+
public function getOAuthClientOptions()
691+
{
692+
return $this->oauthClientOptions;
693+
}
694+
695+
/**
696+
* @param array $clientOptions
697+
* @return Config
698+
*/
699+
public function setOAuthClientOptions(array $clientOptions)
700+
{
701+
$this->oauthClientOptions = $clientOptions;
702+
return $this;
703+
}
704+
685705
/**
686706
* @return string
687707
*/

tests/unit/Client/OAuth/ManagerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cache\Adapter\PHPArray\ArrayCachePool;
1010
use Cache\Adapter\Void\VoidCachePool;
1111
use Commercetools\Core\Cache\CacheAdapterFactory;
12+
use Commercetools\Core\Client\Adapter\ConfigAware;
1213
use GuzzleHttp\Client as HttpClient;
1314
use GuzzleHttp\Handler\MockHandler;
1415
use GuzzleHttp\HandlerStack;
@@ -284,4 +285,19 @@ public function testOAuthUrl()
284285

285286
$this->assertSame($this->getConfig()->getOauthUrl(), $output);
286287
}
288+
289+
public function testSetClientOptions()
290+
{
291+
$config = Config::of()->setClientId('')->setClientSecret('')->setProject('');
292+
$manager = new Manager($config);
293+
294+
$this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient());
295+
$this->assertTrue($manager->getHttpClient()->getConfig('verify'));
296+
297+
$config = Config::of()->setClientId('')->setClientSecret('')->setProject('')->setOAuthClientOptions(['verify' => false]);
298+
$manager = new Manager($config);
299+
300+
$this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient());
301+
$this->assertFalse($manager->getHttpClient()->getConfig('verify'));
302+
}
287303
}

tests/unit/ClientTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Commercetools\Core;
88

9+
use Commercetools\Core\Client\Adapter\AdapterFactory;
910
use Commercetools\Core\Client\Adapter\AdapterOptionInterface;
11+
use Commercetools\Core\Client\Adapter\ConfigAware;
1012
use Commercetools\Core\Client\OAuth\Manager;
1113
use Commercetools\Core\Error\BadGatewayException;
1214
use Commercetools\Core\Error\ConcurrentModificationException;
@@ -805,4 +807,19 @@ public function testHtmlBody()
805807
$this->assertSame('Length Required', $response->getMessage());
806808

807809
}
810+
811+
public function testSetClientOptions()
812+
{
813+
$client = Client::ofConfig(
814+
Config::of()->setClientId('')->setClientSecret('')->setProject('')
815+
);
816+
$this->assertInstanceOf(ConfigAware::class, $client->getHttpClient());
817+
$this->assertTrue($client->getHttpClient()->getConfig('verify'));
818+
819+
$client = Client::ofConfig(
820+
Config::of()->setClientId('')->setClientSecret('')->setProject('')->setClientOptions(['verify' => false])
821+
);
822+
$this->assertInstanceOf(ConfigAware::class, $client->getHttpClient());
823+
$this->assertFalse($client->getHttpClient()->getConfig('verify'));
824+
}
808825
}

0 commit comments

Comments
 (0)