-
Notifications
You must be signed in to change notification settings - Fork 765
feat: PSR18 #885
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
feat: PSR18 #885
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,6 @@ | |
|
||
namespace League\OAuth2\Client\Provider; | ||
|
||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\ClientInterface as HttpClientInterface; | ||
use GuzzleHttp\Exception\BadResponseException; | ||
use League\OAuth2\Client\Grant\AbstractGrant; | ||
use League\OAuth2\Client\Grant\GrantFactory; | ||
use League\OAuth2\Client\OptionProvider\OptionProviderInterface; | ||
|
@@ -28,6 +25,8 @@ | |
use League\OAuth2\Client\Tool\GuardedPropertyTrait; | ||
use League\OAuth2\Client\Tool\QueryBuilderTrait; | ||
use League\OAuth2\Client\Tool\RequestFactory; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use UnexpectedValueException; | ||
|
@@ -89,7 +88,7 @@ abstract class AbstractProvider | |
protected $requestFactory; | ||
|
||
/** | ||
* @var HttpClientInterface | ||
* @var ClientInterface | ||
*/ | ||
protected $httpClient; | ||
|
||
|
@@ -126,11 +125,7 @@ public function __construct(array $options = [], array $collaborators = []) | |
$this->setRequestFactory($collaborators['requestFactory']); | ||
|
||
if (empty($collaborators['httpClient'])) { | ||
$client_options = $this->getAllowedClientOptions($options); | ||
|
||
$collaborators['httpClient'] = new HttpClient( | ||
array_intersect_key($options, array_flip($client_options)) | ||
); | ||
throw new \LogicException('Must specify client'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous workflow no longer works, we cannot create a default client since we don't know what's available. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One could check if the guzzle client class exists and instantiate if it does. That could be a smoother transition but it will be less generic and impartial. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, was thinking about extracting that logic into a factory, something like FallbackHttpClient::discover(); which could then do some heuristic to find some popular clients. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are 3 ways.
To "find popular clients" is the job of the discovery package. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so we can test for any of those and try to construct the client. |
||
} | ||
$this->setHttpClient($collaborators['httpClient']); | ||
|
||
|
@@ -209,10 +204,10 @@ public function getRequestFactory() | |
/** | ||
* 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; | ||
|
||
|
@@ -222,7 +217,7 @@ public function setHttpClient(HttpClientInterface $client) | |
/** | ||
* Returns the HTTP client instance. | ||
* | ||
* @return HttpClientInterface | ||
* @return ClientInterface | ||
*/ | ||
public function getHttpClient() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method really needed? But it is maybe out of scope for this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'd keep it for now, but since this is a BC break, this will have to be 3.x anyway. |
||
{ | ||
|
@@ -600,12 +595,14 @@ protected function createRequest($method, $url, $token, array $options) | |
* WARNING: This method does not attempt to catch exceptions caused by HTTP | ||
* errors! It is recommended to wrap this method in a try/catch block. | ||
* | ||
* @param RequestInterface $request | ||
* @param RequestInterface $request | ||
* | ||
* @return ResponseInterface | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
*/ | ||
public function getResponse(RequestInterface $request) | ||
{ | ||
return $this->getHttpClient()->send($request); | ||
return $this->getHttpClient()->sendRequest($request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add logic here to follow redirects? |
||
} | ||
|
||
/** | ||
|
@@ -619,8 +616,8 @@ public function getParsedResponse(RequestInterface $request) | |
{ | ||
try { | ||
$response = $this->getResponse($request); | ||
} catch (BadResponseException $e) { | ||
$response = $e->getResponse(); | ||
} catch (ClientExceptionInterface $e) { | ||
throw $e; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no equivalent here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, But that is a good thing =) The ClientException will only be thrown when it is impossible to generate a response. Ie network errors or the Request is invalid (missing method) etc. |
||
} | ||
|
||
$parsed = $this->parseResponse($response); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
|
||
namespace League\OAuth2\Client\Tool; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
/** | ||
* Used to produce PSR-7 Request instances. | ||
|
@@ -23,6 +25,16 @@ | |
*/ | ||
class RequestFactory | ||
{ | ||
/** | ||
* @var RequestFactoryInterface | ||
*/ | ||
protected $requestFactory; | ||
|
||
/** | ||
* @var StreamFactoryInterface | ||
*/ | ||
protected $streamFactory; | ||
|
||
Comment on lines
+28
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are these properties injected? I think I'm missing a constructor here. |
||
/** | ||
* Creates a PSR-7 Request instance. | ||
* | ||
|
@@ -32,7 +44,7 @@ class RequestFactory | |
* @param string|resource|StreamInterface $body Message body. | ||
* @param string $version HTTP protocol version. | ||
* | ||
* @return Request | ||
* @return RequestInterface | ||
*/ | ||
public function getRequest( | ||
$method, | ||
|
@@ -41,7 +53,15 @@ public function getRequest( | |
$body = null, | ||
$version = '1.1' | ||
) { | ||
return new Request($method, $uri, $headers, $body, $version); | ||
$request = $this->requestFactory->createRequest($method, $uri) | ||
->withProtocolVersion($version) | ||
->withBody($this->streamFactory->createStream($body)); | ||
|
||
foreach ($headers as $name => $value) { | ||
$request = $request->withHeader($name, $value); | ||
} | ||
|
||
return $request; | ||
} | ||
|
||
/** | ||
|
@@ -70,7 +90,7 @@ protected function parseOptions(array $options) | |
* @param null|string $uri | ||
* @param array $options | ||
* | ||
* @return Request | ||
* @return RequestInterface | ||
*/ | ||
public function getRequestWithOptions($method, $uri, array $options = []) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For tests, uses RC because this package doesn't declare providing the PSR17 factories until 2.x.