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[PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementation and their [factories](message-factory.md).
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[PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementation and their factories.
75
99
76
100
```php
77
101
use Http\Message\UriFactory;
@@ -117,7 +141,7 @@ 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` 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.
Copy file name to clipboardExpand all lines: docs/httplug.md
+27-11Lines changed: 27 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -5,22 +5,35 @@ 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 client 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`.
9
10
10
-
See the [tutorial](tutorial.md) for a concrete example.
11
+
There is also the 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`.
13
+
14
+
Contract for the HttpAsyncClient is still experimental and will be merged into Httplug repository once we considered it stable.
11
15
16
+
See the [tutorial](tutorial.md) for a concrete example.
12
17
13
18
## Httplug implementations
14
19
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.
20
+
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6.
21
+
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
22
17
-
See [packagist](https://packagist.org/providers/php-http/client-implementation) for the full list of implementations.
23
+
There is two kind of implementation:
24
+
25
+
*[php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation), the standard implementation, send requests with a synchronous workflow
26
+
*[php-http/client-async-implementation](https://packagist.org/providers/php-http/client-async-implementation), send requests with an asynchronous workflow by returning promises
27
+
28
+
See [https://packagist.org/providers/php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation) or [https://packagist.org/providers/php-http/client-async-implementation](https://packagist.org/providers/php-http/client-async-implementation) for
29
+
the full list of implementations.
18
30
19
31
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter.
20
32
21
33
## Usage in a project
22
34
23
-
When writing an application, you need to require a concrete [client implementation](https://packagist.org/providers/php-http/client-implementation).
35
+
When writing an application, you need to require a concrete [client implementation](https://packagist.org/providers/php-http/client-implementation) or
36
+
a concrete [async client implementation](https://packagist.org/providers/php-http/client-async-implementation).
24
37
25
38
See [virtual package](virtual-package.md) for more information on the topic of working with Httplug implementations.
26
39
@@ -29,17 +42,20 @@ See [virtual package](virtual-package.md) for more information on the topic of w
29
42
30
43
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
44
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.
45
+
In these cases, they should **not rely on a concrete implementation** (like Guzzle 6), but only require any implementation of Httplug.
46
+
Httplug uses the concept of virtual packages. Instead of depending on only the interfaces, which would be missing an implementation,
47
+
or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation` or `php-http/async-client-implementation`.
48
+
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
49
34
50
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...';
96
+
97
+
throw new HttpException('My error message', $request, $response);
98
+
}, function (Exception $e) {
99
+
// onRejected callback
100
+
echo 'An error happens, but it\'s ok...';
101
+
102
+
return $exception->getResponse();
103
+
});
104
+
```
105
+
106
+
* Get the state of the promise with `$promise->getState()` will return of one `Promise::PENDING`, `Promise::FULFILLED` or `Promise::REJECTED`
107
+
* Get the response of the promise if it's in `FULFILLED` state with `$promise->getResponse()` call
108
+
* Get the error of the promise if it's in `REJECTED` state with `$promise->getRequest()` call
109
+
* wait for the callback to be fulfilled or rejected with the `$promise->wait()` call. The `wait` will return nothing, it will simply call one the callback
110
+
pass to the `then` method depending on the result of the call. It the promise has already been fulfilled or rejected it will do nothing.
111
+
112
+
Here is a full example of a classic usage when using the `sendAsyncRequest` method:
113
+
114
+
```php
115
+
$httpAsyncClient = new HttpAsyncClientImplementation();
0 commit comments