Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from detailnet/fix/guzzle-version
Browse files Browse the repository at this point in the history
Fixed mixup of Guzzle versions
  • Loading branch information
Ivan Wolf authored May 13, 2017
2 parents a4a9471 + 037a864 commit 33c3e00
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 61 deletions.
14 changes: 3 additions & 11 deletions src/Jumbo/Client/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Jumbo\Client;

use Psr\Http\Message\RequestInterface as PsrRequest;
use Psr\Http\Message\ResponseInterface as PsrResponse;

use Guzzle\Http\Message\Response as HttpResponse;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\DescriptionInterface as ServiceDescription;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request as PsrRequest;
use GuzzleHttp\Psr7\Response as PsrResponse;

use Jumbo\Client\Response\Response;

Expand Down Expand Up @@ -43,12 +41,6 @@ public function __invoke(PsrResponse $response, PsrRequest $request, CommandInte
throw RequestException::create($request, $response);
}

$httpResponse = new HttpResponse(
$response->getStatusCode(),
$response->getHeaders(),
$response->getBody()
);

$name = $command->getName();
$operation = $this->description->getOperation($name);

Expand All @@ -74,6 +66,6 @@ public function __invoke(PsrResponse $response, PsrRequest $request, CommandInte

/** @var Response $responseClass */

return $responseClass::fromOperation($operation, $httpResponse);
return $responseClass::fromOperation($operation, $response);
}
}
21 changes: 1 addition & 20 deletions src/Jumbo/Client/JumboClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
use GuzzleHttp\Command\Guzzle\GuzzleClient as ServiceClient;

use Jumbo\Client\Exception;
use Jumbo\Client\Subscriber;

abstract class JumboClient extends ServiceClient
{
const CLIENT_VERSION = '0.1.0';
const CLIENT_VERSION = '0.1.1';

const OPTION_APP_ID = 'app_id';
const OPTION_APP_KEY = 'app_key';
Expand Down Expand Up @@ -88,24 +87,6 @@ public static function factory($options = array())
return $client;
}

// /**
// * @param HttpClientInterface $client
// * @param ServiceDescriptionInterface $description
// */
// public function __construct(
// HttpClientInterface $client,
// ServiceDescriptionInterface $description
// ) {
// $config = array(
// 'process' => false, // Don't use Guzzle Service's processing (we're rolling our own...)
// );
//
// parent::__construct($client, $description, $config);
//
// $emitter = $this->getEmitter();
// $emitter->attach(new Subscriber\Command\ProcessResponse($description));
// }

/**
* @return string|null
*/
Expand Down
42 changes: 34 additions & 8 deletions src/Jumbo/Client/Response/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Jumbo\Client\Response;

use Guzzle\Common\Exception as GuzzleException;
use Guzzle\Http\Message\Response as HttpResponse;
use GuzzleHttp\Psr7\Response as PsrResponse;

use Jumbo\Client\Exception;

abstract class BaseResponse implements
Response
{
/**
* @var HttpResponse
* @var PsrResponse
*/
protected $response;

Expand All @@ -21,16 +20,16 @@ abstract class BaseResponse implements
protected $data;

/**
* @param HttpResponse $response
* @param PsrResponse $response
*/
public function __construct(HttpResponse $response)
public function __construct(PsrResponse $response)
{
$this->response = $response;
$this->data = $this->extractData();
}

/**
* @return HttpResponse
* @return PsrResponse
*/
public function getHttpResponse()
{
Expand Down Expand Up @@ -59,15 +58,42 @@ protected function getData()
private function extractData()
{
try {
$data = $this->getHttpResponse()->json();
$data = $this->decodeJson($this->getHttpResponse()->getBody());

return is_array($data) ? $data : array();
} catch (GuzzleException\RuntimeException $e) {
} catch (\Exception $e) {
throw new Exception\RuntimeException(
sprintf('Failed extract data from HTTP response: %s', $e->getMessage()),
$e->getCode(),
$e
);
}
}

/**
* @param string $value
* @return array
*/
private function decodeJson($value)
{
$data = json_decode($value, true);

if ($data === false) {
$error = json_last_error();

if ($error !== JSON_ERROR_NONE) {
$message = json_last_error_msg();

if ($message === false) {
$message = 'Unknown error';
}

throw new Exception\RuntimeException(
sprintf('Unable to decode JSON: %s', $message)
);
}
}

return $data;
}
}
10 changes: 5 additions & 5 deletions src/Jumbo/Client/Response/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use ArrayIterator;

use Guzzle\Http\Message\Response as HttpResponse;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Psr7\Response as PsrResponse;

use Jumbo\Client\Exception;

Expand All @@ -24,10 +24,10 @@ class ListResponse extends BaseResponse implements

/**
* @param Operation $operation
* @param HttpResponse $response
* @param PsrResponse $response
* @return ListResponse
*/
public static function fromOperation(Operation $operation, HttpResponse $response)
public static function fromOperation(Operation $operation, PsrResponse $response)
{
$operationConfig = $operation->toArray();

Expand All @@ -44,10 +44,10 @@ public static function fromOperation(Operation $operation, HttpResponse $respons
}

/**
* @param HttpResponse $response
* @param PsrResponse $response
* @param string $dataRoot
*/
public function __construct(HttpResponse $response, $dataRoot)
public function __construct(PsrResponse $response, $dataRoot)
{
parent::__construct($response);

Expand Down
20 changes: 7 additions & 13 deletions src/Jumbo/Client/Response/ResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Jumbo\Client\Response;

use Guzzle\Http\Message\Response as HttpResponse;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Psr7\Response as PsrResponse;

class ResourceResponse extends BaseResponse
{
Expand All @@ -14,29 +14,23 @@ class ResourceResponse extends BaseResponse

/**
* @param Operation $operation
* @param HttpResponse $response
* @param PsrResponse $response
* @return ResourceResponse
*/
public static function fromOperation(Operation $operation, HttpResponse $response)
public static function fromOperation(Operation $operation, PsrResponse $response)
{
return new static($response);
}

/**
* @param HttpResponse $response
*/
public function __construct(HttpResponse $response)
{
parent::__construct($response);

$this->resource = new Resource($this->getData());
}

/**
* @return \Jumbo\Client\Response\Resource
*/
public function getResource()
{
if ($this->resource === null) {
$this->resource = new Resource($this->getData());
}

return $this->resource;
}
}
8 changes: 4 additions & 4 deletions src/Jumbo/Client/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace Jumbo\Client\Response;

use Guzzle\Http\Message\Response as HttpResponse;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Psr7\Response as PsrResponse;

interface Response
{
/**
* @param Operation $operation
* @param HttpResponse $response
* @param PsrResponse $response
* @return Response
*/
public static function fromOperation(Operation $operation, HttpResponse $response);
public static function fromOperation(Operation $operation, PsrResponse $response);

/**
* @return HttpResponse
* @return PsrResponse
*/
public function getHttpResponse();

Expand Down

0 comments on commit 33c3e00

Please sign in to comment.