Skip to content

Commit

Permalink
Adds json parsing middleware for better response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Jan 12, 2016
1 parent 23c1aab commit 3116b5c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Shutterstock\Api;

use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\PromiseInterface as Promise;
use Psr\Http\Message\ResponseInterface as Response;

Expand All @@ -18,9 +21,17 @@ class Client
*/
public function __construct($clientId, $clientSecret)
{
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapResponse(function (Response $response) {
$jsonStream = new JsonStream($response->getBody());
return $response->withBody($jsonStream);
}));

$guzzle = new Guzzle([
'base_uri' => 'https://api.shutterstock.com/v2/',
'auth' => [$clientId, $clientSecret],
'handler' => $stack,
]);
$this->guzzle = $guzzle;
}
Expand Down
25 changes: 25 additions & 0 deletions src/JsonStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Shutterstock\Api;

use GuzzleHttp\Psr7\StreamDecoratorTrait;
use JsonSerializable;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

class JsonStream implements StreamInterface, JsonSerializable
{

use StreamDecoratorTrait;

public function jsonSerialize()
{
$contents = (string) $this->getContents();
$decodedContents = json_decode($contents, true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new RuntimeException('Error trying to decode response: ' . json_last_error_msg());
}

return $decodedContents;
}
}
7 changes: 1 addition & 6 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@ public function testIsInstanceOfClient()

public function testConstructSetsGuzzle()
{
$guzzle = new Guzzle([
'base_uri' => 'https://api.shutterstock.com/v2/',
'auth' => ['test_client_id', 'test_client_secret'],
]);
$client = new Client('test_client_id', 'test_client_secret');
$client = $this->getClient();

$this->assertAttributeInstanceOf(
'GuzzleHttp\Client',
'guzzle',
$client
);
$this->assertAttributeEquals($guzzle, 'guzzle', $client);
}

/**
Expand Down
62 changes: 62 additions & 0 deletions tests/JsonStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Shutterstock\Api;

use GuzzleHttp\Psr7\Stream;
use PHPUnit_Framework_TestCase;

class JsonStreamTest extends PHPUnit_Framework_TestCase
{

public function testIsInstanceOfJsonStream()
{
$jsonStream = $this->getJsonStream('');

$this->assertInstanceOf('Shutterstock\Api\JsonStream', $jsonStream);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $jsonStream);
}

/**
* @dataProvider dataJsonSerialize
*/
public function testJsonSerialize($expectedArray, $string)
{
$jsonStream = $this->getJsonStream($string);
$array = $jsonStream->jsonSerialize();

$this->assertEquals($expectedArray, $array);
}

public function dataJsonSerialize()
{
return [
[
'expectedArray' => ['key' => 'value'],
'string' => '{"key":"value"}',
],
[
'expectedArray' => null,
'string' => '',
],
];
}

/**
* @expectedException RuntimeException
*/
public function testJsonSerializeException()
{
$jsonStream = $this->getJsonStream('words');
$jsonStream->jsonSerialize();
}

protected function getJsonStream($string)
{
$stream = fopen('php://temp', 'r+');
fwrite($stream, $string);
fseek($stream, 0);
$streamObject = new Stream($stream);

return new JsonStream($streamObject);
}
}

0 comments on commit 3116b5c

Please sign in to comment.