Skip to content

Commit 7883359

Browse files
Improved documentation
1 parent 96d2c5a commit 7883359

File tree

1 file changed

+68
-29
lines changed

1 file changed

+68
-29
lines changed

README.md

+68-29
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,88 @@
11
# Guzzle Commands
22

3-
This library uses Guzzle (``guzzlehttp/guzzle``, version 7.x) and provides the
4-
foundations to create fully-featured web service clients by abstracting Guzzle
5-
HTTP **requests** and **responses** into higher-level **commands** and
6-
**results**. A **middleware** system, analogous to — but separate from — the one
7-
in the HTTP layer may be used to customize client behavior when preparing
8-
commands into requests and processing responses into results.
3+
This library uses Guzzle and provides the foundations to create fully-featured
4+
web service clients by abstracting Guzzle HTTP *requests* and *responses* into
5+
higher-level *commands* and *results*. A *middleware* system, analogous to, but
6+
separate from, the one in the HTTP layer may be used to customize client
7+
behavior when preparing commands into requests and processing responses into
8+
results.
99

1010
### Commands
1111

12-
Key-value pair objects representing an operation of a web service. Commands have a name and a set of parameters.
12+
Key-value pair objects representing an operation of a web service. Commands
13+
have a name and a set of parameters.
1314

1415
### Results
1516

16-
Key-value pair objects representing the processed result of executing an operation of a web service.
17+
Key-value pair objects representing the processed result of executing an
18+
operation of a web service.
1719

1820
## Installing
1921

20-
This project can be installed using Composer:
22+
This project can be installed using [Composer](https://getcomposer.org/):
2123

22-
``composer require guzzlehttp/command``
23-
24-
For **Guzzle 5**, use ``composer require guzzlehttp/command:0.8.*``. The source
25-
code for the Guzzle 5 version is available on the
26-
`0.8 branch <https://github.com/guzzle/command/tree/0.8>`_.
27-
28-
**Note:** If Composer is not installed [globally](https://getcomposer.org/doc/00-intro.md#globally) then you may need to run the preceding Composer commands using ``php composer.phar`` (where ``composer.phar`` is the path to your copy of Composer), instead of just ``composer``.
24+
```
25+
composer require guzzlehttp/command
26+
```
2927

3028
## Service Clients
3129

3230
Service Clients are web service clients that implement the
33-
``GuzzleHttp\Command\ServiceClientInterface`` and use an underlying Guzzle HTTP
34-
client (``GuzzleHttp\Client``) to communicate with the service. Service clients
35-
create and execute **commands** (``GuzzleHttp\Command\CommandInterface``),
31+
`GuzzleHttp\Command\ServiceClientInterface` and use an underlying Guzzle HTTP
32+
client (`GuzzleHttp\ClientInterface`) to communicate with the service. Service
33+
clients create and execute *commands* (`GuzzleHttp\Command\CommandInterface`),
3634
which encapsulate operations within the web service, including the operation
3735
name and parameters. This library provides a generic implementation of a service
38-
client: the ``GuzzleHttp\Command\ServiceClient`` class.
36+
client: the `GuzzleHttp\Command\ServiceClient` class.
3937

4038
## Instantiating a Service Client
4139

42-
@TODO Add documentation
40+
The provided service client implementation (`GuzzleHttp\Command\ServiceClient`)
41+
can be instantiated by providing the following arguments:
4342

44-
* ``ServiceClient``'s constructor
45-
* Transformer functions (``$commandToRequestTransformer`` and ``$responseToResultTransformer``)
46-
* The ``HandlerStack``
43+
1. A fully-configured Guzzle HTTP client that will be used to perform the
44+
underlying HTTP requests. That is, an instance of an object implementing
45+
`GuzzleHttp\ClientInterface` such as `new GuzzleHttp\Client()`.
46+
1. A callable that transforms a Command into a Request. The function should
47+
accept a `GuzzleHttp\Command\CommandInterface` object and return a
48+
`Psr\Http\Message\RequestInterface` object.
49+
1. A callable that transforms a Response into a Result. The function should
50+
accept a `Psr\Http\Message\ResponseInterface` object and optionally a
51+
`Psr\Http\Message\RequestInterface` object, and return a
52+
`GuzzleHttp\Command\ResultInterface` object.
53+
1. Optionally, a Guzzle HandlerStack (`GuzzleHttp\HandlerStack`), which can be
54+
used to add command-level middleware to the service client.
55+
56+
Below is an example configured to send and receive JSON payloads:
57+
58+
```php
59+
use GuzzleHttp\Command\CommandInterface;
60+
use GuzzleHttp\Command\Result;
61+
use GuzzleHttp\Command\ResultInterface;
62+
use GuzzleHttp\Command\ServiceClient;
63+
use GuzzleHttp\Psr7\Request;
64+
use GuzzleHttp\UriTemplate\UriTemplate;
65+
use GuzzleHttp\Utils;
66+
use Psr\Http\Message\RequestInterface;
67+
use Psr\Http\Message\ResponseInterface;
68+
69+
$client = new ServiceClient(
70+
new HttpClient(),
71+
function (CommandInterface $command): RequestInterface {
72+
return new Request(
73+
'POST',
74+
UriTemplate::expand('/{command}', ['command' => $command->getName()]),
75+
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
76+
Utils::jsonEncode($command->toArray())
77+
);
78+
},
79+
function (ResponseInterface $response, RequestInterface $request): ResultInterface {
80+
return new Result(
81+
Utils::jsonDecode((string) $response->getBody(), true)
82+
);
83+
}
84+
);
85+
```
4786

4887
## Executing Commands
4988

@@ -53,18 +92,18 @@ Service clients create command objects using the ``getCommand()`` method.
5392
$commandName = 'foo';
5493
$arguments = ['baz' => 'bar'];
5594
$command = $client->getCommand($commandName, $arguments);
56-
5795
```
5896

59-
After creating a command, you may execute the command using the ``execute()`` method of the client.
97+
After creating a command, you may execute the command using the `execute()`
98+
method of the client.
6099

61100
```php
62101
$result = $client->execute($command);
63102
```
64103

65-
The result of executing a command will be a ``GuzzleHttp\Command\ResultInterface``
66-
object. Result objects are ``ArrayAccess``-ible and contain the data parsed from
67-
HTTP response.
104+
The result of executing a command will be an instance of an object implementing
105+
`GuzzleHttp\Command\ResultInterface`. Result objects are `ArrayAccess`-ible and
106+
contain the data parsed from HTTP response.
68107

69108
Service clients have magic methods that act as shortcuts to executing commands
70109
by name without having to create the ``Command`` object in a separate step

0 commit comments

Comments
 (0)