From 418ba2e1f994cd4913c39a179d09da466b5d73c3 Mon Sep 17 00:00:00 2001 From: IanM Date: Sat, 15 Jun 2024 07:18:58 +0100 Subject: [PATCH] fix: handle edge case where no response body is recieved --- src/Api/Services/BaseGeoService.php | 4 ++-- src/Api/Services/IPApi.php | 4 ++-- src/Api/Services/IPData.php | 4 ++-- src/Api/Services/IPLocation.php | 4 ++-- src/Api/Services/IPSevenEx.php | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Api/Services/BaseGeoService.php b/src/Api/Services/BaseGeoService.php index 8283244..ee01a08 100644 --- a/src/Api/Services/BaseGeoService.php +++ b/src/Api/Services/BaseGeoService.php @@ -63,7 +63,7 @@ public function get(string $ip): ?ServiceResponse $this->updateRateLimitsFromResponse($response); } - $body = json_decode($response->getBody()); + $body = json_decode($response->getBody()->getContents()); if ($this->hasError($response, $body)) { $this->logger->error("Error detected in response from {$this->host}"); @@ -140,7 +140,7 @@ abstract protected function buildBatchUrl(array $ips, ?string $apiKey): string; abstract protected function getRequestOptions(?string $apiKey, array $ips = null): array; - abstract protected function hasError(ResponseInterface $response, object $body): bool; + abstract protected function hasError(ResponseInterface $response, mixed $body): bool; abstract protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse; diff --git a/src/Api/Services/IPApi.php b/src/Api/Services/IPApi.php index 8e66779..a5bf85c 100644 --- a/src/Api/Services/IPApi.php +++ b/src/Api/Services/IPApi.php @@ -92,11 +92,11 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array ]; } - protected function hasError(ResponseInterface $response, object $body): bool + protected function hasError(ResponseInterface $response, mixed $body): bool { $allowedFailures = ['reserved range', 'private range']; - return $body->status === 'fail' && !in_array($body->message, $allowedFailures); + return $body?->status === 'fail' && !in_array($body->message, $allowedFailures); } protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse diff --git a/src/Api/Services/IPData.php b/src/Api/Services/IPData.php index d5b47ab..33b9f56 100644 --- a/src/Api/Services/IPData.php +++ b/src/Api/Services/IPData.php @@ -82,9 +82,9 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array ]; } - protected function hasError(ResponseInterface $response, object $body): bool + protected function hasError(ResponseInterface $response, mixed $body): bool { - return isset($body->error) || ($response->getStatusCode() >= 400 && !Str::contains($body->message, ['is a reserved IP address'])); + return isset($body?->error) || ($response->getStatusCode() >= 400 && !Str::contains($body?->message, ['is a reserved IP address'])); } protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse diff --git a/src/Api/Services/IPLocation.php b/src/Api/Services/IPLocation.php index 27fd69b..e9548c1 100644 --- a/src/Api/Services/IPLocation.php +++ b/src/Api/Services/IPLocation.php @@ -55,9 +55,9 @@ public function isRateLimited(): bool return false; } - protected function hasError(ResponseInterface $response, object $body): bool + protected function hasError(ResponseInterface $response, mixed $body): bool { - return $body->response_code !== '200'; + return $body?->response_code !== '200'; } protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse diff --git a/src/Api/Services/IPSevenEx.php b/src/Api/Services/IPSevenEx.php index 337e94b..f7ffa14 100644 --- a/src/Api/Services/IPSevenEx.php +++ b/src/Api/Services/IPSevenEx.php @@ -50,7 +50,7 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array ]; } - protected function hasError(ResponseInterface $response, object $body): bool + protected function hasError(ResponseInterface $response, mixed $body): bool { return $response->getStatusCode() > 200; }