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 a HTTP Client implementation.
22
+
This type of discovery finds an HTTP Client implementation.
23
23
24
24
```php
25
25
use Http\Client\HttpClient;
@@ -33,7 +33,7 @@ class MyClass
33
33
protected $httpClient;
34
34
35
35
/**
36
-
* @param HttpClient|null $httpClient to do HTTP requests.
36
+
* @param HttpClient|null $httpClient Client to do HTTP requests, if not set, autodiscovery will be used to find a HTTP client.
37
37
*/
38
38
public function __construct(HttpClient $httpClient = null)
39
39
{
@@ -58,7 +58,7 @@ class MyClass
58
58
protected $httpAsyncClient;
59
59
60
60
/**
61
-
* @param HttpAsyncClient|null $httpAsyncClient to do HTTP requests.
61
+
* @param HttpAsyncClient|null $httpAsyncClient Client to do HTTP requests, if not set, autodiscovery will be used to find an asynchronous client.
62
62
*/
63
63
public function __construct(HttpAsyncClient $httpAsyncClient = null)
64
64
{
@@ -69,7 +69,7 @@ class MyClass
69
69
70
70
## PSR-7 Message Factory Discovery
71
71
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).
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.
73
73
74
74
```php
75
75
use Http\Message\MessageFactory;
@@ -95,7 +95,7 @@ class MyClass
95
95
96
96
## PSR-7 URI Factory Discovery
97
97
98
-
This type of discovery finds a [PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementation 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.
99
99
100
100
```php
101
101
use Http\Message\UriFactory;
@@ -141,7 +141,8 @@ Classes registered manually are put on top of the list.
141
141
142
142
### Writing your own discovery
143
143
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.
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
+7-9Lines changed: 7 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,10 @@ 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`
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
9
or throws an exception that implements `Http\Client\Exception`.
10
10
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`.
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
12
It can be used later to retrieve a PSR-7 `ResponseInterface` or an exception that implements `Http\Client\Exception`.
13
13
14
14
@@ -24,20 +24,18 @@ See the [tutorial](tutorial.md) for a concrete example.
24
24
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6.
25
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.
26
26
27
-
There is two kind of implementation:
27
+
There are two kind of implementation:
28
28
29
-
*[php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation), the standard implementation, send requests with a synchronous workflow
30
-
*[php-http/client-async-implementation](https://packagist.org/providers/php-http/client-async-implementation), send requests with an asynchronous workflow by returning promises
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
31
32
-
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
33
-
the full list of implementations.
32
+
Check links above for the full list of implementations.
34
33
35
34
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter.
36
35
37
36
## Usage in a project
38
37
39
-
When writing an application, you need to require a concrete [client implementation](https://packagist.org/providers/php-http/client-implementation) or
40
-
a concrete [async client implementation](https://packagist.org/providers/php-http/client-async-implementation).
38
+
When writing an application, you need to require a concrete [implementation](https://packagist.org/providers/php-http/client-implementation).
41
39
42
40
See [virtual package](virtual-package.md) for more information on the topic of working with Httplug implementations.
echo 'The response is available, but it\'s not ok...';
103
98
104
-
return $exception->getResponse();
105
-
});
106
-
```
107
-
108
-
* Get the state of the promise with `$promise->getState()` will return of one `Promise::PENDING`, `Promise::FULFILLED` or `Promise::REJECTED`
109
-
* Get the response of the promise if it's in `FULFILLED` state with `$promise->getResponse()` call
110
-
* Get the error of the promise if it's in `REJECTED` state with `$promise->getRequest()` call
111
-
* 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
112
-
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.
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
111
+
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`
113
+
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
113
118
114
119
Here is a full example of a classic usage when using the `sendAsyncRequest` method:
0 commit comments