PSR ClientInterface implementation that wraps another implementation and observes the transfers using ObserverInterfce from slepic/http-transfer package.
PHP 7
Install with composer
composer require slepic/psr-http-observing-client
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()));
See slepic/http-transfer-observer-implementation
for list of existing observers.