$parameters * @param array $options * @param bool $throw @see https://symfony.com/doc/current/http_client.html#handling-exceptions * * @return array * * @throws HttpException * @throws \JsonException */ public function getJsonContent(string $path, array $parameters = [], array $options = [], bool $throw = true): array { return json_decode($this->getContent($path, $parameters, $options, $throw), true, 512, JSON_THROW_ON_ERROR); } /** * Sends a GET request and returns the response's body. * * @param array $parameters * @param array $options * @param bool $throw @see https://symfony.com/doc/current/http_client.html#handling-exceptions * * @throws HttpException */ public function getContent(string $path, array $parameters = [], array $options = [], bool $throw = true): string { try { return $this->get($path, $parameters, $options)->getContent($throw); } catch (ClientExceptionInterface|TransportExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface $e) { throw new HttpException($e->getCode(), 'Request error : '.$e->getMessage(), $e); } } /** * Sends a GET request and returns the response. * * @param array $parameters * @param array $options * * @throws HttpException */ public function get(string $path, array $parameters = [], array $options = []): ResponseInterface { return $this->request('GET', $path, $parameters, $options); } /** * Complète les options en y ajoutant le body. * * @param array $options * @param array|string $body * * @return array */ protected function addBodyOption(array $options, array|string $body): array { $option = is_array($body) ? ['json' => $body] : ['body' => $body]; return array_merge($options, $option); } /** * Sends a POST request and returns the response. * * @param array|string $body * @param array $parameters * @param array $options * * @throws HttpException */ public function post(string $path, array|string $body, array $parameters = [], array $options = []): ResponseInterface { $options = $this->addBodyOption($options, $body); return $this->request('POST', $path, $parameters, $options); } /** * Sends a PUT request and returns the response. * * @param array|string $body * @param array $parameters * @param array $options * * @throws HttpException */ public function put(string $path, array|string $body, array $parameters = [], array $options = []): ResponseInterface { $options = $this->addBodyOption($options, $body); return $this->request('PUT', $path, $parameters, $options); } /** * Sends a DELETE request and returns the response. * * @param array $parameters * @param array $options * * @throws HttpException */ public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface { return $this->request('DELETE', $path, $parameters, $options); } /** * Send an HTTP request to a REST API, * and return the decoded content of the response's body. * * @param array $parameters * @param array $options * * @throws HttpException */ public function request( string $method, string $url, array $parameters = [], array $options = [], ): ResponseInterface { $url = ltrim($url, '/'); $url = UrlBuilder::concatParameters($url, $parameters); try { return $this->client->request($method, $url, $options); } catch (TransportExceptionInterface $e) { throw new HttpException($e->getCode(), 'Request error : '.$e->getMessage(), $e); } } }