toRequest($this->baseUri, $this->headers); $response = $this->client->sendRequest($request); $contents = (string) $response->getBody(); if ($response->getHeader('Content-Type')[0] === 'text/plain; charset=utf-8') { return $contents; } try { /** @var array{error?: array{message: string, type: string, code: string}} $response */ $response = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $jsonException) { throw new UnserializableResponse($jsonException); } if (isset($response['error'])) { throw new ErrorException($response['error']); } return $response; } /** * {@inheritDoc} */ public function requestContent(Payload $payload): string { $request = $payload->toRequest($this->baseUri, $this->headers); $response = $this->client->sendRequest($request); $contents = $response->getBody()->getContents(); try { /** @var array{error?: array{message: string, type: string, code: string}} $response */ $response = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); if (isset($response['error'])) { throw new ErrorException($response['error']); } } catch (JsonException) { // .. } return $contents; } }