|
1 |
| -Introduction to HTTPlug |
2 |
| -======================= |
| 1 | +HTTPlug: HTTP client abstraction |
| 2 | +================================ |
3 | 3 |
|
4 |
| -HTTPlug implementations |
5 |
| ------------------------ |
| 4 | +HTTPlug allows you to write reusable libraries and applications that need |
| 5 | +an HTTP client without binding to a specific implementation. |
| 6 | +When all packages used in an application only specify HTTPlug, |
| 7 | +the application developers can choose the client that best fits their project |
| 8 | +and use the same client with all packages. |
6 | 9 |
|
7 |
| -HTTPlug implementations typically are either HTTP clients of their own, or adapters wrapping existing clients |
8 |
| -like Guzzle 6. In the latter case, they will depend on the required client implementation, |
9 |
| -so you only need to require the adapter and not the actual client. |
| 10 | +The client interfaces |
| 11 | +--------------------- |
10 | 12 |
|
| 13 | +HTTPlug defines two HTTP client interfaces that we kept as simple as possible: |
11 | 14 |
|
12 |
| -There are two kind of implementations: |
| 15 | +* ``HttpClient`` defines a ``sendRequest`` method that sends a PSR-7 |
| 16 | + ``RequestInterface`` and either returns a PSR-7 ``ResponseInterface`` or |
| 17 | + throws an exception that implements ``Http\Client\Exception``. |
13 | 18 |
|
14 |
| - - `php-http/client-implementation`_: |
15 |
| - the synchronous implementation that waits for the response / error before returning from the ``sendRequest`` method. |
| 19 | +* ``HttpAsyncClient`` defines a ``sendAsyncRequest`` method that sends a request |
| 20 | + asynchronously and always returns a ``Http\Client\Promise``. |
16 | 21 |
|
17 |
| - - `php-http/async-client-implementation`_: |
18 |
| - the asynchronous implementation that immediately returns a ``Http\Promise\Promise``, |
19 |
| - allowing to send several requests in parallel and handling responses later. |
| 22 | +Implementations |
| 23 | +--------------- |
20 | 24 |
|
21 |
| -Check links above for the full list of implementations. |
| 25 | +PHP-HTTP offers two types of clients that implement the above interfaces: |
22 | 26 |
|
23 |
| -.. _`php-http/client-implementation`: https://packagist.org/providers/php-http/client-implementation |
24 |
| -.. _`php-http/async-client-implementation`: https://packagist.org/providers/php-http/async-client-implementation: |
| 27 | +1. Standalone clients that directly implement the interfaces. |
25 | 28 |
|
26 |
| -Usage in an application |
27 |
| ------------------------ |
| 29 | + Examples: :doc:`/clients/curl-client` and :doc:`/clients/socket-client`. |
28 | 30 |
|
29 |
| -When writing an application, you need to require a concrete implementation_. |
| 31 | +2. Adapters that wrap existing HTTP client, such as Guzzle. These adapters act |
| 32 | + as a bridge between the HTTPlug interfaces and the clients that do not (yet) |
| 33 | + implement these interfaces. |
30 | 34 |
|
31 |
| -See :doc:`virtual-package` for more information on the topic of working with HTTPlug implementations. |
| 35 | + Examples: :doc:`/clients/guzzle6-adapter` and :doc:`/clients/react-adapter`. |
32 | 36 |
|
33 |
| -Installation in a reusable package |
34 |
| ----------------------------------- |
| 37 | +.. note:: |
35 | 38 |
|
36 |
| -In many cases, packages are designed to be reused from the very beginning. |
37 |
| -For example, API clients are usually used in other packages/applications, not on their own. |
| 39 | + Ideally, all HTTP client libraries out there will implement the HTTPlug |
| 40 | + interfaces. At that point, our adapters will no longer be necessary. |
38 | 41 |
|
39 |
| -Reusable packages should **not rely on a concrete implementation** (like Guzzle 6), |
40 |
| -but only require any implementation of HTTPlug. HTTPlug uses the concept of virtual packages. |
41 |
| -Instead of depending on only the interfaces, which would be missing an implementation, |
42 |
| -or depending on one concrete implementation, you should depend on the virtual package ``php-http/client-implementation`` |
43 |
| -or ``php-http/async-client-implementation``. There is no package with that name, |
44 |
| -but all clients and adapters implementing HTTPlug declare that they provide one of this virtual package or both. |
| 42 | +Usage |
| 43 | +----- |
45 | 44 |
|
46 |
| -You need to edit the ``composer.json`` of your package to add the virtual package. |
47 |
| -For development (installing the package standalone, running tests), |
48 |
| -add a concrete implementation in the ``require-dev`` section to make the project installable: |
| 45 | +There are two main use cases for HTTPlug: |
49 | 46 |
|
50 |
| -.. code-block:: json |
51 |
| -
|
52 |
| - { |
53 |
| - "require": { |
54 |
| - "php-http/client-implementation": "^1.0" |
55 |
| - }, |
56 |
| - "require-dev": { |
57 |
| - "php-http/guzzle6-adapter": "^1.0" |
58 |
| - }, |
59 |
| - } |
60 |
| -
|
61 |
| -For extra convenience, you can use the :doc:`/discovery` system to free the user of your |
62 |
| -package from having to instantiate the client. |
63 |
| -You should however always accept injecting the client instance to allow the user to configure the client as needed. |
64 |
| -You can find an example in the :doc:`/discovery` documentation. |
65 |
| - |
66 |
| -Users of your package will have to select a concrete adapter in their project to make your package installable. |
67 |
| -Best point them to the :doc:`virtual-package` page. |
68 |
| - |
69 |
| -To be able to send requests, you should not depend on a specific PSR-7 implementation, |
70 |
| -but use the :ref:`message-factory` system. |
71 |
| - |
72 |
| -Framework Integration |
73 |
| -^^^^^^^^^^^^^^^^^^^^^ |
74 |
| - |
75 |
| -HTTPlug can be used in any PHP based project. |
76 |
| -Nonetheless, we provide particular integration for some popular frameworks: |
77 |
| - |
78 |
| -- HttplugBundle_: integration with the Symfony framework. |
| 47 | +* usage in an application that executes HTTP requests (see :doc:`application-developers`). |
| 48 | +* usage in a reusable package that executes HTTP requests (see :doc:`library-developers`). |
79 | 49 |
|
80 | 50 | History
|
81 | 51 | -------
|
82 | 52 |
|
83 |
| -This project has been started by `Eric Geloen`_ as `Ivory Http Adapter`_. It never made it to a stable release, |
84 |
| -but it relied on PSR-7 which was not stable either that time. Because of the constantly changing PSR-7, |
85 |
| -Eric had to rewrite the library over and over again (at least the message handling part, |
86 |
| -which in most cases affected every adapter as well). |
| 53 | +This project has been started by `Eric Geloen`_ as `Ivory Http Adapter`_. It |
| 54 | +never made it to a stable release, but it relied on PSR-7 which was not stable |
| 55 | +either that time. Because of the constantly changing PSR-7, Eric had to rewrite |
| 56 | +the library over and over again (at least the message handling part, which in |
| 57 | +most cases affected every adapter as well). |
87 | 58 |
|
88 |
| -In 2015, a decision has been made to move the library to its own organization, so PHP HTTP was born. |
| 59 | +In 2015, a decision has been made to move the library to its own organization, |
| 60 | +so PHP-HTTP was born. |
89 | 61 |
|
90 |
| -See :doc:`migrating` for a guide how to migrate your code from the Ivory adapter to HTTPlug. |
| 62 | +See :doc:`migrating` for a guide how to migrate your code from the Ivory |
| 63 | +adapter. |
91 | 64 |
|
92 |
| -.. _implementation: https://packagist.org/providers/php-http/client-implementation |
93 |
| -.. _HttplugBundle: https://github.com/php-http/HttplugBundle |
94 | 65 | .. _`Eric Geloen`: https://github.com/egeloen
|
95 |
| -.. _`Ivory Http Adapter`: https://github.com/egeloen/ivory-http-adapter). |
| 66 | +.. _`Ivory Http Adapter`: https://github.com/egeloen/ivory-http-adapter |
0 commit comments