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

fix: handle edge case where no response body is recieved #37

Merged
merged 1 commit into from
Jun 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/Api/Services/BaseGeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Services/IPSevenEx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading