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

Commit

Permalink
feat(OAuthManager): add configuration options for internal HTTP client
Browse files Browse the repository at this point in the history
Closes #395
  • Loading branch information
Jens Schulze committed Mar 29, 2018
1 parent 53aaa0e commit 5746c4a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Core/Client.php
Expand Up @@ -203,7 +203,7 @@ public function getHttpClient($options = [])
{
if (is_null($this->httpClient)) {
$clientOptions = $this->config->getClientOptions();
if (count($clientOptions)) {
if (count($clientOptions) > 0) {
$options = array_merge($clientOptions, $options);
}
$client = parent::getHttpClient($options);
Expand Down
15 changes: 15 additions & 0 deletions src/Core/Client/Adapter/ConfigAware.php
@@ -0,0 +1,15 @@
<?php
/**
* @author @jenschude <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Client\Adapter;

interface ConfigAware
{
/**
* @param string $option
* @return mixed
*/
public function getConfig($option);
}
10 changes: 9 additions & 1 deletion src/Core/Client/Adapter/Guzzle5Adapter.php
Expand Up @@ -21,7 +21,7 @@
use Commercetools\Core\Error\ApiException;
use Psr\Log\LogLevel;

class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware
{
const DEFAULT_CONCURRENCY = 25;

Expand Down Expand Up @@ -283,4 +283,12 @@ public static function getAdapterInfo()
{
return 'GuzzleHttp/' . Client::VERSION;
}

/**
* @inheritdoc
*/
public function getConfig($option)
{
return $this->client->getDefaultOption($option);
}
}
10 changes: 9 additions & 1 deletion src/Core/Client/Adapter/Guzzle6Adapter.php
Expand Up @@ -19,7 +19,7 @@
use Commercetools\Core\Error\ApiException;
use Psr\Log\LogLevel;

class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware
class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware
{
const DEFAULT_CONCURRENCY = 25;
/**
Expand Down Expand Up @@ -221,4 +221,12 @@ public static function getAdapterInfo()
{
return 'GuzzleHttp/' . Client::VERSION;
}

/**
* @inheritdoc
*/
public function getConfig($option)
{
return $this->client->getConfig($option);
}
}
4 changes: 4 additions & 0 deletions src/Core/Client/OAuth/Manager.php
Expand Up @@ -251,6 +251,10 @@ protected function getBearerToken(array $data)
public function getHttpClient($options = [])
{
if (is_null($this->httpClient)) {
$clientOptions = $this->config->getOAuthClientOptions();
if (count($clientOptions) > 0) {
$options = array_merge($clientOptions, $options);
}
$client = parent::getHttpClient($options);
if ($this->getConfig()->getCorrelationIdProvider() instanceof CorrelationIdProvider
&& $client instanceof CorrelationIdAware
Expand Down
20 changes: 20 additions & 0 deletions src/Core/Config.php
Expand Up @@ -174,6 +174,8 @@ class Config implements ContextAwareInterface

protected $clientOptions = [];

protected $oauthClientOptions = [];

public function __construct()
{
$this->enableCorrelationId = Uuid::active();
Expand Down Expand Up @@ -682,6 +684,24 @@ public function setClientOptions(array $clientOptions)
return $this;
}

/**
* @return array
*/
public function getOAuthClientOptions()
{
return $this->oauthClientOptions;
}

/**
* @param array $clientOptions
* @return Config
*/
public function setOAuthClientOptions(array $clientOptions)
{
$this->oauthClientOptions = $clientOptions;
return $this;
}

/**
* @return string
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Client/OAuth/ManagerTest.php
Expand Up @@ -9,6 +9,7 @@
use Cache\Adapter\PHPArray\ArrayCachePool;
use Cache\Adapter\Void\VoidCachePool;
use Commercetools\Core\Cache\CacheAdapterFactory;
use Commercetools\Core\Client\Adapter\ConfigAware;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -284,4 +285,19 @@ public function testOAuthUrl()

$this->assertSame($this->getConfig()->getOauthUrl(), $output);
}

public function testSetClientOptions()
{
$config = Config::of()->setClientId('')->setClientSecret('')->setProject('');
$manager = new Manager($config);

$this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient());
$this->assertTrue($manager->getHttpClient()->getConfig('verify'));

$config = Config::of()->setClientId('')->setClientSecret('')->setProject('')->setOAuthClientOptions(['verify' => false]);
$manager = new Manager($config);

$this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient());
$this->assertFalse($manager->getHttpClient()->getConfig('verify'));
}
}
17 changes: 17 additions & 0 deletions tests/unit/ClientTest.php
Expand Up @@ -6,7 +6,9 @@

namespace Commercetools\Core;

use Commercetools\Core\Client\Adapter\AdapterFactory;
use Commercetools\Core\Client\Adapter\AdapterOptionInterface;
use Commercetools\Core\Client\Adapter\ConfigAware;
use Commercetools\Core\Client\OAuth\Manager;
use Commercetools\Core\Error\BadGatewayException;
use Commercetools\Core\Error\ConcurrentModificationException;
Expand Down Expand Up @@ -805,4 +807,19 @@ public function testHtmlBody()
$this->assertSame('Length Required', $response->getMessage());

}

public function testSetClientOptions()
{
$client = Client::ofConfig(
Config::of()->setClientId('')->setClientSecret('')->setProject('')
);
$this->assertInstanceOf(ConfigAware::class, $client->getHttpClient());
$this->assertTrue($client->getHttpClient()->getConfig('verify'));

$client = Client::ofConfig(
Config::of()->setClientId('')->setClientSecret('')->setProject('')->setClientOptions(['verify' => false])
);
$this->assertInstanceOf(ConfigAware::class, $client->getHttpClient());
$this->assertFalse($client->getHttpClient()->getConfig('verify'));
}
}

0 comments on commit 5746c4a

Please sign in to comment.