Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(guzzle): upgrade to version 7.5 #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}
Expand Down
43 changes: 32 additions & 11 deletions src/Foxy/FoxyClient/FoxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
{
Expand Down