Skip to content

Commit

Permalink
Merge pull request #26 from andrzejwaw/SKYHELIX-653
Browse files Browse the repository at this point in the history
SKYHELIX-653 | Metrics interface and request proxy decorator
  • Loading branch information
andrzejwaw authored Sep 26, 2019
2 parents 7b6ec73 + 5370bb2 commit 4cede08
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 16 deletions.
42 changes: 34 additions & 8 deletions src/Toper/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}
}
8 changes: 8 additions & 0 deletions src/Toper/MetricsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Toper;

interface MetricsInterface
{
public function increment($method, $url);
}
19 changes: 19 additions & 0 deletions src/Toper/ProxyDecoratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Toper;

use Guzzle\Http\Message\Request as GuzzleRequest;

interface ProxyDecoratorInterface
{
/**
* @return string
*/
public function getBaseUrl();

/**
* @param GuzzleRequest $request
* @return string
*/
public function decorate(GuzzleRequest $request);
}
35 changes: 29 additions & 6 deletions src/Toper/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,40 @@ class Request
private $headers = array();

/**
* @param string $method
* @param string $url
* @param array $binds
* @param HostPoolInterface $hostPool
* @param GuzzleClientInterface $guzzleClient
* @var MetricsInterface
*/
private $metrics;

/**
* @var ProxyDecoratorInterface
*/
private $proxyDecorator;

/**
* @param string $method
* @param string $url
* @param array $binds
* @param HostPoolInterface $hostPool
* @param GuzzleClientInterface $guzzleClient
* @param MetricsInterface $metrics
* @param ProxyDecoratorInterface $proxyDecorator
*/
public function __construct(
$method,
$url,
array $binds,
HostPoolInterface $hostPool,
GuzzleClientInterface $guzzleClient
GuzzleClientInterface $guzzleClient,
MetricsInterface $metrics = null,
ProxyDecoratorInterface $proxyDecorator = null
) {
$this->method = $method;
$this->url = $url;
$this->binds = $binds;
$this->hostPool = $hostPool;
$this->guzzleClient = $guzzleClient;
$this->metrics = $metrics;
$this->proxyDecorator = $proxyDecorator;
}

/**
Expand Down Expand Up @@ -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());
Expand Down
48 changes: 46 additions & 2 deletions tests/unit/Toper/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
);
}

Expand Down

0 comments on commit 4cede08

Please sign in to comment.