diff --git a/composer.json b/composer.json index 154b492..4f0434b 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,8 @@ } ], "require": { - "guzzlehttp/guzzle": "~5.2", - "guzzlehttp/cache-subscriber": "0.1.*@dev", - "php": ">=5.3.3" + "guzzlehttp/guzzle": "^7.5", + "php": "^7.2.5 || ^8.0" }, "autoload": { "psr-0": {"Foxy\\FoxyClient": "src/"} diff --git a/src/Foxy/FoxyClient/FoxyClient.php b/src/Foxy/FoxyClient/FoxyClient.php index cc3f884..e79bdac 100644 --- a/src/Foxy/FoxyClient/FoxyClient.php +++ b/src/Foxy/FoxyClient/FoxyClient.php @@ -190,11 +190,6 @@ public function get($uri = "", $post = null) return $this->go('GET', $uri, $post); } - public function put($uri, $post = null) - { - return $this->go('PUT', $uri, $post); - } - public function post($uri, $post = null) { return $this->go('POST', $uri, $post); @@ -205,6 +200,11 @@ public function patch($uri, $post = null) return $this->go('PATCH', $uri, $post); } + public function put($uri, $post = null) + { + return $this->go('PUT', $uri, $post); + } + public function delete($uri, $post = null) { return $this->go('DELETE', $uri, $post); @@ -241,7 +241,11 @@ private function go($method, $uri, $post, $is_retry = false) if ($method === "GET" && $post !== null) { $guzzle_args['query'] = $post; } elseif ($post !== null) { - $guzzle_args['body'] = $post; + if (is_array($post)) { + $guzzle_args['form_params'] = $post; + } else { + $guzzle_args['body'] = $post; + } } if (!$this->handle_exceptions) { @@ -251,10 +255,10 @@ private function go($method, $uri, $post, $is_retry = false) return $this->processRequest($method, $uri, $post, $guzzle_args, $is_retry); //Catch Errors - http error } catch (\GuzzleHttp\Exception\RequestException $e) { - return array("error_description" => $e->getMessage()); + return $this->handleException($e); //Catch Errors - not JSON } catch (\GuzzleHttp\Exception\ParseException $e) { - return array("error_description" => $e->getMessage()); + return $this->handleException($e); } } } @@ -267,9 +271,8 @@ private function processRequest($method, $uri, $post, $guzzle_args, $is_retry = $guzzle_args['headers']['X-HTTP-Method-Override'] = 'PATCH'; } - $api_request = $this->guzzle->createRequest($method, $uri, $guzzle_args); - $this->last_response = $this->guzzle->send($api_request); - $data = $this->last_response->json(); + $this->last_response = $this->guzzle->request($method, $uri, $guzzle_args); + $data = json_decode($this->last_response->getBody()->getContents(),true); $this->saveLinks($data); if ($this->hasExpiredAccessTokenError($data) && !$this->shouldRefreshToken()) { if (!$is_retry) { @@ -352,6 +355,24 @@ public function getLinks() return $links; } + private function handleException(\Exception $e) + { + $error = array( + "error_description" => $e->getMessage() + ); + + if ($e->hasResponse()) { + $response = $e->getResponse(); + $error = array_merge($error, array( + "error_code" => $response->getStatusCode(), + "response" => (string)$response->getReasonPhrase(), + "error_contents" => (string) $response->getBody()->getContents() + )); + } + + return $error; + } + //Return any errors that exist in the response data. public function getErrors($data) {