<?php
namespace Mtc\Core;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mtc\Shop\TitanPos\Models\EposDebug;
class RestApi {
/*
* Guzzle client GuzzleHttp\Client;
*/
private $client = null;
/*
* Options for Guzzle client
*/
private $defaultOptions = [
'timeout' => 60,
'connect_timeout' => 5,
];
/*
* Data that would be present on every request like auth details
*/
private $defaultBody = [];
/*
* Final request data excluding options
*/
private $requestBody = [];
/*
* Final request options excluding data
*/
private $requestOptions = [];
/*
* Should we send this as json request
*/
private $isJsonRequest = false;
/*
* Automatically reset api body/options on each end of the call
* - Clears custom body, options, json flags etc.
* - Does not reset default body/options
*/
private $autoReset = true;
/*
* Last requests response
*/
private $response = null;
/**
* RestApi constructor.
* @param $endpointBase
* @param array $guzzleOptions
* @param array $defaultBody
* @param bool $json
* @param bool $autoReset
*/
public function __construct
($endpointBase, array $guzzleOptions = [], array $defaultBody = [], $json = false, $autoReset = true)
{
$this->setEndpointBase($endpointBase);
$this->reset($guzzleOptions);
$this->setDefaultBody($defaultBody);
$this->isJsonRequest = (bool) $json;
}
/**
* @param string $endpointBase
*/
private function setEndpointBase($endpointBase)
{
$this->defaultOptions['base_uri'] = strrev($endpointBase)[0] == '/' ?
$endpointBase : $endpointBase . '/';
}
/**
* @param string $defaultBody
*/
private function setDefaultBody($defaultBody)
{
$this->defaultBody = $defaultBody;
}
/*
* Reset Guzzle Client and API object to basic state
* Will retain only defaultOptions + options passed to this method
* - Guzzle 6+ has immutable client (this is why we need to build new one)
*/
public function reset(array $guzzleOptions = [], $json = null)
{
$this->requestBody = $this->requestOptions = [];
$this->options($guzzleOptions);
$options = array_merge($this->defaultOptions, $this->requestOptions);
$this->client = new Client($options);
$this->isJsonRequest = (bool) $json;
}
return $this;
}
/*
* Add request body to Guzzle Client
* Also acts as json body if json has been enabled on the reset or initialization call
*/
public function body
(array $body = [])
{
$this->requestBody = $body;
return $this;
}
/*
* Add Guzzle Client options
* - form_params is stripped from options use body() method for that
* - base_uri is stripped for security, initialize another api object for different base_uri
*
* @see http://docs.guzzlephp.org/en/stable/request-options.html
*/
public function options
(array $options = [])
{
if (isset($options['form_params'])) {
unset($options['form_params']);
}
if (isset($options['base_uri'])) {
unset($options['base_uri']);
}
$this->requestOptions = $options;
return $this;
}
/**
* Retrieve latest performed api calls response
* - bodyOnly (true) | Set to false to retrieve whole object of response
* - decodeJson (false) | Set to true to decode json encoded response body
* only available if bodyOnly = true
*
* @param bool $bodyOnly
* @param bool $decodeJson
* @return Response|mixed|null
*/
public function getResponse($bodyOnly = true, $decodeJson = false)
{
$contents = $this->response;
if ($bodyOnly === true) {
if ($decodeJson === true) {
return \GuzzleHttp\
json_decode($contents->getBody()->getContents(), true);
}
return $contents->getBody()->getContents();
}
return $contents;
}
/**
* Rely any post, get, put, update etc. calls to the
* We will populate this with our options, body contents and such
*
* @param $method
* @param $arguments
* @return mixed|null
*/
public function __call($method, $arguments)
{
$body = array_merge($this->defaultBody, $this->requestBody);
$options = array_merge($this->defaultOptions, $this->requestOptions);;
if ($this->isJsonRequest) {
$options['json'] = $body;
} else {
$options['form_params'] = $body;
}
$arguments[] = $options;
// Reset api at this point
if ($this->autoReset === true) {
}
return $this->response;
}
public function saveDebugInfo
(array $debug_info = [])
{
if (count($debug_info) == 0) {
return false;
}
// Log debug info under any response type
EposDebug::create($debug_info);
return true;
}
}