Skip to content

Commit

Permalink
Return all responses as Response object.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarnovanleeuwen committed May 30, 2018
1 parent 4d313df commit f1f20bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Client
{
const VERSION = '0.1.1';
const VERSION = '0.2.0';
const DEFAULT_HASH_ALGORITHM = 'RS256';
const HEADER_API_KEY = 'API-Key';

Expand Down Expand Up @@ -128,11 +128,7 @@ public function send(Request $request): Response
$this->getRequestOptions($request)
);

if (($statusCode = $response->getStatusCode()) >= 200 && $statusCode < 300) {
return new Response($response);
}

throw new RequestException($response->getBody());
return new Response($response);
} catch (ClientException $exception) {
throw new RequestException($exception->getMessage());
}
Expand Down
23 changes: 21 additions & 2 deletions src/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,34 @@ class Response
*/
private $data;

/**
* @var ResponseInterface
*/
private $response;

public function __construct(ResponseInterface $response)
{
if (($this->data = json_decode($response->getBody(), true)) === null) {
throw new ResponseException("Could not decode response");
$this->response = $response;

if (($this->data = json_decode($body = $response->getBody(), true)) === null) {
throw new ResponseException("Could not decode response, expected valid JSON. Raw response: {$body}");
}
}

public function isSuccessful(): bool
{
$statusCode = $this->response->getStatusCode();

return $statusCode >= 200 && $statusCode < 300;
}

public function getData(): array
{
return $this->data;
}

public function getResponse(): ResponseInterface
{
return $this->response;
}
}

0 comments on commit f1f20bc

Please sign in to comment.