Просмотр исходного кода

add a $body parameter to ApiRequestService post an put methods

Olivier Massot 1 год назад
Родитель
Сommit
bae0063b42

+ 14 - 39
src/Service/Dolibarr/DolibarrApiService.php

@@ -7,10 +7,8 @@ namespace App\Service\Dolibarr;
 use App\Entity\Organization\Organization;
 use App\Service\Rest\ApiRequestService;
 use JetBrains\PhpStorm\Pure;
-use Symfony\Component\HttpClient\Exception\ClientException;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\HttpException;
-use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 /**
@@ -185,44 +183,21 @@ class DolibarrApiService extends ApiRequestService
 
     /**
      * @param Organization $organization
-     * @throws TransportExceptionInterface
+     * @return mixed
      */
-    public function createSociety(Organization $organization): void
+    public function createSociety(Organization $organization): mixed
     {
-        try {
-            $body = json_encode([
-                'name' => $organization->getName(),
-                'client' => 2,
-                'code_client' => -1,
-                'import_key' => 'crm',
-                'array_options' => ['options_2iopen_organization_id' => $organization->getId()]
-            ]);
-
-            $options = [
-                'headers' => [
-                    'Accept'=> 'application/json',
-                    'DOLAPIKEY' => "Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ"
-                ],
-                'body' => $body
-            ];
-
-            /** @var Response $response */
-            $response = $this->client->request('POST',
-                "api/index.php/thirdparties",
-                $options
-            );
-
-            // $content = json_decode($response->getBody()->getContents(), true);
-
-        } catch (ClientException $exception) {
-            switch ($exception->getResponse()->getStatusCode()) {
-                case Response::HTTP_BAD_REQUEST:
-                    //throw new \Exception('errors', self::HTTP_ERROR);
-                    break;
-                case Response::HTTP_NOT_FOUND:
-                    //throw new \Exception('credentials_incorrects', self::HTTP_UNAUTHORIZED);
-                    break;
-            }
-        }
+        $body = [
+            'name' => $organization->getName(),
+            'client' => 2,
+            'code_client' => -1,
+            'import_key' => 'crm',
+            'array_options' => ['options_2iopen_organization_id' => $organization->getId()]
+        ];
+
+        /** @var Response $response */
+        $response = $this->post("api/index.php/thirdparties", $body);
+
+        return json_decode($response->getContent(), true);
     }
 }

+ 8 - 2
src/Service/Rest/ApiRequestInterface.php

@@ -47,22 +47,28 @@ interface ApiRequestInterface
     /**
      * Sends a POST request and returns the response.
      *
+     * @param string $path
+     * @param array<mixed>| string $body
      * @param array<mixed> $parameters
      * @param array<mixed> $options
      *
+     * @return ResponseInterface
      * @throws HttpException
      */
-    public function post(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function post(string $path, array | string $body, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
      * Sends a PUT request and returns the response.
      *
+     * @param string $path
+     * @param array<mixed> | string $body
      * @param array<mixed> $parameters
      * @param array<mixed> $options
      *
+     * @return ResponseInterface
      * @throws HttpException
      */
-    public function put(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function put(string $path, array | string $body, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
      * Sends a DELETE request and returns the response.

+ 17 - 2
src/Service/Rest/ApiRequestService.php

@@ -71,29 +71,44 @@ class ApiRequestService implements ApiRequestInterface
         return $this->request('GET', $path, $parameters, $options);
     }
 
+    /**
+     * @param array $options
+     * @param array|string $body
+     * @return array
+     */
+    private 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<mixed>|string $body
      * @param array<mixed> $parameters
      * @param array<mixed> $options
      *
      * @throws HttpException
      */
-    public function post(string $path, array $parameters = [], array $options = []): ResponseInterface
+    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<mixed>|string $body
      * @param array<mixed> $parameters
      * @param array<mixed> $options
      *
      * @throws HttpException
      */
-    public function put(string $path, array $parameters = [], array $options = []): ResponseInterface
+    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);
     }