Skip to content

Instantly share code, notes, and snippets.

@msbrime msbrime/ApiResponse.php
Last active Jul 25, 2017

Embed
What would you like to do?
A wrapper for guzzle http responses from an API
<?php
namespace App\Library\Response;
use GuzzleHttp\Message\Response;
class ApiResponse
{
/*
* The status code of a response represented as a single integer
* denoting the general status of the response
*
* @var integer
*/
private $level;
/*
* The decoded data received or null if the
* request was unsuccessful
*
* @var object|null
*/
private $data = null;
/*
* The original response object
*
* @var \GuzzleHttp\Message\ResponseInterface
*/
protected $originalResponse;
/*
* The field/key in the decoded response data that corresponds to
* the actual data returned
*
* @var string
*/
private $responseDataField;
/**
* Create a new ApiResponse
*
* @param \GuzzleHttp\Message\ResponseInterface $response
* @param string $responseDataField
*/
public function __construct(
\GuzzleHttp\Message\ResponseInterface $response,
$responseDataField = 'data'
) {
$this->originalResponse = $response;
$this->reasonPhrase = $response->getReasonPhrase();
$this->level = floor($response->getStatusCode() / 100);
$this->headers = $response->getHeaders();
$this->responseDataField = $responseDataField;
if ($this->level == 2) {
if ($this->isJsonContent($response)) {
try{
$this->data = $response->json(["object" => true]);
}
catch(\Exception $e){
$this->data = null;
}
}
}
}
/**
* Get the data property
*
* @return object the data property
*/
public function getData()
{
return $this->data;
}
/**
* Get the level of the response
*
* @return integer the level
*/
public function getLevel()
{
return $this->level;
}
/**
*
* @return \GuzzleHttp\Message\Response
*/
public function getOriginalResponse()
{
return $this->originalResponse;
}
/**
* Get the status code
*
* @return integer the status code
*/
public function getStatusCode()
{
return $this->originalResponse->getStatusCode();
}
/**
* Get the response body
*
* @return mixed|string the response body
*/
public function getBody()
{
return $this->originalResponse->getBody()->getContents();
}
/**
* Get the response headers
*
* @return string|array the headers
*/
public function getHeaders()
{
return $this->originalResponse->getHeaders();
}
/**
* Get the response message
*
* @return string the response phrase
*/
private function getReasonPhrase()
{
return $this->originalResponse->getReasonPhrase();
}
/**
* Determine whether or not the payload returned contains
* JSON content
*
* @param \GuzzleHttp\Message\ResponseInterface $response
* @return boolean
*/
private function isJsonContent($response)
{
return $response->getHeader("Content-type") == "application/json";
}
public static function successResponse(ApiResponse $response,Callable $callback = null)
{
if($callback){
return $response->level == 2 && $callback($response);
}
return $response->level == 2;
}
/**
* Determine whether a group of responses were all success responses
*
* @param array $responses
* @return boolean
*/
public static function successResponses($responses,Callable $callback = null)
{
$success = true;
foreach ($responses as $response) {
$success = $success && self::successResponse($response,$callback);
}
return $success;
}
/**
* Handle dynamic method calls into the class
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
if ($this->data) {
return $this->data->{$name};
}
return null;
}
/**
* Handle dynamic property calls into the class
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if ($this->data) {
return $this->data->{$this->responseDataField}->{$name};
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.