Skip to content

Commit c044a92

Browse files
committed
Merge pull request #76 from php-http/feature/plugins-doc
Feature/plugins doc
2 parents 9f87c4c + c5b2795 commit c044a92

File tree

7 files changed

+147
-8
lines changed

7 files changed

+147
-8
lines changed

plugins/authentication.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ from ``php-http/message`` to authenticate requests sent through the client::
66

77
use Http\Discovery\HttpClientDiscovery;
88
use Http\Message\Authentication\BasicAuth;
9-
use Http\Plugins\PluginClient;
10-
use Http\Plugins\AuthenticationPlugin;
9+
use Http\Client\Plugin\PluginClient;
10+
use Http\Client\Plugin\AuthenticationPlugin;
1111

1212
$authentication = new BasicAuth('username', 'password');
1313
$authenticationPlugin = new AuthenticationPlugin($authentication);

plugins/content-length.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
Content-Length Plugin
22
=====================
33

4-
TODO
4+
The ``ContentLengthPlugin`` sets the correct ``Content-Length`` header value based on the size of the body stream of the
5+
request. This helps HTTP servers to handle the request::
6+
7+
use Http\Discovery\HttpClientDiscovery;
8+
use Http\Client\Plugin\PluginClient;
9+
use Http\Client\Plugin\ContentLengthPlugin;
10+
11+
$contentLengthPlugin = new ContentLengthPlugin();
12+
13+
$pluginClient = new PluginClient(
14+
HttpClientDiscovery::find(),
15+
[$contentLengthPlugin]
16+
);
17+
18+
If the size of the stream can not be determined, the plugin sets the Encoding header to ``chunked``, as defined in
19+
:rfc:`7230#section-4.1`
20+
21+
This is useful when you want to transfer data of unknown size to an HTTP application without consuming memory.
22+
23+
As an example, let's say you want to send a tar archive of the current directory to an API. Normally you would
24+
end up doing this in 2 steps, first saving the result of the tar archive into a file or into the memory of
25+
PHP with a variable, then sending this content with an HTTP Request.
26+
27+
With this plugin you can achieve this behavior without doing the first step::
28+
29+
proc_open("/usr/bin/env tar c .", [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes, "/path/to/directory");
30+
$tarResource = $pipes[1];
31+
32+
$request = MessageFactoryDiscovery::find()->createRequest('POST', '/url/to/api/endpoint', [], $tarResource);
33+
$response = $pluginClient->sendRequest($request);
34+
35+
In this case the tar output is directly streamed to the server without using memory on the PHP side.

plugins/cookie.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
Cookie Plugin
22
=============
33

4-
TODO: explain the cookie plugin
4+
The ``CookiePlugin`` allow you to store cookies in a ``CookieJar`` and reuse them on consequent requests according
5+
to :rfc:`6265#section-4` specification::
6+
7+
use Http\Discovery\HttpClientDiscovery;
8+
use Http\Message\CookieJar;
9+
use Http\Client\Plugin\PluginClient;
10+
use Http\Client\Plugin\CookiePlugin;
11+
12+
$cookiePlugin = new CookiePlugin(new CookieJar());
13+
14+
$pluginClient = new PluginClient(
15+
HttpClientDiscovery::find(),
16+
[$cookiePlugin]
17+
);

plugins/decoder.rst

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
Decoder Plugin
22
==============
33

4-
TODO
4+
The ``DecoderPlugin`` decodes the body of the response with filters coming from the ``Transfer-Encoding``
5+
or ``Content-Encoding`` headers::
6+
7+
use Http\Discovery\HttpClientDiscovery;
8+
use Http\Client\Plugin\PluginClient;
9+
use Http\Client\Plugin\DecoderPlugin;
10+
11+
$decoderPlugin = new DecoderPlugin();
12+
13+
$pluginClient = new PluginClient(
14+
HttpClientDiscovery::find(),
15+
[$decoderPlugin]
16+
);
17+
18+
The plugin can handle the following encodings:
19+
20+
* ``chunked``: Decode a stream with a ``chunked`` encoding (no size in the ``Content-Length`` header of the response)
21+
* ``compress``: Decode a stream encoded with the ``compress`` encoding according to :rfc:`1950`
22+
* ``deflate``: Decode a stream encoded with the ``inflate`` encoding according to :rfc:`1951`
23+
* ``gzip``: Decode a stream encoded with the ``gzip`` encoding according to :rfc:`1952`
24+
25+
You can also use the decoder plugin to decode only the ``Transfer-Encoding`` header and not the ``Content-Encoding`` one
26+
by setting the ``use_content_encoding`` configuration option to false::
27+
28+
$decoderPlugin = new DecoderPlugin(['use_content_encoding' => false]);
29+
30+
Not decoding content is useful when you don't want to get the encoded response body, or acting as a proxy but sill
31+
be able to decode message from the ``Transfer-Encoding`` header value.

plugins/error.rst

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
Error Plugin
22
============
33

4-
TODO: explain the error plugin
4+
The ``ErrorPlugin`` transforms responses with HTTP error status codes into exceptions:
5+
6+
* 400-499 status code are transformed into ``Http\Client\Plugin\Exception\ClientErrorException``;
7+
* 500-599 status code are transformed into ``Http\Client\Plugin\Exception\ServerErrorException``
8+
9+
Both exceptions extend the ``Http\Client\Exception\HttpException`` class, so you can fetch the request
10+
and the response coming from them::
11+
12+
use Http\Discovery\HttpClientDiscovery;
13+
use Http\Client\Plugin\PluginClient;
14+
use Http\Client\Plugin\ErrorPlugin;
15+
use Http\Client\Plugin\Exception\ClientErrorException;
16+
17+
$errorPlugin = new ErrorPlugin();
18+
19+
$pluginClient = new PluginClient(
20+
HttpClientDiscovery::find(),
21+
[$errorPlugin]
22+
);
23+
24+
...
25+
26+
try {
27+
$response = $pluginClient->sendRequest($request);
28+
} catch (ClientErrorException $e) {
29+
if ($e->getResponse()->getStatusCode() == 404) {
30+
// Something has not been found
31+
}
32+
}

plugins/history.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
History Plugin
22
==============
33

4-
TODO: explain the error plugin
4+
The ``HistoryPlugin`` notifies a Http\Client\Plugin\Journal of all successful and failed calls::
5+
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Client\Plugin\PluginClient;
8+
use Http\Client\Plugin\HistoryPlugin;
9+
10+
$historyPlugin = new HistoryPlugin(new \My\Journal\Implementation());
11+
12+
$pluginClient = new PluginClient(
13+
HttpClientDiscovery::find(),
14+
[$historyPlugin]
15+
);
16+
17+
18+
As an example, HttplugBundle uses this plugin to collect responses or exceptions associated with
19+
requests for the debug toolbar
20+
21+
This plugin only collect data after resolution. For logging purposes it's best to use the `LoggerPlugin` which logs
22+
as soon as possible.

plugins/logger.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
Logger Plugin
22
=============
33

4-
TODO
4+
The ``LoggerPlugin`` converts requests, responses and exceptions to strings and logs them with a PSR3_
5+
compliant logger::
6+
7+
use Http\Discovery\HttpClientDiscovery;
8+
use Http\Client\Plugin\PluginClient;
9+
use Http\Client\Plugin\LoggerPlugin;
10+
use Monolog\Logger;
11+
12+
$loggerPlugin = new LoggerPlugin(new Logger('http'));
13+
14+
$pluginClient = new PluginClient(
15+
HttpClientDiscovery::find(),
16+
[$loggerPlugin]
17+
);
18+
19+
By default it uses ``Http\Message\Formatter\SimpleFormatter`` to format the request or the response into a string.
20+
You can use any formatter implementing the ``Http\Message\Formatter`` interface::
21+
22+
$formatter = new \My\Formatter\Implemenation();
23+
24+
$loggerPlugin = new LoggerPlugin(new Logger('http'), $formatter);
25+
26+
.. _PSR3: http://www.php-fig.org/psr/psr-3/

0 commit comments

Comments
 (0)