diff --git a/src/Abstracts/AbstractApi.php b/src/Abstracts/AbstractApi.php index 6e4555e..8c473a0 100644 --- a/src/Abstracts/AbstractApi.php +++ b/src/Abstracts/AbstractApi.php @@ -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; } /** diff --git a/src/Abstracts/AbstractEndpoint.php b/src/Abstracts/AbstractEndpoint.php index cb8d357..9e93068 100644 --- a/src/Abstracts/AbstractEndpoint.php +++ b/src/Abstracts/AbstractEndpoint.php @@ -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; @@ -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) { @@ -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; } } diff --git a/src/ApiRequest.php b/src/ApiRequest.php index b493801..e72c355 100644 --- a/src/ApiRequest.php +++ b/src/ApiRequest.php @@ -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; } /** @@ -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(); diff --git a/src/Interfaces/ApiRequestInterface.php b/src/Interfaces/ApiRequestInterface.php index 6626724..e63659c 100644 --- a/src/Interfaces/ApiRequestInterface.php +++ b/src/Interfaces/ApiRequestInterface.php @@ -1,6 +1,8 @@