From 780643b0345c2ad11667a5a579bc954321a0234e Mon Sep 17 00:00:00 2001 From: Daniel Medeiros Date: Thu, 30 Nov 2023 19:55:17 -0300 Subject: [PATCH 1/2] Update ApiRequester.php --- src/ApiRequester.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ApiRequester.php b/src/ApiRequester.php index e4240e8..949ed84 100644 --- a/src/ApiRequester.php +++ b/src/ApiRequester.php @@ -78,8 +78,7 @@ public function response(ResponseInterface $response) $data = $decoded; if (!empty($decoded)) { - reset($decoded); - $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors. + $data = is_array($decoded) ? reset($decoded) : $decoded; } $this->checkRateLimit($response) From 89dbefa451e363f7db30da151d8f97dc20fd43ec Mon Sep 17 00:00:00 2001 From: Daniel Medeiros Date: Thu, 30 Nov 2023 21:00:24 -0300 Subject: [PATCH 2/2] Update ApiRequester.php --- src/ApiRequester.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/ApiRequester.php b/src/ApiRequester.php index 949ed84..674b746 100644 --- a/src/ApiRequester.php +++ b/src/ApiRequester.php @@ -69,24 +69,28 @@ public function request($method, $endpoint, array $options = []) * @throws \Vindi\Exceptions\RequestException */ public function response(ResponseInterface $response) - { - $this->lastResponse = $response; - - $content = $response->getBody()->getContents(); +{ + $this->lastResponse = $response; - $decoded = json_decode($content); // parse as object - $data = $decoded; + $content = $response->getBody()->getContents(); - if (!empty($decoded)) { - $data = is_array($decoded) ? reset($decoded) : $decoded; - } + // Decodifica como array ao invés de objeto + $decoded = json_decode($content, true); - $this->checkRateLimit($response) - ->checkForErrors($response, $data); + $data = $decoded; - return $data; + if (!empty($decoded)) { + // Se $decoded é um array, obtém o primeiro elemento + $data = is_array($decoded) ? reset($decoded) : $decoded; } + $this->checkRateLimit($response) + ->checkForErrors($response, $data); + + return $data; +} + + /** * @param ResponseInterface $response *