Skip to content

Commit 1ff4919

Browse files
committed
Fix typo and rewrite tutorial part
1 parent 5c714c7 commit 1ff4919

File tree

3 files changed

+76
-72
lines changed

3 files changed

+76
-72
lines changed

docs/discovery.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ composer require "php-http/discovery"
1919

2020
## HTTP Client Discovery
2121

22-
This type of discovery finds a HTTP Client implementation.
22+
This type of discovery finds an HTTP Client implementation.
2323

2424
``` php
2525
use Http\Client\HttpClient;
@@ -33,7 +33,7 @@ class MyClass
3333
protected $httpClient;
3434

3535
/**
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.
3737
*/
3838
public function __construct(HttpClient $httpClient = null)
3939
{
@@ -58,7 +58,7 @@ class MyClass
5858
protected $httpAsyncClient;
5959

6060
/**
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.
6262
*/
6363
public function __construct(HttpAsyncClient $httpAsyncClient = null)
6464
{
@@ -69,7 +69,7 @@ class MyClass
6969

7070
## PSR-7 Message Factory Discovery
7171

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

7474
``` php
7575
use Http\Message\MessageFactory;
@@ -95,7 +95,7 @@ class MyClass
9595

9696
## PSR-7 URI Factory Discovery
9797

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

100100
``` php
101101
use Http\Message\UriFactory;
@@ -141,7 +141,8 @@ Classes registered manually are put on top of the list.
141141

142142
### Writing your own discovery
143143

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

146147
Here is an example discovery:
147148

docs/httplug.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ Httplug is an abstraction for HTTP clients. There are two main use cases:
55
1. Usage in a project
66
2. Usage in a reusable package
77

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`
99
or throws an exception that implements `Http\Client\Exception`.
1010

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`.
1212
It can be used later to retrieve a PSR-7 `ResponseInterface` or an exception that implements `Http\Client\Exception`.
1313

1414

@@ -24,20 +24,18 @@ See the [tutorial](tutorial.md) for a concrete example.
2424
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6.
2525
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.
2626

27-
There is two kind of implementation:
27+
There are two kind of implementation:
2828

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

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

3534
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter.
3635

3736
## Usage in a project
3837

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).
4139

4240
See [virtual package](virtual-package.md) for more information on the topic of working with Httplug implementations.
4341

docs/tutorial.md

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TODO: create client instance with discovery and do some requests
3434

3535
## Using an asynchronous client
3636

37-
When using an asynchronous client, it will use a PSR-7 `RequestInterface` and returns a `Http\Client\Promise` :
37+
Asynchronous client accepts a PSR-7 `RequestInterface` and returns a `Http\Client\Promise` :
3838

3939
```php
4040
use Http\Discovery\HttpAsyncClientDiscovery;
@@ -43,73 +43,78 @@ $httpAsyncClient = HttpAsyncClientDiscovery::find();
4343
$promise = $httpAsyncClient->sendAsyncRequest($request);
4444
```
4545

46-
This promise allows you to :
46+
### Using callback system
4747

48-
* Add callbacks for when the response is available or an errors happens by using the then method:
48+
This promise allows you to add callbacks for when the response is available or an errors happens by using the then method:
4949

50-
```php
51-
$promise->then(function (ResponseInterface $response) {
52-
// onFulfilled callback
53-
echo 'The response is available';
54-
55-
return $response;
56-
}, function (Exception $e) {
57-
// onRejected callback
58-
echo 'An error happens';
59-
60-
throw $e;
61-
});
62-
```
50+
```php
51+
$promise->then(function (ResponseInterface $response) {
52+
// onFulfilled callback
53+
echo 'The response is available';
54+
55+
return $response;
56+
}, function (Exception $e) {
57+
// onRejected callback
58+
echo 'An error happens';
59+
60+
throw $e;
61+
});
62+
```
6363

64-
This method will return another promise so you can manipulate the response and/or exception and
65-
still provide a way to interact with this object for your users:
64+
This method will return another promise so you can manipulate the response and/or exception and
65+
still provide a way to interact with this object for your users:
6666

67-
```php
68-
$promise->then(function (ResponseInterface $response) {
69-
// onFulfilled callback
70-
echo 'The response is available';
67+
```php
68+
$promise->then(function (ResponseInterface $response) {
69+
// onFulfilled callback
70+
echo 'The response is available';
7171

72-
return $response;
73-
}, function (Exception $e) {
74-
// onRejected callback
75-
echo 'An error happens';
72+
return $response;
73+
}, function (Exception $e) {
74+
// onRejected callback
75+
echo 'An error happens';
7676

77-
throw $e;
78-
})->then(function (ResponseInterface $response) {
79-
echo 'Response stil available';
77+
throw $e;
78+
})->then(function (ResponseInterface $response) {
79+
echo 'Response stil available';
8080

81-
return $response;
82-
}, function (Exception $e) {
83-
throw $e
84-
});
85-
```
81+
return $response;
82+
}, function (Exception $e) {
83+
throw $e
84+
});
85+
```
8686

87-
In order to achieve the chain callback, if you read previous examples carefully, callbacks provided to the `then` method __must__
88-
return a PSR-7 `ResponseInterface` or throw a `Http\Client\Exception`. For both of the callbacks, if it returns a PSR-7 `ResponseInterface`
89-
it will call the `onFulfilled` callback for the next element in the chain, if it throws a `Http\Client\Exception` it will call the `onRejected`
90-
callback.
87+
In order to achieve the chain callback, if you read previous examples carefully, callbacks provided to the `then` method __must__
88+
return a PSR-7 `ResponseInterface` or throw a `Http\Client\Exception`. For both of the callbacks, if it returns a PSR-7 `ResponseInterface`
89+
it will call the `onFulfilled` callback for the next element in the chain, if it throws a `Http\Client\Exception` it will call the `onRejected`
90+
callback.
9191

92-
i.e. you can inverse the behavior of a call:
92+
i.e. you can inverse the behavior of a call:
9393

94-
```php
95-
$promise->then(function (ResponseInterface $response) use($request) {
96-
// onFulfilled callback
97-
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...';
94+
```php
95+
$promise->then(function (ResponseInterface $response) use($request) {
96+
// onFulfilled callback
97+
echo 'The response is available, but it\'s not ok...';
10398

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
113118

114119
Here is a full example of a classic usage when using the `sendAsyncRequest` method:
115120

0 commit comments

Comments
 (0)