From 6925d5a23b174ea0b19fd08d2a17f5e30296be3d Mon Sep 17 00:00:00 2001 From: Kyle Essex Date: Fri, 2 Jun 2023 11:32:29 +0100 Subject: [PATCH] Add support for v2 rmccue/requests (#5) --- composer.json | 2 +- src/Connections/Requests2Connection.php | 190 ++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/Connections/Requests2Connection.php diff --git a/composer.json b/composer.json index 6234020..71caa82 100755 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require-dev": { }, "suggest": { - "rmccue/requests": ">=1.0 to use the RequestsConnection", + "rmccue/requests": "^=1.0 to use the RequestsConnection, or ^=2.0 to use the Requests2Connection", "guzzlehttp/guzzle": "5.0 to use the Guzzle5Connection, or 6.0 to use the Guzzle6Connection" }, "autoload": { diff --git a/src/Connections/Requests2Connection.php b/src/Connections/Requests2Connection.php new file mode 100644 index 0000000..9e9ec03 --- /dev/null +++ b/src/Connections/Requests2Connection.php @@ -0,0 +1,190 @@ +_timeout = $timout; + return $this; + } + + /** + * @inheritDoc + */ + public function load(IApiRequest $request) + { + $req = $request->getRequestDetail(); + + try + { + $headers = $this->_buildHeaders($req); + $data = $this->_buildData($req); + if(!$data || (is_array($data) && empty(array_filter($data)))) + { + // Prevent 411 errors from some servers + $headers['Content-Length'] = 0; + } + + $response = Requests::request( + $req->getUrl(), + $headers, + $data, + $req->getMethod(), + array_merge(['timeout' => $this->_timeout], (array)$req->getOptions()) + ); + + $result = $this->_getResult($response); + + $request->setRawResult($result); + return $request; + } + catch(\Exception $e) + { + $this->_handleException($e); + } + } + + protected function _handleException(\Exception $e) + { + try + { + throw $e; + } + catch(Exception $e) + { + if(is_resource($e->getData())) + { + $errNo = curl_errno($e->getData()); + switch($errNo) + { + case 7: + throw new ServiceUnavailableException($e->getMessage(), 503, $e); + case 28: + throw new RequestTimeoutException($e->getMessage(), 408, $e); + } + } + } + catch(ApiException $e) + { + throw $e; + } + throw new ServerApiException($e->getMessage(), 500, $e); + } + + /** + * @inheritDoc + */ + public function batchLoad($requests) + { + /** @var IApiRequest[] $originals */ + $originals = []; + $batchRequests = []; + foreach($requests as $i => $request) + { + if($request instanceof IApiRequest) + { + $originals[$i] = $request; + $reqDet = $request->getRequestDetail(); + + $batchReq = [ + 'url' => $reqDet->getUrl(), + 'headers' => $this->_buildHeaders($reqDet), + 'data' => $this->_buildData($reqDet), + 'type' => $reqDet->getMethod(), + 'options' => $reqDet->getOptions(), + 'cookies' => [], + ]; + $batchRequests[$i] = $batchReq; + } + } + + try + { + $responses = Requests::request_multiple($batchRequests); + + foreach($responses as $id => $response) + { + $originals[$id]->setRawResult($this->_getResult($response)); + } + + return $this; + } + catch(\Exception $e) + { + $this->_handleException($e); + } + } + + /** + * @param Response $response + * + * @return IApiResult + */ + protected function _getResult(Response $response) + { + $result = new ApiResult(); + $result->setStatusCode($response->status_code); + $callId = $response->headers->getValues('X-Call-Id'); + if(!empty($callId)) + { + $result->setCallId(reset($callId)); + } + + $decoded = json_decode($response->body); + if(isset($decoded->meta) && isset($decoded->data) + && isset($decoded->meta->code) + && $decoded->meta->code == $response->status_code + ) + { + $meta = $decoded->meta; + $data = $decoded->data; + if(isset($meta->message)) + { + $result->setStatusMessage($meta->message); + } + $result->setContent(json_encode($data)); + } + else + { + $result->setContent($response->body); + } + + $cookies = $response->cookies; + if(!is_array($cookies)) + { + if($cookies instanceof \IteratorAggregate) + { + $cookies = (array)$cookies->getIterator(); + } + } + $result->setCookies($cookies); + $headers = $response->headers; + if(!is_array($headers)) + { + if($headers instanceof \IteratorAggregate) + { + $headers = (array)$headers->getIterator(); + } + } + $result->setHeaders($headers); + return $result; + } +}