apiConfig->endpoint, $queryPath ); $request = $this->requestFactory->buildRequest( HttpMethod::GET, $uri, $query->toArray() ); return $this->callWithRetry($request); } protected function callWithRetry(RequestInterface $request): ApiResponse { $maxAttempts = $this->apiConfig->maxRetries + 1; $lastException = new \RuntimeException("Max attempts '{$maxAttempts}' reached on api call"); for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) { try { return $this->call($request); } catch (ApiCallFailed $e) { $lastException = $e; if ($attempt >= $maxAttempts) { break; } $this->delay($attempt); } catch (ApiErrorException $e) { if ($e->apiError !== ArcadiaGenesisApiErrorCode::ERR_TOO_MANY_REQUESTS) { throw $e; } $lastException = $e; if ($attempt >= $maxAttempts) { break; } $this->delay($attempt); } } throw $lastException; } protected function call(RequestInterface $request): ApiResponse { try { $response = $this->client->sendRequest($request); $this->assertResponseHasExpectedHeaders($response); $body = $this->parseResponseBody($response); $body = $this->assertApiResponseFormIsAsExpected($body); if ($this->apiResponseIsError($body)) { throw ApiErrorException::fromResponseError($body['error']); } return ApiResponse::fromBody($body); } catch (ConnectException $e) { throw new ApiCallFailed( 'Failed to connect to Api backend: ' . $e->getMessage(), $e->getCode(), $e ); } catch (ClientException $e) { throw new ApiCallFailed( 'Failed to call Api backend seeing a ClientException', $e->getCode(), $e ); } catch (ServerException $e) { $response = $e->getResponse(); $body = $this->parseResponseBody($response); if ($this->apiResponseIsError($body)) { throw ApiErrorException::fromResponseError($body['error']); } throw new ApiErrorException( ArcadiaGenesisApiErrorCode::UNKNOWN, $e ); } } /** * @throws \Uwil\ArcadiaGenesisPhpClient\Exception\InvalidApiResponse */ protected function parseResponseBody(ResponseInterface $response): mixed { $bodyContent = $response->getBody()->getContents(); if (! json_validate($bodyContent)) { $jsonErrorMessage = json_last_error_msg(); throw new InvalidApiResponse( "Invalid Json message '{$jsonErrorMessage}'" ); } return json_decode($bodyContent, true); } /** * @throws \Uwil\ArcadiaGenesisPhpClient\Exception\InvalidApiResponse */ protected function assertResponseHasExpectedHeaders(ResponseInterface $response): void { $headerIsJsonContent = in_array( $response->getHeaders()['Content-Type'][0] ?? '', [ 'application/json;charset=UTF-8', 'application/json', ], true ); if (! $headerIsJsonContent) { throw new InvalidApiResponse( 'Expected Json header in Content-Type' ); } } /** * @return array * * @throws \Uwil\ArcadiaGenesisPhpClient\Exception\InvalidApiResponse */ protected function assertApiResponseFormIsAsExpected(mixed $body): array { $e = new InvalidApiResponse( 'Body content shape cannot be recognized' ); if (! is_array($body) || ! array_key_exists('success', $body)) { throw $e; } if (! $this->apiResponseIsError($body) && ! $this->apiResponseContainsData($body)) { throw $e; } return $body; } /** * @param array $responseData */ protected function apiResponseIsError(array $responseData): bool { return array_key_exists('error', $responseData) && is_array($responseData['error']) && ! empty($responseData['error']); } /** * @param array $responseData */ protected function apiResponseContainsData(array $responseData): bool { return array_key_exists('data', $responseData) && is_array($responseData['data']); } protected function delay(int $attempt): void { $delayMs = $this->apiConfig->retryDelayMs * $attempt; usleep($delayMs * 1000); } }