From 37babaf1d7ae632b468ca3d729352f8c57175e28 Mon Sep 17 00:00:00 2001 From: Riccardo Piccoli Date: Mon, 21 Aug 2017 09:25:28 +0200 Subject: [PATCH 1/2] add trailing comma --- tests/AccessTokenCacheHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AccessTokenCacheHandlerTest.php b/tests/AccessTokenCacheHandlerTest.php index c85fbc4..a515e5e 100644 --- a/tests/AccessTokenCacheHandlerTest.php +++ b/tests/AccessTokenCacheHandlerTest.php @@ -50,7 +50,7 @@ public function testGetCacheKeyIsDifferentBetweenSameProviderButDifferentOptions $optionsA = [ 'grant_type' => 'client_credentials', - 'scope' => 'myscopeA' + 'scope' => 'myscopeA', ]; $optionsB = [ 'grant_type' => 'client_credentials', From bc9f8685c1761532a1d0bd789cf737eccdb13e2c Mon Sep 17 00:00:00 2001 From: Riccardo Piccoli Date: Mon, 21 Aug 2017 09:31:44 +0200 Subject: [PATCH 2/2] add client builder --- README.md | 24 ++--------- src/ClientBuilder.php | 86 +++++++++++++++++++++++++++++++++++++ tests/ClientBuilderTest.php | 40 +++++++++++++++++ 3 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 src/ClientBuilder.php create mode 100644 tests/ClientBuilderTest.php diff --git a/README.md b/README.md index 7aa0888..87a2ade 100644 --- a/README.md +++ b/README.md @@ -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); ``` diff --git a/src/ClientBuilder.php b/src/ClientBuilder.php new file mode 100644 index 0000000..0473f52 --- /dev/null +++ b/src/ClientBuilder.php @@ -0,0 +1,86 @@ + $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); + } +} diff --git a/tests/ClientBuilderTest.php b/tests/ClientBuilderTest.php new file mode 100644 index 0000000..58e8d80 --- /dev/null +++ b/tests/ClientBuilderTest.php @@ -0,0 +1,40 @@ +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']); + } +}