|
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|
|
namespace App\Service;
|
|
|
|
|
|
use App\Service\Utils\UrlBuilder;
|
|
|
+use HttpException;
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
|
@@ -28,6 +29,7 @@ class ApiRequestService
|
|
|
* @param array $parameters
|
|
|
* @param array $options
|
|
|
* @return array
|
|
|
+ * @throws HttpException
|
|
|
*/
|
|
|
public function getJsonContent(string $path, array $parameters = [], array $options = []): array {
|
|
|
return json_decode($this->getContent($path, $parameters, $options), true);
|
|
|
@@ -40,12 +42,13 @@ class ApiRequestService
|
|
|
* @param array $parameters
|
|
|
* @param array $options
|
|
|
* @return string
|
|
|
+ * @throws HttpException
|
|
|
*/
|
|
|
public function getContent(string $path, array $parameters = [], array $options = []): string {
|
|
|
try {
|
|
|
return $this->get($path, $parameters, $options)->getContent();
|
|
|
} catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
|
|
|
- throw new NotFoundHttpException('data not found', $e, 404);
|
|
|
+ throw new HttpException('data not found', $e, 404);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -56,11 +59,54 @@ class ApiRequestService
|
|
|
* @param array $parameters
|
|
|
* @param array $options
|
|
|
* @return ResponseInterface
|
|
|
+ * @throws HttpException
|
|
|
*/
|
|
|
protected function get(string $path, array $parameters = [], array $options = []): ResponseInterface {
|
|
|
return $this->request('GET', $path, $parameters, $options);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sends a POST request and returns the response
|
|
|
+ *
|
|
|
+ * @param string $path
|
|
|
+ * @param array $parameters
|
|
|
+ * @param array $options
|
|
|
+ * @return ResponseInterface
|
|
|
+ * @throws HttpException
|
|
|
+ */
|
|
|
+ public function post(string $path, array $parameters = [], array $options = []): ResponseInterface
|
|
|
+ {
|
|
|
+ return $this->request('POST', $path, $parameters, $options);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends a PUT request and returns the response
|
|
|
+ *
|
|
|
+ * @param string $path
|
|
|
+ * @param array $parameters
|
|
|
+ * @param array $options
|
|
|
+ * @return ResponseInterface
|
|
|
+ * @throws HttpException
|
|
|
+ */
|
|
|
+ public function put(string $path, array $parameters = [], array $options = []): ResponseInterface
|
|
|
+ {
|
|
|
+ return $this->request('PUT', $path, $parameters, $options);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends a DELETE request and returns the response
|
|
|
+ *
|
|
|
+ * @param string $path
|
|
|
+ * @param array $parameters
|
|
|
+ * @param array $options
|
|
|
+ * @return ResponseInterface
|
|
|
+ * @throws HttpException
|
|
|
+ */
|
|
|
+ public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface
|
|
|
+ {
|
|
|
+ return $this->request('DELETE', $path, $parameters, $options);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Send an HTTP request to the Dolibarr API,
|
|
|
* and return the decoded content of the response's body
|
|
|
@@ -70,8 +116,9 @@ class ApiRequestService
|
|
|
* @param array $parameters
|
|
|
* @param array $options
|
|
|
* @return ResponseInterface
|
|
|
+ * @throws HttpException
|
|
|
*/
|
|
|
- protected function request(
|
|
|
+ public function request(
|
|
|
string $method,
|
|
|
string $url,
|
|
|
array $parameters = [],
|
|
|
@@ -83,7 +130,7 @@ class ApiRequestService
|
|
|
try {
|
|
|
return $this->client->request($method, $url, $options);
|
|
|
} catch (HttpExceptionInterface | TransportExceptionInterface $e) {
|
|
|
- throw new NotFoundHttpException('fetch error', $e, 500);
|
|
|
+ throw new HttpException('Request error : ', $e, 500);
|
|
|
}
|
|
|
}
|
|
|
}
|