slepic/psr-http-observing-client

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

0.1.0 2019-07-07 01:25 UTC

This package is auto-updated.

Last update: 2024-10-07 13:36:22 UTC


README

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.