slepic / guzzle-http-observing-middleware
Adapter of slepic/http-transfer ObserverInterface to guzzlehttp/guzzle middleware.
Installs: 17 158
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
- guzzlehttp/guzzle: ^6.3 || ^7.0
- slepic/http-transfer: ^0.1 || ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpunit/phpunit: ~5.0
Suggests
- slepic/http-transfer-observer-implementation: See existing observers you can plug into this middleware.
Provides
README
guzzle-http-observing-middleware
Adapter of ObserverInterface from slepic/http-transfer package to guzzlehttp/guzzle middleware.
Requirements
PHP >=5.6
Installation
Install with composer.
composer require slepic/guzzle-http-observing-middleware
Usage
Wrap any instance of \Slepic\Http\Transfer\Observer\ObserverInterface
from package slepic/http-transfer
in the \Slepic\Guzzle\Http\ObservingMiddleware\ObservingMiddleware
and pass it to handler stack of your guzzle client.
All requests sent through the guzzle client will now be notified when requests are starting to get processed and when responses are received.
See an example where we use \Slepic\Http\Transfer\History\HistoryObserver
to log requests and responses with timing.
$storage = new ArrayStorage();
$observer = new HistoryObserver($storage);
$middleware = new ObservingMiddleware($observer);
$client = new \GuzzleHttp\Client();
$client->getConfig('handler')->unshift($middleware);
try {
$response = $client->request($method, $uri);
} catch (\Exception $e) {
assert($storage[0]->getRequest()->getMethod() === $method);
assert((string)($storage[0]->getRequest()->getUri()) === $uri);
assert($storage[0]->getException() === $e);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
throw $e;
}
assert($storage[0]->getRequest()->getMethod() === 'GET');
assert((string)($storage[0]->getRequest()->getUri()) === $uri);
assert($storage[0]->getResponse() === $response);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
Related
- See slepic/http-transfer-observer-implementation for known observers.
- See slepic/psr-http-message-tracy-panel to get your http client transfer into Tracy.