Skip to content

Commit

Permalink
Improve compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jul 9, 2018
1 parent 600085b commit b0dd426
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
4 changes: 3 additions & 1 deletion src/Abstracts/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ protected function _processResponse($response)
*/
protected function _createRequest(ApiRequestInterface $request)
{
return $this->_getClient()->requestAsync(
$promise = $this->_getClient()->requestAsync(
$request->getVerb(),
Path::buildUnix($this->getUrl(), $request->getPath())
. $request->getQueryString(),
$this->_makeOptions($request)
);
$request->setPromise($promise);
return $promise;
}

/**
Expand Down
23 changes: 5 additions & 18 deletions src/Abstracts/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ protected function _buildPath($path, ApiPayloadInterface $payload = null)
{
if(stristr($path, ':') && $payload !== null)
{
$find = array_map(
function ($value) { return ':' . $value; },
array_keys($payload->toArray())
);
$find = array_map(function ($value) { return ':' . $value; }, array_keys($payload->toArray()));
return str_replace($find, $payload->toArray(), $path);
}
return $path;
Expand All @@ -75,9 +72,7 @@ function ($value) { return ':' . $value; },
*
* @return ApiRequest
*/
protected function _createRequest(
ApiPayloadInterface $payload = null, $path = null, $verb = null
)
protected function _createRequest(ApiPayloadInterface $payload = null, $path = null, $verb = null)
{
if($path === null)
{
Expand All @@ -96,22 +91,14 @@ protected function _createRequest(
}
else if($verb === HttpVerb::GET)
{
$request = ApiRequest::create(
HttpVerb::GET,
$path,
[],
$payload->toArray()
);
$request = ApiRequest::create(HttpVerb::GET, $path, [], $payload->toArray());
}
else
{
$request = ApiRequest::create(
$verb ?: HttpVerb::POST,
$path,
$payload->toArray()
);
$request = ApiRequest::create($verb ?: HttpVerb::POST, $path, $payload->toArray());
}
$request->setApi($this->getApi());
$this->getApi()->prepareRequest($request);
return $request;
}
}
40 changes: 20 additions & 20 deletions src/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,35 @@ class ApiRequest implements ApiRequestInterface
*/
public function get()
{
$this->_response = $this->getApi()->processRequest($this);
return $this->_response;
}

public function request()
{
$this->_promise = $this->getApi()->prepareRequest($this);
return $this;
}

public function getResponse()
{
if(!$this->_response && $this->_promise)
if(!$this->_response)
{
$this->_response = $this->getApi()->processPreparedRequest($this->_promise);
if($this->_promise)
{
$this->_response = $this->getApi()->processPreparedRequest($this->_promise);
}
else
{
$this->_response = $this->getApi()->processRequest($this);
}
}
return $this->_response;
}

public function setBatchId($batchId)
/**
* @param PromiseInterface $promise
*
* @return $this
*/
public function setPromise(PromiseInterface $promise)
{
$this->_batchId = $batchId;
$this->_promise = $promise;
return $this;
}

public function getBatchId()
public function prepareRequest()
{
return $this->_batchId;
$this->setPromise($this->getApi()->prepareRequest($this));
return $this;
}

/**
Expand All @@ -64,8 +65,7 @@ public function getBatchId()
* @return static
*/
public static function create(
$verb = HttpVerb::POST, $path = '/', $postData = [], $querystring = '',
$requiresAuth = true
$verb = HttpVerb::POST, $path = '/', $postData = [], $querystring = '', $requiresAuth = true
)
{
$request = new static();
Expand Down
4 changes: 4 additions & 0 deletions src/Interfaces/ApiRequestInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Packaged\Api\Interfaces;

use GuzzleHttp\Promise\PromiseInterface;

interface ApiRequestInterface extends ApiAwareInterface
{
const HTTP_GET = 'get';
Expand All @@ -20,4 +22,6 @@ public function getPath();
public function getPostData();

public function getQueryString();

public function setPromise(PromiseInterface $promise);
}

0 comments on commit b0dd426

Please sign in to comment.