httpClient = $httpClient; $this->requestBuilder = $requestBuilder; $this->jsonDecoder = $jsonDecoder; $this->responseExceptionFactory = $responseExceptionFactory; } /** * @param array $query * @param JsonObject|null $body * @param array $headers * * @throws ApiResponseException * @throws UnexpectedResponseException * @throws ClientExceptionInterface * @throws JsonException */ public function execute( string $method, ApiUri $uri, array $query = [], ?array $body = null, ?AuthToken $token = null, array $headers = [] ): ResponseInterface { $request = $this->requestBuilder->build($method, $uri, $query, $body, $token, $headers); $response = $this->httpClient->sendRequest($request); $statusCode = $response->getStatusCode(); if ($statusCode >= 200 && $statusCode < 400) { return $response; } if ($statusCode >= 400) { throw $this->responseExceptionFactory->make($response); } throw new UnexpectedResponseException('Unexpected response status code.', $statusCode); } /** * @param array $query * @param JsonObject|null $body * @param array $headers * * @throws ApiResponseException * @throws UnexpectedResponseException * @throws ClientExceptionInterface * @throws JsonException * * @return array */ public function executeJson( string $method, ApiUri $uri, array $query = [], ?array $body = null, ?AuthToken $token = null, array $headers = [] ): array { $response = $this->execute($method, $uri, $query, $body, $token, $headers); $body = (string) $response->getBody(); try { return $this->jsonDecoder->decode($body); } catch (DecodingException $exception) { throw new UnexpectedResponseException('Unable to decode JSON response.', 0, $exception); } } }