• Stars
    star
    110
  • Rank 305,595 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides the foundation for building web service clients with Guzzle

Guzzle Commands

This library uses Guzzle and provides the foundations to create fully-featured web service clients by abstracting Guzzle HTTP requests and responses into higher-level commands and results. A middleware system, analogous to, but separate from, the one in the HTTP layer may be used to customize client behavior when preparing commands into requests and processing responses into results.

Commands

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

Results

Key-value pair objects representing the processed result of executing an operation of a web service.

Installing

This project can be installed using Composer:

composer require guzzlehttp/command

Service Clients

Service Clients are web service clients that implement the GuzzleHttp\Command\ServiceClientInterface and use an underlying Guzzle HTTP client (GuzzleHttp\ClientInterface) to communicate with the service. Service clients create and execute commands (GuzzleHttp\Command\CommandInterface), which encapsulate operations within the web service, including the operation name and parameters. This library provides a generic implementation of a service client: the GuzzleHttp\Command\ServiceClient class.

Instantiating a Service Client

The provided service client implementation (GuzzleHttp\Command\ServiceClient) can be instantiated by providing the following arguments:

  1. A fully-configured Guzzle HTTP client that will be used to perform the underlying HTTP requests. That is, an instance of an object implementing GuzzleHttp\ClientInterface such as new GuzzleHttp\Client().
  2. A callable that transforms a Command into a Request. The function should accept a GuzzleHttp\Command\CommandInterface object and return a Psr\Http\Message\RequestInterface object.
  3. A callable that transforms a Response into a Result. The function should accept a Psr\Http\Message\ResponseInterface object and optionally a Psr\Http\Message\RequestInterface object, and return a GuzzleHttp\Command\ResultInterface object.
  4. Optionally, a Guzzle HandlerStack (GuzzleHttp\HandlerStack), which can be used to add command-level middleware to the service client.

Below is an example configured to send and receive JSON payloads:

use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use GuzzleHttp\Command\ServiceClient;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\UriTemplate\UriTemplate;
use GuzzleHttp\Utils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$client = new ServiceClient(
    new HttpClient(),
    function (CommandInterface $command): RequestInterface {
        return new Request(
            'POST',
            UriTemplate::expand('/{command}', ['command' => $command->getName()]),
            ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
            Utils::jsonEncode($command->toArray())
        );
    },
    function (ResponseInterface $response, RequestInterface $request): ResultInterface {
        return new Result(
            Utils::jsonDecode((string) $response->getBody(), true)
        );
    }
);

Executing Commands

Service clients create command objects using the getCommand() method.

$commandName = 'foo';
$arguments = ['baz' => 'bar'];
$command = $client->getCommand($commandName, $arguments);

After creating a command, you may execute the command using the execute() method of the client.

$result = $client->execute($command);

The result of executing a command will be an instance of an object implementing GuzzleHttp\Command\ResultInterface. Result objects are ArrayAccess-ible and contain the data parsed from HTTP response.

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

$result = $client->foo(['baz' => 'bar']);

Asynchronous Commands

@TODO Add documentation

  • -Async suffix for client methods
  • Promises
// Create and execute an asynchronous command.
$command = $command = $client->getCommand('foo', ['baz' => 'bar']);
$promise = $client->executeAsync($command);

// Use asynchronous commands with magic methods.
$promise = $client->fooAsync(['baz' => 'bar']);

@TODO Add documentation

  • wait()-ing on promises.
$result = $promise->wait();

echo $result['fizz']; //> 'buzz'

Concurrent Requests

@TODO Add documentation

  • executeAll()
  • executeAllAsync().
  • Options (fulfilled, rejected, concurrency)

Middleware: Extending the Client

Middleware can be added to the service client or underlying HTTP client to implement additional behavior and customize the Command-to-Result and Request-to-Response lifecycles, respectively.

Security

If you discover a security vulnerability within this package, please send an email to [email protected]. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced. Please see Security Policy for more information.

License

Guzzle is made available under the MIT License (MIT). Please see License File for more information.

For Enterprise

Available as part of the Tidelift Subscription

The maintainers of Guzzle and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

More Repositories

1

guzzle

Guzzle, an extensible PHP HTTP client
PHP
22,941
star
2

psr7

PSR-7 HTTP message library
PHP
7,820
star
3

promises

Promises/A+ library for PHP with synchronous support
PHP
7,527
star
4

RingPHP

[DEPRECATED] Simple handler system used to power clients and servers in PHP (this project is no longer used in Guzzle 6+)
PHP
845
star
5

streams

[DEPRECATED] Provides a simple abstraction over streams of data
PHP
640
star
6

guzzle-services

Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
PHP
251
star
7

oauth-subscriber

Signs Guzzle requests using OAuth 1.0 (Guzzle 6+)
PHP
242
star
8

guzzle_sphinx_theme

Sphinx theme used by Guzzle
HTML
168
star
9

guzzle3

[DEPRECATED] This is end of life and not maintained. Migrate to https://github.com/guzzle/guzzle
PHP
151
star
10

uri-template

PHP
104
star
11

cache-subscriber

[DEPRECATED] Private transparent proxy cache that caches HTTP responses (Guzzle 5+)
PHP
63
star
12

retry-subscriber

[DEPRECATED] Retries failed requests using customizable retry strategies. Guzzle 4/5 only.
PHP
61
star
13

log-subscriber

[DEPRECATED] Logs HTTP requests and Responses as they are sent over the wire. Not used in Guzzle 6.
PHP
43
star
14

progress-subscriber

[DEPRECATED] Emits upload and download progress events (Guzzle 4)
PHP
19
star
15

message-integrity-subscriber

[DEPRECATED] Verifies the integrity of HTTP responses using customizable validators. Guzzle 4/5.
PHP
12
star
16

test-server

A node.js server and a PHP controller class one can use when testing
JavaScript
8
star
17

.github

Default community health files
3
star