Skip to content

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

Closed
wants to merge 1 commit into from
Closed

feat: PSR18 #885

wants to merge 1 commit into from

Conversation

dkarlovi
Copy link

@dkarlovi dkarlovi commented May 12, 2021

Closes #685. See #538.

Copy link
Author

@dkarlovi dkarlovi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nyholm would love to hear your thoughts.

"paragonie/random_compat": "^1 || ^2 || ^9.99"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^2.0@RC",
Copy link
Author

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.

$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
throw new \LogicException('Must specify client');
Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 3 ways.

  1. No fallback
  2. Just Guzzle.
  3. https://github.com/php-http/discovery

To "find popular clients" is the job of the discovery package.

Copy link
Author

Choose a reason for hiding this comment

The 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.

} catch (BadResponseException $e) {
$response = $e->getResponse();
} catch (ClientExceptionInterface $e) {
throw $e;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no equivalent here.

Copy link
Member

Choose a reason for hiding this comment

The 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.

@dkarlovi dkarlovi marked this pull request as draft May 12, 2021 19:55
Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
throw new \LogicException('Must specify client');
Copy link
Member

Choose a reason for hiding this comment

The 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.

@@ -222,7 +217,7 @@ public function setHttpClient(HttpClientInterface $client)
/**
* Returns the HTTP client instance.
*
* @return HttpClientInterface
* @return ClientInterface
*/
public function getHttpClient()
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

} catch (BadResponseException $e) {
$response = $e->getResponse();
} catch (ClientExceptionInterface $e) {
throw $e;
Copy link
Member

Choose a reason for hiding this comment

The 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.

*/
public function getResponse(RequestInterface $request)
{
return $this->getHttpClient()->send($request);
return $this->getHttpClient()->sendRequest($request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add logic here to follow redirects?

Comment on lines +28 to +37
/**
* @var RequestFactoryInterface
*/
protected $requestFactory;

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support using different HTTP Clients through PSR-17 and PSR-18
3 participants