Browse Source

set dolibarr requests headers from .env

Olivier Massot 4 years ago
parent
commit
b9a76f9325
3 changed files with 9 additions and 15 deletions
  1. 2 1
      .env
  2. 4 1
      config/packages/framework.yaml
  3. 3 13
      src/Service/Dolibarr/DolibarrService.php

+ 2 - 1
.env

@@ -44,5 +44,6 @@ OPENTALENT_CONFIG=/config/opentalent
 ###< opentalent config folder ###
 
 ###> dolibarr client ###
-DOLIBARR_BASE_URI='https://prod-erp.2iopenservice.com/api/index.php/'
+DOLIBARR_API_BASE_URI='https://prod-erp.2iopenservice.com/api/index.php/'
+DOLIBARR_API_TOKEN='Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ'
 ###< dolibarr client ###

+ 4 - 1
config/packages/framework.yaml

@@ -23,4 +23,7 @@ framework:
             openstreetmap:
                 base_uri: 'https://nominatim.openstreetmap.org/'
             dolibarr_client:
-                base_uri: '%env(DOLIBARR_BASE_URI)%'
+                base_uri: '%env(DOLIBARR_API_BASE_URI)%'
+                headers:
+                    Accept: 'application/json'
+                    DOLAPIKEY: '%env(DOLIBARR_API_TOKEN)%'

+ 3 - 13
src/Service/Dolibarr/DolibarrService.php

@@ -70,7 +70,7 @@ class DolibarrService
             $organization->getId()
         );
 
-        return $this->request("api/index.php/thirdparties",'POST', $body);
+        return $this->request("api/index.php/thirdparties",'POST', ['body' => $body]);
     }
 
     /**
@@ -79,23 +79,13 @@ class DolibarrService
      *
      * @param string $path
      * @param string $method
-     * @param string|null $body
+     * @param array $options
      * @return array
      * @throws NotFoundHttpException
      */
-    private function request(string $path, string $method = 'GET', string $body = ''): array
+    private function request(string $path, string $method = 'GET', array $options = []): array
     {
         try {
-            $options = [
-                'headers' => [
-                    'Accept'=> 'application/json',
-                    'DOLAPIKEY' => 'Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ'
-                ]
-            ];
-
-            if ($body !== null) {
-                $options['body'] = $body;
-            }
             $uri = ltrim($path, '/');
             $response = $this->client->request($method, $uri, $options);
             return json_decode($response->getContent(), true);