-
Notifications
You must be signed in to change notification settings - Fork 56
Add documentation for async client #21
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
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 |
---|---|---|
|
@@ -5,22 +5,37 @@ Httplug is an abstraction for HTTP clients. There are two main use cases: | |
1. Usage in a project | ||
2. Usage in a reusable package | ||
|
||
In both cases, the client provides a `sendRequest` method to send a PSR-7 `RequestInterface` and returns a PSR-7 `ResponseInterface` or throws an exception that implements `Http\Client\Exception`. | ||
In both cases, the `Http\Client\HttpClient` provides a `sendRequest` method to send a PSR-7 `RequestInterface` and returns a PSR-7 `ResponseInterface` | ||
or throws an exception that implements `Http\Client\Exception`. | ||
|
||
There is also the `Http\Client\HttpAsyncClient`, available in [php-http/httplug-async](https://packagist.org/packages/php-http/httplug-async), which provides the `sendAsyncRequest` method to send a request asynchronously and returns a `Http\Client\Promise`. | ||
It can be used later to retrieve a PSR-7 `ResponseInterface` or an exception that implements `Http\Client\Exception`. | ||
|
||
See the [tutorial](tutorial.md) for a concrete example. | ||
|
||
<p class="text-warning"> | ||
Contract for the HttpAsyncClient is experimental until [PSR about Promise is released](https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs). | ||
Once it is out, we will use this interface in the main client and deprecate the separated repository. | ||
</p> | ||
|
||
See the [tutorial](tutorial.md) for a concrete example. | ||
|
||
## Httplug implementations | ||
|
||
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6. In the latter case, they will depend on the required client implementation, so you only need to require the adapter and not the actual client. | ||
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6. | ||
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. "or adapters..." I think you can omit the "they are" part in case of an "either .... or ...." structure. 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 had it like this to make it more clear that implementations can be one or the other things. reading over it quickly could otherwise lead to confusion as we talk about clients and then stuff that wraps clients (the later are a different kind of clients) |
||
In the latter case, they will depend on the required client implementation, so you only need to require the adapter and not the actual client. | ||
|
||
See [packagist](https://packagist.org/providers/php-http/client-implementation) for the full list of implementations. | ||
There are two kind of implementation: | ||
|
||
- [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation), the synchronous implementation that waits for the response / error before returning from the `sendRequest` method. | ||
- [php-http/client-async-implementation](https://packagist.org/providers/php-http/async-client-implementation), the asynchronous implementation that immediately returns a `Http\Client\Promise`, allowing to send several requests in parallel and handling responses later. | ||
|
||
Check links above for the full list of implementations. | ||
|
||
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter. | ||
|
||
## Usage in a project | ||
|
||
When writing an application, you need to require a concrete [client implementation](https://packagist.org/providers/php-http/client-implementation). | ||
When writing an application, you need to require a concrete [implementation](https://packagist.org/providers/php-http/client-implementation). | ||
|
||
See [virtual package](virtual-package.md) for more information on the topic of working with Httplug implementations. | ||
|
||
|
@@ -29,17 +44,20 @@ See [virtual package](virtual-package.md) for more information on the topic of w | |
|
||
In many cases, packages are designed to be reused from the very beginning. For example, API clients are usually used in other packages/applications, not on their own. | ||
|
||
In these cases, they should **not rely on a concrete implementation** (like Guzzle 6), but only require any implementation of Httplug. Httplug uses the concept of virtual packages. Instead of depending on only the interfaces, which would be missing an implementation, or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation`. There is no package with that name, but all clients and adapters implementing Httplug declare that they provide this virtual package. | ||
In these cases, they should **not rely on a concrete implementation** (like Guzzle 6), but only require any implementation of Httplug. | ||
Httplug uses the concept of virtual packages. Instead of depending on only the interfaces, which would be missing an implementation, | ||
or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation` or `php-http/async-client-implementation`. | ||
There is no package with that name, but all clients and adapters implementing Httplug declare that they provide one of this virtual package or both. | ||
|
||
You need to edit the `composer.json` of your package to add the virtual package. For development (installing the package standalone, running tests), add a concrete implementation in the `require-dev` section to make the project installable: | ||
|
||
``` json | ||
... | ||
"require": { | ||
"php-http/client-implementation": "^1.0" | ||
}, | ||
"require-dev": { | ||
"php-http/guzzle6-adapter": "^1.0" | ||
"require": { | ||
"php-http/client-implementation": "^1.0" | ||
}, | ||
"require-dev": { | ||
"php-http/guzzle6-adapter": "^1.0" | ||
}, | ||
... | ||
``` | ||
|
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.
I don't insist on waiting for the PSR before marking the Async client as fully stable. We should see if the group can agree on whether they want that PSR at all and if yes, in what form.
Eventually, we should use the PSR, however, I don't think we should wait years for it. We should also state this in the docs.
Good sign is that people seem to become more active in the FIG: more membership requests, phpdoc and cache moving forward, new proposals are in progress, etc.
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.
i would keep our position like this for now, and if the promise PSR goes nowhere, we can still be more permanent about our implementation.
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.
👍