slepic / psr-http-observing-client
PSR ClientInterface implementation that wraps another implementation and observes the transfers using ObserverInterfce from slepic/http-transfer package.
Requires
- php: ^7.0
- psr/http-client: ^1.0
- slepic/http-transfer: ^0.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpunit/phpunit: ~5.0
Suggests
- slepic/http-transfer-observer-implementation: See existing observers to plug in your psr http client.
Provides
This package is auto-updated.
Last update: 2024-10-07 13:36:22 UTC
README
psr-http-observing-client
PSR ClientInterface implementation that wraps another implementation and observes the transfers using ObserverInterfce from slepic/http-transfer package.
Requirements
PHP 7
Installation
Install with composer
composer require slepic/psr-http-observing-client
Usage
Wrap any instance of \Psr\Http\Client\ClientInterface
with the \Slepic\Psr\Http\ObservingClient\ObservingClient
and pass it a \Slepic\Http\Transfer\Observer\ObserverInterface
from package slepic/http-transfer
.
If you now send all your requests through the ObservingClient, the observer will be notified about start and end of all the transfers.
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);
$psrClient = new SomePsrClient();
$client = new ObservingClient($psrClient, $observer);
try {
$response = $client->sendRequest($request);
} catch (\Exception $e) {
assert($storage[0]->getRequest() === $request);
assert($storage[0]->getException() === $e);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
throw $e;
}
assert($storage[0]->getRequest() === $request);
assert($storage[0]->getResponse() === $response);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
Observers
See slepic/http-transfer-observer-implementation
for list of existing observers.