Skip to content

PSR ClientInterface implementation that wraps another implementation and observes the transfers using ObserverInterfce from slepic/http-transfer package.

License

Notifications You must be signed in to change notification settings

slepic/psr-http-observing-client

Repository files navigation

Build Status Style Status

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.

About

PSR ClientInterface implementation that wraps another implementation and observes the transfers using ObserverInterfce from slepic/http-transfer package.

Resources

License

Stars

Watchers

Forks

Packages

No packages published