Skip to content

Commit

Permalink
Merge pull request #4 from softonic/feature/add-client-builder
Browse files Browse the repository at this point in the history
Feature/add client builder
  • Loading branch information
rccrdpccl authored Aug 21, 2017
2 parents 9e9fead + bc9f868 commit cb71ba6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 21 deletions.
24 changes: 4 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,14 @@ $config = ['grant_type' => 'client_credentials', 'scope' => 'myscope'];

// Any implementation of PSR-6 Cache will do
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$cacheHandler = new \Softonic\OAuth2\Guzzle\Middleware\AccessTokenCacheHandler($cache);


$stack = new \GuzzleHttp\HandlerStack();
$stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());

$addAuthorizationHeader = new \Softonic\OAuth2\Guzzle\Middleware\AddAuthorizationHeader(
$client = \Softonic\OAuth2\Guzzle\Middleware\ClientBuilder::build(
$provider,
$config,
$cacheHandler
$cache,
['base_uri' => 'https://foo.bar/']
);

$stack->push(\GuzzleHttp\Middleware::mapRequest($addAuthorizationHeader));

$retryOnAuthorizationError = new \Softonic\OAuth2\Guzzle\Middleware\RetryOnAuthorizationError(
$provider,
$cacheHandler
);

$stack->push(\GuzzleHttp\Middleware::retry($retryOnAuthorizationError));

$client = new \GuzzleHttp\Client(['handler' => $stack]);

$response = $client->request('POST', 'https://foo.bar/endpoint');
$response = $client->request('POST', 'qux);


```
Expand Down
86 changes: 86 additions & 0 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Softonic\OAuth2\Guzzle\Middleware;

use GuzzleHttp\HandlerStack as Stack;
use League\OAuth2\Client\Provider\AbstractProvider as OAuth2Provider;
use Psr\Cache\CacheItemPoolInterface as Cache;

class ClientBuilder
{
public static function build(
OAuth2Provider $oauthProvider,
array $tokenOptions,
Cache $cache,
array $guzzleOptions = null
): \GuzzleHttp\Client {
$cacheHandler = new AccessTokenCacheHandler($cache);

$stack = static::getStack();

$stack = static::addHeaderMiddlewareToStack(
$stack,
$oauthProvider,
$tokenOptions,
$cacheHandler
);
$stack = static::addRetryMiddlewareToStack(
$stack,
$oauthProvider,
$tokenOptions,
$cacheHandler
);

$defaultOptions = [
'handler' => $stack,
];
$guzzleOptions = static::mergeOptions($defaultOptions, $guzzleOptions);

return new \GuzzleHttp\Client($guzzleOptions);
}

private static function getStack(): Stack
{
$stack = new Stack();
$stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());
return $stack;
}

private static function addHeaderMiddlewareToStack(
Stack $stack,
OAuth2Provider $oauthProvider,
array $tokenOptions,
AccessTokenCacheHandler $cacheHandler
): Stack {
$addAuthorizationHeader = new AddAuthorizationHeader(
$oauthProvider,
$tokenOptions,
$cacheHandler
);

$stack->push(\GuzzleHttp\Middleware::mapRequest($addAuthorizationHeader));
return $stack;
}

private static function addRetryMiddlewareToStack(
Stack $stack,
OAuth2Provider $oauthProvider,
array $tokenOptions,
AccessTokenCacheHandler $cacheHandler
): Stack {
$retryOnAuthorizationError = new RetryOnAuthorizationError(
$oauthProvider,
$tokenOptions,
$cacheHandler
);

$stack->push(\GuzzleHttp\Middleware::retry($retryOnAuthorizationError));
return $stack;
}

private static function mergeOptions(array $defaultOptions, array $options = null): array
{
$options = $options ?? [];
return array_merge($options, $defaultOptions);
}
}
2 changes: 1 addition & 1 deletion tests/AccessTokenCacheHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testGetCacheKeyIsDifferentBetweenSameProviderButDifferentOptions

$optionsA = [
'grant_type' => 'client_credentials',
'scope' => 'myscopeA'
'scope' => 'myscopeA',
];
$optionsB = [
'grant_type' => 'client_credentials',
Expand Down
40 changes: 40 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Softonic\OAuth2\Guzzle\Middleware\Test;

use PHPUnit\Framework\TestCase;
use Softonic\OAuth2\Guzzle\Middleware\ClientBuilder;

class ClientBuilderTest extends TestCase
{
public function testClientBuilderWithoutGuzzleOptions()
{
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class);
$mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class);
$mockTokenOptions = [];

$client = ClientBuilder::build($mockProvider, $mockTokenOptions, $mockCache);
$this->assertInstanceOf(\GuzzleHttp\ClientInterface::class, $client);
}

public function testClientBuilderWithGuzzleOptions()
{
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class);
$mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class);
$mockTokenOptions = [];
$baseUri = 'https://foo.bar/';
$mockGuzzleOptions = [
'base_uri' => $baseUri,
];
$client = ClientBuilder::build(
$mockProvider,
$mockTokenOptions,
$mockCache,
$mockGuzzleOptions
);
$this->assertInstanceOf(\GuzzleHttp\ClientInterface::class, $client);

$guzzleConfig = $client->getConfig();
$this->assertEquals($baseUri, (string) $guzzleConfig['base_uri']);
}
}

0 comments on commit cb71ba6

Please sign in to comment.