ApiRequestService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Rest;
  4. use App\Service\Utils\UrlBuilder;
  5. use Symfony\Component\HttpKernel\Exception\HttpException;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  9. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  11. use Symfony\Contracts\HttpClient\HttpClientInterface;
  12. use Symfony\Contracts\HttpClient\ResponseInterface;
  13. /**
  14. * Base class for services sending requests to an external API
  15. */
  16. class ApiRequestService implements ApiRequestInterface
  17. {
  18. public function __construct(
  19. protected HttpClientInterface $client
  20. ) {}
  21. /**
  22. * Sends a GET request and returns the response's body decoded as json
  23. * @param string $path
  24. * @param array<mixed> $parameters
  25. * @param array<mixed> $options
  26. * @return array<mixed>
  27. * @throws HttpException
  28. * @throws \JsonException
  29. */
  30. public function getJsonContent(string $path, array $parameters = [], array $options = []): array
  31. {
  32. return json_decode($this->getContent($path, $parameters, $options), true, 512, JSON_THROW_ON_ERROR);
  33. }
  34. /**
  35. * Sends a GET request and returns the response's body
  36. *
  37. * @param string $path
  38. * @param array<mixed> $parameters
  39. * @param array<mixed> $options
  40. * @return string
  41. * @throws HttpException
  42. */
  43. public function getContent(string $path, array $parameters = [], array $options = []): string
  44. {
  45. try {
  46. return $this->get($path, $parameters, $options)->getContent();
  47. } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
  48. throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
  49. }
  50. }
  51. /**
  52. * Sends a GET request and returns the response
  53. *
  54. * @param string $path
  55. * @param array<mixed> $parameters
  56. * @param array<mixed> $options
  57. * @return ResponseInterface
  58. * @throws HttpException
  59. */
  60. public function get(string $path, array $parameters = [], array $options = []): ResponseInterface
  61. {
  62. return $this->request('GET', $path, $parameters, $options);
  63. }
  64. /**
  65. * Sends a POST request and returns the response
  66. *
  67. * @param string $path
  68. * @param array<mixed> $parameters
  69. * @param array<mixed> $options
  70. * @return ResponseInterface
  71. * @throws HttpException
  72. */
  73. public function post(string $path, array $parameters = [], array $options = []): ResponseInterface
  74. {
  75. return $this->request('POST', $path, $parameters, $options);
  76. }
  77. /**
  78. * Sends a PUT request and returns the response
  79. *
  80. * @param string $path
  81. * @param array<mixed> $parameters
  82. * @param array<mixed> $options
  83. * @return ResponseInterface
  84. * @throws HttpException
  85. */
  86. public function put(string $path, array $parameters = [], array $options = []): ResponseInterface
  87. {
  88. return $this->request('PUT', $path, $parameters, $options);
  89. }
  90. /**
  91. * Sends a DELETE request and returns the response
  92. *
  93. * @param string $path
  94. * @param array<mixed> $parameters
  95. * @param array<mixed> $options
  96. * @return ResponseInterface
  97. * @throws HttpException
  98. */
  99. public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface
  100. {
  101. return $this->request('DELETE', $path, $parameters, $options);
  102. }
  103. /**
  104. * Send an HTTP request to a REST API,
  105. * and return the decoded content of the response's body
  106. *
  107. * @param string $method
  108. * @param string $url
  109. * @param array<mixed> $parameters
  110. * @param array<mixed> $options
  111. * @return ResponseInterface
  112. * @throws HttpException
  113. */
  114. public function request(
  115. string $method,
  116. string $url,
  117. array $parameters = [],
  118. array $options = []
  119. ): ResponseInterface
  120. {
  121. $url = ltrim($url, '/');
  122. $url = UrlBuilder::concatParameters($url, $parameters);
  123. try {
  124. return $this->client->request($method, $url, $options);
  125. } catch (TransportExceptionInterface $e) {
  126. throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
  127. }
  128. }
  129. }