Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GuzzleHttp with PSR-18 HTTP Clients #1032

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
},
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"paragonie/random_compat": "^1 || ^2 || ^9.99"
"paragonie/random_compat": "^1 || ^2 || ^9.99",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.1",
"psr/http-message": "^2.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"mockery/mockery": "^1.3.5",
"php-parallel-lint/php-parallel-lint": "^1.3.1",
"phpunit/phpunit": "^5.7 || ^6.0 || ^9.5",
Expand Down
82 changes: 63 additions & 19 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace League\OAuth2\Client\Provider;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use InvalidArgumentException;
use League\OAuth2\Client\Grant\AbstractGrant;
Expand All @@ -28,9 +27,11 @@
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
use League\OAuth2\Client\Tool\GuardedPropertyTrait;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use League\OAuth2\Client\Tool\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -103,12 +104,17 @@ abstract class AbstractProvider
protected $grantFactory;

/**
* @var RequestFactory
* @var RequestFactoryInterface
*/
protected $requestFactory;

/**
* @var HttpClientInterface
* @var StreamFactoryInterface
*/
protected $streamFactory;

/**
* @var ClientInterface
*/
protected $httpClient;

Expand Down Expand Up @@ -140,16 +146,17 @@ public function __construct(array $options = [], array $collaborators = [])
$this->setGrantFactory($collaborators['grantFactory']);

if (empty($collaborators['requestFactory'])) {
$collaborators['requestFactory'] = new RequestFactory();
throw new InvalidArgumentException('No request factory set');
}
$this->setRequestFactory($collaborators['requestFactory']);

if (empty($collaborators['httpClient'])) {
$client_options = $this->getAllowedClientOptions($options);
if (empty($collaborators['streamFactory'])) {
throw new InvalidArgumentException('No stream factory set');
}
$this->setStreamFactory($collaborators['streamFactory']);

$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
if (empty($collaborators['httpClient'])) {
throw new InvalidArgumentException('No http client set');
}
$this->setHttpClient($collaborators['httpClient']);

Expand Down Expand Up @@ -205,10 +212,10 @@ public function getGrantFactory()
/**
* Sets the request factory instance.
*
* @param RequestFactory $factory
* @param RequestFactoryInterface $factory
* @return self
*/
public function setRequestFactory(RequestFactory $factory)
public function setRequestFactory(RequestFactoryInterface $factory)
{
$this->requestFactory = $factory;

Expand All @@ -218,20 +225,43 @@ public function setRequestFactory(RequestFactory $factory)
/**
* Returns the request factory instance.
*
* @return RequestFactory
* @return RequestFactoryInterface
*/
public function getRequestFactory()
{
return $this->requestFactory;
}

/**
* Sets the stream factory instance.
*
* @param StreamFactoryInterface $factory
* @return self
*/
public function setStreamFactory(StreamFactoryInterface $factory)
{
$this->streamFactory = $factory;

return $this;
}

/**
* Returns the stream factory instance.
*
* @return StreamFactoryInterface
*/
public function getStreamFactory()
{
return $this->streamFactory;
}

/**
* Sets the HTTP client instance.
*
* @param HttpClientInterface $client
* @param ClientInterface $client
* @return self
*/
public function setHttpClient(HttpClientInterface $client)
public function setHttpClient(ClientInterface $client)
{
$this->httpClient = $client;

Expand All @@ -241,7 +271,7 @@ public function setHttpClient(HttpClientInterface $client)
/**
* Returns the HTTP client instance.
*
* @return HttpClientInterface
* @return ClientInterface
*/
public function getHttpClient()
{
Expand Down Expand Up @@ -687,9 +717,23 @@ protected function createRequest($method, $url, $token, array $options)
];

$options = array_merge_recursive($defaults, $options);
$factory = $this->getRequestFactory();
$requestFactory = $this->getRequestFactory();
$streamFactory = $this->getStreamFactory();

$request = $requestFactory->createRequest($method, $url);
foreach ($options['headers'] as $name => $value) {
$request = $request->withAddedHeader($name, $value);
}

$request = $request->withProtocolVersion($options['version'] ?? '1.1');

if (!empty($options['body'])) {
$request = $request->withBody(
$streamFactory->createStream($options['body'] ?? null)
);
}

return $factory->getRequestWithOptions($method, $url, $options);
return $request;
}

/**
Expand All @@ -703,7 +747,7 @@ protected function createRequest($method, $url, $token, array $options)
*/
public function getResponse(RequestInterface $request)
{
return $this->getHttpClient()->send($request);
return $this->getHttpClient()->sendRequest($request);
}

/**
Expand Down
87 changes: 0 additions & 87 deletions src/Tool/RequestFactory.php

This file was deleted.

12 changes: 9 additions & 3 deletions test/src/Grant/GrantTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace League\OAuth2\Client\Test\Grant;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use League\OAuth2\Client\Token\AccessTokenInterface;
Expand All @@ -19,6 +21,10 @@ protected function getMockProvider()
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
],[
'httpClient' => new Client(),
'requestFactory' => new HttpFactory(),
'streamFactory' => new HttpFactory()
]);
}

Expand Down Expand Up @@ -65,12 +71,12 @@ public function testGetAccessToken($grant, array $params = [])
->shouldReceive('getHeader')
->once()
->with('content-type')
->andReturn('application/json');
->andReturn(['application/json']);

/** @var ClientInterface & MockInterface $client */
$client = Mockery::spy(ClientInterface::class)->makePartial();
$client
->shouldReceive('send')
->shouldReceive('sendRequest')
->once()
->withArgs(function ($request) {
parse_str((string) $request->getBody(), $body);
Expand Down
Loading