Skip to content

Commit

Permalink
Pipe Guzzle responses through a response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
colindecarlo committed Nov 21, 2015
1 parent e0cf497 commit 5d7524a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/ResponseHandler/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Uhura\ResponseHandler;

class Json
{
public function handle($response)
{
return json_decode($response->getBody()->getContents(), true);
}
}
11 changes: 11 additions & 0 deletions src/ResponseHandler/Passthru.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Uhura\ResponseHandler;

class Passthru
{
public function handle($response)
{
return $response;
}
}
11 changes: 10 additions & 1 deletion src/Uhura.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Uhura
{
private $api;
private $http;
private $responseHandler;

private $resource = '';
private $token = null;
Expand All @@ -17,6 +18,7 @@ public function __construct($api)
{
$this->api = $api;
$this->http = new Client(['base_uri' => $this->api]);
$this->responseHandler = new ResponseHandler\Passthru;
}

public static function test($api)
Expand All @@ -31,6 +33,11 @@ public static function test($api)
return $uhura;
}

public function useResponseHandler($handler)
{
$this->responseHandler = $handler;
}

public function useBasicAuthentication($username, $password)
{
$token = base64_encode(sprintf("%s:%s", $username, $password));
Expand Down Expand Up @@ -80,7 +87,9 @@ private function request($method, $payload = null)
'form_params' => $payload
]);

return $this->http->request($method, $this->resource, $options);
return $this->responseHandler->handle(
$this->http->request($method, $this->resource, $options)
);
}

public function __get($name)
Expand Down
19 changes: 18 additions & 1 deletion tests/UhuraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function test_that_uhura_sends_a_delete_request_to_the_correct_url_when_d
$this->assertEquals('https://example.com/users/1', $handler->getLastRequest()->getUri());
}

public function test_that_uhura_sends_an_authorization_header_when_sending_authenticated_requests()
public function test_that_uhura_sends_an_basic_authorization_header_when_sending_authenticated_requests_using_basic_authentication()
{
$handler = $this->uhura->getHttp()->getConfig('handler');
$handler->append(new Response);
Expand All @@ -117,4 +117,21 @@ public function test_that_uhura_sends_an_authorization_header_when_sending_authe
$handler->getLastRequest()->getHeader('Authorization')[0]
);
}

public function test_that_uhura_returns_a_json_decode_response_body_when_using_the_json_response_handler()
{
$this->uhura->useResponseHandler(new \Uhura\ResponseHandler\Json);

$expectedResponse = [
'status' => 'ok',
'data' => [
'foo' => 'bar'
]
];

$handler = $this->uhura->getHttp()->getConfig('handler');
$handler->append(new Response(200, [], json_encode($expectedResponse)));

$this->assertEquals($this->uhura->get(), $expectedResponse);
}
}

0 comments on commit 5d7524a

Please sign in to comment.