ApiRequestService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 $parameters
  25. * @param array $options
  26. * @return array
  27. * @throws HttpException
  28. * @throws \JsonException
  29. */
  30. public function getJsonContent(string $path, array $parameters = [], array $options = []): array {
  31. return json_decode($this->getContent($path, $parameters, $options), true, 512, JSON_THROW_ON_ERROR);
  32. }
  33. /**
  34. * Sends a GET request and returns the response's body
  35. *
  36. * @param string $path
  37. * @param array $parameters
  38. * @param array $options
  39. * @return string
  40. * @throws HttpException
  41. */
  42. public function getContent(string $path, array $parameters = [], array $options = []): string {
  43. try {
  44. return $this->get($path, $parameters, $options)->getContent();
  45. } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
  46. throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
  47. }
  48. }
  49. /**
  50. * Sends a GET request and returns the response
  51. *
  52. * @param string $path
  53. * @param array $parameters
  54. * @param array $options
  55. * @return ResponseInterface
  56. * @throws HttpException
  57. */
  58. public function get(string $path, array $parameters = [], array $options = []): ResponseInterface {
  59. return $this->request('GET', $path, $parameters, $options);
  60. }
  61. /**
  62. * Sends a POST request and returns the response
  63. *
  64. * @param string $path
  65. * @param array $parameters
  66. * @param array $options
  67. * @return ResponseInterface
  68. * @throws HttpException
  69. */
  70. public function post(string $path, array $parameters = [], array $options = []): ResponseInterface
  71. {
  72. return $this->request('POST', $path, $parameters, $options);
  73. }
  74. /**
  75. * Sends a PUT request and returns the response
  76. *
  77. * @param string $path
  78. * @param array $parameters
  79. * @param array $options
  80. * @return ResponseInterface
  81. * @throws HttpException
  82. */
  83. public function put(string $path, array $parameters = [], array $options = []): ResponseInterface
  84. {
  85. return $this->request('PUT', $path, $parameters, $options);
  86. }
  87. /**
  88. * Sends a DELETE request and returns the response
  89. *
  90. * @param string $path
  91. * @param array $parameters
  92. * @param array $options
  93. * @return ResponseInterface
  94. * @throws HttpException
  95. */
  96. public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface
  97. {
  98. return $this->request('DELETE', $path, $parameters, $options);
  99. }
  100. /**
  101. * Send an HTTP request to a REST API,
  102. * and return the decoded content of the response's body
  103. *
  104. * @param string $method
  105. * @param string $url
  106. * @param array $parameters
  107. * @param array $options
  108. * @return ResponseInterface
  109. * @throws HttpException
  110. */
  111. public function request(
  112. string $method,
  113. string $url,
  114. array $parameters = [],
  115. array $options = []
  116. ): ResponseInterface
  117. {
  118. $url = ltrim($url, '/');
  119. $url = UrlBuilder::concatParameters($url, $parameters);
  120. try {
  121. return $this->client->request($method, $url, $options);
  122. } catch (TransportExceptionInterface $e) {
  123. throw new HttpException($e->getCode(), 'Request error : ' . $e->getMessage(), $e);
  124. }
  125. }
  126. }