diff --git a/composer.json b/composer.json index 53e05d80..eb059c1e 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "require": { "php": "^8.2", "ext-curl": "^8.2", - "guzzlehttp/guzzle": "^7.0", + "guzzlehttp/guzzle": "^7.0 || ^8.0", "paragonie/halite": "^5.1" }, "require-dev": { diff --git a/lib/HttpClient.php b/lib/HttpClient.php index ad7f94ab..432db628 100644 --- a/lib/HttpClient.php +++ b/lib/HttpClient.php @@ -8,7 +8,9 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\ConnectTimeoutException; use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\ResponseException; use Psr\Http\Message\ResponseInterface; use WorkOS\Exception\ApiException; use WorkOS\Exception\AuthenticationException; @@ -120,17 +122,22 @@ public function request( throw $this->mapTransportException($e); } catch (RequestException $e) { - if ($e->hasResponse()) { + $response = null; + if ($e instanceof ResponseException) { $response = $e->getResponse(); - if ($response !== null) { - $statusCode = $response->getStatusCode(); - if (in_array($statusCode, self::RETRY_STATUS_CODES, true) && $attempt < $maxRetries) { - $this->sleep($attempt, $response->getHeaderLine('Retry-After')); - continue; - } - - throw $this->mapApiException($response, $e); + } elseif (method_exists($e, 'getResponse')) { + // BC layer for guzzle 7 + $response = $e->getResponse(); + } + + if ($response instanceof ResponseInterface) { + $statusCode = $response->getStatusCode(); + if (in_array($statusCode, self::RETRY_STATUS_CODES, true) && $attempt < $maxRetries) { + $this->sleep($attempt, $response->getHeaderLine('Retry-After')); + continue; } + + throw $this->mapApiException($response, $e); } if ($attempt < $maxRetries) { @@ -417,10 +424,17 @@ private function mapTransportException(\Throwable $exception): \Exception private function isTimeoutException(\Throwable $exception): bool { if ($exception instanceof ConnectException || $exception instanceof RequestException) { - $errno = $exception->getHandlerContext()['errno'] ?? null; - if ($errno === 28) { + if ($exception instanceof ConnectTimeoutException) { return true; } + + if (method_exists($exception, 'getHandlerContext')) { + // BC layer for guzzle 7 + $errno = $exception->getHandlerContext()['errno'] ?? null; + if ($errno === 28) { + return true; + } + } } return str_contains(strtolower($exception->getMessage()), 'timed out');