From 5370bb20842d70571e18af0e8f1d70099f5178a8 Mon Sep 17 00:00:00 2001 From: Andrzej Wawrzyniak Date: Mon, 2 Sep 2019 12:00:02 +0200 Subject: [PATCH] SKYHELIX-653 | Metrics interface and request proxy decorator --- src/Toper/Client.php | 42 ++++++++++++++++++----- src/Toper/MetricsInterface.php | 8 +++++ src/Toper/ProxyDecoratorInterface.php | 19 +++++++++++ src/Toper/Request.php | 35 +++++++++++++++---- tests/unit/Toper/RequestTest.php | 48 +++++++++++++++++++++++++-- 5 files changed, 136 insertions(+), 16 deletions(-) create mode 100644 src/Toper/MetricsInterface.php create mode 100644 src/Toper/ProxyDecoratorInterface.php diff --git a/src/Toper/Client.php b/src/Toper/Client.php index 009d8b1..319ca07 100644 --- a/src/Toper/Client.php +++ b/src/Toper/Client.php @@ -15,15 +15,31 @@ class Client implements ClientInterface private $guzzleClientFactory; /** - * @param HostPoolProviderInterface $hostPoolProvider - * @param GuzzleClientFactoryInterface $guzzleClientFactory + * @var MetricsInterface + */ + private $metrics; + + /** + * @var ProxyDecoratorInterface + */ + private $proxyDecorator; + + /** + * @param HostPoolProviderInterface $hostPoolProvider + * @param GuzzleClientFactoryInterface $guzzleClientFactory + * @param MetricsInterface|null $metrics + * @param ProxyDecoratorInterface $proxyDecorator */ public function __construct( HostPoolProviderInterface $hostPoolProvider, - GuzzleClientFactoryInterface $guzzleClientFactory + GuzzleClientFactoryInterface $guzzleClientFactory, + MetricsInterface $metrics = null, + ProxyDecoratorInterface $proxyDecorator = null ) { $this->hostPoolProvider = $hostPoolProvider; $this->guzzleClientFactory = $guzzleClientFactory; + $this->metrics = $metrics; + $this->proxyDecorator = $proxyDecorator; } /** @@ -39,7 +55,9 @@ public function get($url, array $binds = array()) $url, $binds, $this->hostPoolProvider->get(), - $this->guzzleClientFactory->create() + $this->guzzleClientFactory->create(), + $this->metrics, + $this->proxyDecorator ); } @@ -56,7 +74,9 @@ public function post($url, array $binds = array()) $url, $binds, $this->hostPoolProvider->get(), - $this->guzzleClientFactory->create() + $this->guzzleClientFactory->create(), + $this->metrics, + $this->proxyDecorator ); } @@ -73,7 +93,9 @@ public function patch($url, array $binds = array()) $url, $binds, $this->hostPoolProvider->get(), - $this->guzzleClientFactory->create() + $this->guzzleClientFactory->create(), + $this->metrics, + $this->proxyDecorator ); } @@ -90,7 +112,9 @@ public function put($url, array $binds = array()) $url, $binds, $this->hostPoolProvider->get(), - $this->guzzleClientFactory->create() + $this->guzzleClientFactory->create(), + $this->metrics, + $this->proxyDecorator ); } @@ -107,7 +131,9 @@ public function delete($url, array $binds = array()) $url, $binds, $this->hostPoolProvider->get(), - $this->guzzleClientFactory->create() + $this->guzzleClientFactory->create(), + $this->metrics, + $this->proxyDecorator ); } } diff --git a/src/Toper/MetricsInterface.php b/src/Toper/MetricsInterface.php new file mode 100644 index 0000000..554e327 --- /dev/null +++ b/src/Toper/MetricsInterface.php @@ -0,0 +1,8 @@ +method = $method; $this->url = $url; $this->binds = $binds; $this->hostPool = $hostPool; $this->guzzleClient = $guzzleClient; + $this->metrics = $metrics; + $this->proxyDecorator = $proxyDecorator; } /** @@ -142,6 +158,13 @@ public function send() $this->updateQueryParams($guzzleRequest); + if (!is_null($this->proxyDecorator)) { + $this->proxyDecorator->decorate($guzzleRequest); + } + if (!is_null($this->metrics)) { + $this->metrics->increment($this->method, $this->url); + } + return new Response($guzzleRequest->send()); } catch (ClientErrorResponseException $e) { return new Response($e->getResponse()); diff --git a/tests/unit/Toper/RequestTest.php b/tests/unit/Toper/RequestTest.php index 87155c9..e6f8d8c 100644 --- a/tests/unit/Toper/RequestTest.php +++ b/tests/unit/Toper/RequestTest.php @@ -53,6 +53,43 @@ public function shouldSendRequest() $this->assertEquals($guzzleResponse->getBody(true), $response->getBody()); } + /** + * @test + */ + public function shouldSendMetricsAndDecorateRequestBeforeSend() + { + $guzzleClient = $this->createGuzzleClientMock(); + + $guzzleResponse = new GuzzleResponse(200, array(), 'ok'); + + $guzzleRequest = $this->createGuzzleRequest($guzzleResponse); + + $this->prepareGuzzleClientMock($guzzleClient, $guzzleRequest); + + $this->returnValue($guzzleResponse); + + $metricsInterfaceMock = $this->getMock('Toper\MetricsInterface', array('increment')); + $metricsInterfaceMock->expects($this->once())->method('increment'); + + $proxyDecoratorInterfaceMock = $this->getMock( + 'Toper\ProxyDecoratorInterface', + array('decorate', 'getBaseUrl') + ); + $proxyDecoratorInterfaceMock->expects($this->once())->method('decorate'); + + $instance = $this->createInstance( + Request::GET, + array(), + $guzzleClient, + $metricsInterfaceMock, + $proxyDecoratorInterfaceMock + ); + $response = $instance->send(); + + $this->assertEquals($guzzleResponse->getStatusCode(), $response->getStatusCode()); + $this->assertEquals($guzzleResponse->getBody(true), $response->getBody()); + } + /** * @test */ @@ -376,19 +413,26 @@ public function shouldReturnBinds() * @param array $binds * @param GuzzleClientInterface $guzzleClient * + * @param null $metrics + * @param null $proxyDecorator * @return Request */ private function createInstance( $method, array $binds, - GuzzleClientInterface $guzzleClient + GuzzleClientInterface $guzzleClient, + $metrics = null, + $proxyDecorator = null ) { + return new Request( $method, self::URL, $binds, $this->hostPool, - $guzzleClient + $guzzleClient, + $metrics, + $proxyDecorator ); }