Skip to content

Add http client router feature #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Add a flexible http client providing both contract, and only emulating what's necessary
- HTTP Client Router: route requests to underlying clients


## 1.0.0 - 2016-01-27
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require": {
"php": ">=5.4",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0"
"php-http/message-factory": "^1.0",
"php-http/message": "^1.2"
},
"require-dev": {
"phpspec/phpspec": "^2.4",
Expand Down
63 changes: 63 additions & 0 deletions spec/HttpClientRouterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace spec\Http\Client\Common;

use Http\Message\RequestMatcher;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class HttpClientRouterSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Common\HttpClientRouter');
}

function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

function it_is_an_async_http_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

function it_send_request(RequestMatcher $matcher, HttpClient $client, RequestInterface $request, ResponseInterface $response)
{
$this->addClient($client, $matcher);
$matcher->matches($request)->willReturn(true);
$client->sendRequest($request)->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}

function it_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request, Promise $promise)
{
$this->addClient($client, $matcher);
$matcher->matches($request)->willReturn(true);
$client->sendAsyncRequest($request)->willReturn($promise);

$this->sendAsyncRequest($request)->shouldReturn($promise);
}

function it_throw_exception_on_send_request(RequestMatcher $matcher, HttpClient $client, RequestInterface $request)
{
$this->addClient($client, $matcher);
$matcher->matches($request)->willReturn(false);

$this->shouldThrow('Http\Client\Exception\RequestException')->duringSendRequest($request);
}

function it_throw_exception_on_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request)
{
$this->addClient($client, $matcher);
$matcher->matches($request)->willReturn(false);

$this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request);
}
}
74 changes: 74 additions & 0 deletions src/HttpClientRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Http\Client\Common;

use Http\Client\Exception\RequestException;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\RequestMatcher;
use Psr\Http\Message\RequestInterface;

/**
* Route a request to a specific client in the stack based using a RequestMatcher.
*
* @author Joel Wurtz <[email protected]>
*/
final class HttpClientRouter implements HttpClient, HttpAsyncClient
{
/**
* @var array
*/
private $clients = [];

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
{
$client = $this->chooseHttpClient($request);

return $client->sendRequest($request);
}

/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$client = $this->chooseHttpClient($request);

return $client->sendAsyncRequest($request);
}

/**
* Add a client to the router.
*
* @param HttpClient|HttpAsyncClient $client
* @param RequestMatcher $requestMatcher
*/
public function addClient($client, RequestMatcher $requestMatcher)
{
$this->clients[] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could find a better way to store these. Although it's an internal detail, it feels a little but weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a member client and a member matcher would make more sense.

re-reading this i am confused about this code. we have a bunch of matcher, but only one single client? if none of the matchers match, we throw an exception, if any matches we always use the same client. that is not what i would expect from a router. i would have expected this to be an array of matcher, client and if a matcher matches, that client is used...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code also indicates that this was originally the idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry. i misread the code. it does what i would expect. i don't see how we could store these differently. its a list of pairs [matcher, client] - i would not know how to store that differently. you could try some spl hashmap that can use objects as key if that exists, but i think its quite straightforward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would have expected this to be an array of matcher, client and if a matcher matches, that client is used...

I think this is what's happening right now. Isn't it?

That one client is only used as a decorator in order to correctly emulate a client if necessary.

'matcher' => $requestMatcher,
'client' => new FlexibleHttpClient($client),
];
}

/**
* Choose an HTTP client given a specific request.
*
* @param RequestInterface $request
*
* @return HttpClient|HttpAsyncClient
*/
protected function chooseHttpClient(RequestInterface $request)
{
foreach ($this->clients as $client) {
if ($client['matcher']->matches($request)) {
return $client['client'];
}
}

throw new RequestException('No client found for the specified request', $request);
}
}