-
Notifications
You must be signed in to change notification settings - Fork 56
add documentation for retry and redirect plugins, split building own, document library usage #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
.. _client-common: | ||
|
||
Client Common | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i propose we stop using labels for whole doc chapters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't really know why or why not, so fine with me :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess he means using doc reference instead. Am I right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with a label like this, you write There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok, got it. |
||
============= | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,16 @@ Discovery | |
========= | ||
|
||
The discovery service allows to find and use installed resources. | ||
|
||
Under the hood it uses `Puli`_ for the discovery logic. All of our packages provide Puli resources. | ||
Discovery is simply a convenience wrapper to statically access clients and factories for when | ||
Dependency Injection is not an option. Discovery is useful in libraries that want to offer | ||
zero-configuration services relying on the virtual packages. If you have Dependency Injection available, | ||
using Puli directly is more elegant (see for example the Symfony HttplugBundle). | ||
|
||
Consumers of libraries using discovery still need to make sure they install one of the implementations. | ||
Discovery can only find installed code, not fetch code from other sources. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its a bit redundant but maybe not that obvious for beginners... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I think it is enough to make clear that discovery discovers installed stuff. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is many question about discovery, writing more things about (even if its redundant) is for the best IMO |
||
|
||
Currently available discovery services: | ||
|
||
- HTTP Client Discovery | ||
|
@@ -32,7 +36,7 @@ In both cases you have to install the discovery package itself: | |
|
||
.. code-block:: bash | ||
|
||
$ composer require php-http/discovery | ||
$ composer require php-http/discovery | ||
|
||
As mentioned above, discovery relies on Puli. In order to use discovery, you need to also set up Puli. | ||
The easiest way is installing the composer-plugin which automatically configures all the composer packages to act as | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ The ``MessageFactory`` aims to provide an easy way to construct messages. | |
Usage | ||
----- | ||
|
||
.. _stream-factory: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is sort of a temporary label until we expand this documentation. |
||
|
||
This package provides interfaces for PSR-7 factories including: | ||
|
||
- ``MessageFactory`` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
Building Custom Plugins | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no changes in this text, just moved out of the introduction as it was getting too long There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
======================= | ||
|
||
When writing your own Plugin, you need to be aware that the Plugin Client is async first. | ||
This means that every plugin must be written with Promises. More about this later. | ||
|
||
Each plugin must implement the ``Http\Client\Plugin\Plugin`` interface. | ||
|
||
This interface defines the ``handleRequest`` method that allows to modify behavior of the call:: | ||
|
||
/** | ||
* Handles the request and returns the response coming from the next callable. | ||
* | ||
* @param RequestInterface $request Request to use. | ||
* @param callable $next Callback to call to have the request, it muse have the request as it first argument. | ||
* @param callable $first First element in the plugin chain, used to to restart a request from the beginning. | ||
* | ||
* @return Promise | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first); | ||
|
||
The ``$request`` comes from an upstream plugin or Plugin Client itself. | ||
You can replace it and pass a new version downstream if you need. | ||
|
||
.. note:: | ||
|
||
Be aware that the request is immutable. | ||
|
||
The ``$next`` callable is the next plugin in the execution chain. When you need to call it, you must pass the ``$request`` | ||
as the first argument of this callable. | ||
|
||
For example a simple plugin setting a header would look like this:: | ||
|
||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
$newRequest = $request->withHeader('MyHeader', 'MyValue'); | ||
|
||
return $next($newRequest); | ||
} | ||
|
||
The ``$first`` callable is the first plugin in the chain. It allows you to completely reboot the execution chain, or send | ||
another request if needed, while still going through all the defined plugins. | ||
Like in case of the ``$next`` callable, you must pass the ``$request`` as the first argument:: | ||
|
||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
if ($someCondition) { | ||
$newRequest = new Request(); | ||
$promise = $first($newRequest); | ||
|
||
// Use the promise to do some jobs ... | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
.. warning:: | ||
|
||
In this example the condition is not superfluous: | ||
you need to have some way to not call the ``$first`` callable each time | ||
or you will end up in an infinite execution loop. | ||
|
||
The ``$next`` and ``$first`` callables will return a :doc:`/components/promise`. | ||
You can manipulate the ``ResponseInterface`` or the ``Exception`` by using the | ||
``then`` method of the promise:: | ||
|
||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
$newRequest = $request->withHeader('MyHeader', 'MyValue'); | ||
|
||
return $next($request)->then(function (ResponseInterface $response) { | ||
return $response->withHeader('MyResponseHeader', 'value'); | ||
}, function (Exception $exception) { | ||
echo $exception->getMessage(); | ||
|
||
throw $exception; | ||
}); | ||
} | ||
|
||
.. warning:: | ||
|
||
Contract for the ``Http\Promise\Promise`` is temporary until a | ||
PSR_ is released. Once it is out, we will use this PSR in HTTPlug and | ||
deprecate the old contract. | ||
|
||
To better understand the whole process check existing implementations in the | ||
`plugin repository`_. | ||
|
||
Contributing Your Plugins to PHP-HTTP | ||
------------------------------------- | ||
|
||
We are open to contributions. If the plugin is of general interest and is not too complex, the best | ||
is to do a Pull Request to ``php-http/plugins``. Please see the :doc:`contribution guide <../development/contributing>`. | ||
We don't promise that every plugin gets merged into the core. We need to keep the core as small as | ||
possible with only the most widely used plugins to keep it maintainable. | ||
|
||
The alternative is providing your plugins in your own repository. Please let us know when you do, | ||
we would like to add a list of existing third party plugins to the list of plugins. | ||
|
||
.. _PSR: https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs | ||
.. _plugin repository: https://github.com/php-http/plugins |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sagikazarmark is this what you meant by mentioning the factories in the installation instructions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant this. If the message factory is necessary, it probably worths mentioning in the install instructions:
http://docs.php-http.org/en/latest/clients/guzzle5-adapter.html#installation
There are two include files for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which i now include. correct like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, missed that they are included.