You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementations and their [factories](message-factory.md).
72
+
This type of discovery finds a [message factory](message-factory.md) for a [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementation.
49
73
50
74
```php
51
75
use Http\Message\MessageFactory;
@@ -71,7 +95,7 @@ class MyClass
71
95
72
96
## PSR-7 URI Factory Discovery
73
97
74
-
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementations and their factories.
98
+
This type of discovery finds a uri factory for a [PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementation.
75
99
76
100
```php
77
101
use Http\Message\UriFactory;
@@ -117,7 +141,8 @@ Classes registered manually are put on top of the list.
117
141
118
142
### Writing your own discovery
119
143
120
-
Each discovery service is based on the `ClassDiscovery` and has to specify a `cache` field and a `class` field to specify classes for the corresponding service. The fields need to be redeclared in each discovery class. If `ClassDiscovery` would declare them, they would be shared between the discovery classes which would make no sense.
144
+
Each discovery service is based on the `ClassDiscovery` and has to specify a `cache` property and a `class` property to specify classes for the corresponding service.
145
+
Since they are static, this properties need to be redeclared in each discovery class. If `ClassDiscovery` would declare them, they would be shared between the discovery classes which would make no sense.
Copy file name to clipboardExpand all lines: docs/httplug.md
+29-11Lines changed: 29 additions & 11 deletions
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:
5
5
1. Usage in a project
6
6
2. Usage in a reusable package
7
7
8
-
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`.
8
+
In both cases, the `Http\Client\HttpClient` provides a `sendRequest` method to send a PSR-7 `RequestInterface` and returns a PSR-7 `ResponseInterface`
9
+
or throws an exception that implements `Http\Client\Exception`.
10
+
11
+
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`.
12
+
It can be used later to retrieve a PSR-7 `ResponseInterface` or an exception that implements `Http\Client\Exception`.
9
13
10
-
See the [tutorial](tutorial.md) for a concrete example.
11
14
15
+
<pclass="text-warning">
16
+
Contract for the HttpAsyncClient is experimental until [PSR about Promise is released](https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs).
17
+
Once it is out, we will use this interface in the main client and deprecate the separated repository.
18
+
</p>
19
+
20
+
See the [tutorial](tutorial.md) for a concrete example.
12
21
13
22
## Httplug implementations
14
23
15
-
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.
24
+
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6.
25
+
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.
16
26
17
-
See [packagist](https://packagist.org/providers/php-http/client-implementation) for the full list of implementations.
27
+
There are two kind of implementation:
28
+
29
+
-[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.
30
+
-[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.
31
+
32
+
Check links above for the full list of implementations.
18
33
19
34
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter.
20
35
21
36
## Usage in a project
22
37
23
-
When writing an application, you need to require a concrete [client implementation](https://packagist.org/providers/php-http/client-implementation).
38
+
When writing an application, you need to require a concrete [implementation](https://packagist.org/providers/php-http/client-implementation).
24
39
25
40
See [virtual package](virtual-package.md) for more information on the topic of working with Httplug implementations.
26
41
@@ -29,17 +44,20 @@ See [virtual package](virtual-package.md) for more information on the topic of w
29
44
30
45
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.
31
46
32
-
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.
47
+
In these cases, they should **not rely on a concrete implementation** (like Guzzle 6), but only require any implementation of Httplug.
48
+
Httplug uses the concept of virtual packages. Instead of depending on only the interfaces, which would be missing an implementation,
49
+
or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation` or `php-http/async-client-implementation`.
50
+
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.
33
51
34
52
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:
echo 'The response is available, but it\'s not ok...';
98
+
99
+
throw new HttpException('My error message', $request, $response);
100
+
}, function (Exception $e) {
101
+
// onRejected callback
102
+
echo 'An error happens, but it\'s ok...';
103
+
104
+
return $exception->getResponse();
105
+
});
106
+
```
107
+
108
+
Calling the `wait` method on the promise will wait for the response or exception to be available and invoke callback provided in the `then` method.
109
+
110
+
### Using the promise directly
38
111
39
-
## Doing parallel requests
112
+
If you don't want to use the callback system, you can also get the state of the promise with `$promise->getState()` will return of one `Promise::PENDING`, `Promise::FULFILLED` or `Promise::REJECTED`
40
113
41
-
TODO explain sendRequests and how to work with BatchResult and BatchException
114
+
Then you can get the response of the promise if it's in `FULFILLED` state with `$promise->getResponse()` call or
115
+
get the error of the promise if it's in `REJECTED` state with `$promise->getRequest()` call
116
+
117
+
### Example
118
+
119
+
Here is a full example of a classic usage when using the `sendAsyncRequest` method:
0 commit comments