]> _ Git - cubist_dsn.git/commitdiff
try #7646 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 17 Jul 2025 12:37:08 +0000 (14:37 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 17 Jul 2025 12:37:08 +0000 (14:37 +0200)
src/Api.php

index 904123893e2581a6572d9e7749f113cd44ead251..f2c853f82e1fea21fe0b9c94f5a0472674ed35d3 100644 (file)
@@ -1,30 +1,34 @@
 <?php
+
 namespace Cubist\DSN;
+
 use GuzzleHttp\Client;
 
 class Api
 {
-    const AUTH_URL = 'https://net-entreprises.fr/authentifier/1.0/';
+    const AUTH_URL = 'https://services.net-entreprises.fr/authentifier/1.0/';
+    const SUBMISSIONS_LIST_URL = 'https://consultation.dsnrg.net-entreprises.fr/lister-depots/2.0/';
+    const FEEDBACKS_LIST_URL = 'https://consultation.dsnrg.net-entreprises.fr/lister-retours-flux/2.0/';
     const USER_AGENT = 'Cubist DSN PHP API';
 
     /**
      * @var Client
      */
     protected $client;
+    protected $token;
 
+    protected $authData = [];
 
-    public function _construct()
-    {
-
-    }
 
-    public function auth($siret, $name, $firstname, $password, $service = 25)
+    public function __construct($siret, $name, $firstname, $password, $service = 25)
     {
         $this->client = new Client();
-        $response = $this->client->request('POST', self::AUTH_URL,
-            ['body' => $this->_authRequest($siret, $name, $firstname, $password, $service), 'headers' => ['Content-Type' => 'application/xml', 'User-Agent' => self::USER_AGENT]]
-        );
-        dddd($response);
+        $this->token = cache()->remember('dsn_' . $siret, 7200, function () use ($siret, $name, $firstname, $password, $service) {
+            $response = $this->_request('POST', self::AUTH_URL,
+                ['body' => $this->_authRequest($siret, $name, $firstname, $password, $service), 'headers' => ['Content-Type' => 'application/xml', 'User-Agent' => self::USER_AGENT]]
+            );
+            return $response->getBody()->getContents();
+        });
     }
 
     protected function _authRequest($siret, $name, $firstname, $password, $service = 25)
@@ -37,4 +41,42 @@ class Api
 <service>' . $service . '</service>
 </identifiants>';
     }
+
+    public function listSubmissions($timestamp = '')
+    {
+        return $this->_json($this->_request('GET', self::SUBMISSIONS_LIST_URL . $timestamp));
+    }
+
+    public function getFeedbacks($id)
+    {
+        return $this->_json($this->_request('GET', self::FEEDBACKS_LIST_URL . '/' . $id));
+    }
+
+    /**
+     * @param $method
+     * @param $uri
+     * @param $options
+     * @return \Psr\Http\Message\ResponseInterface|void
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     */
+    protected function _request($method, $uri, $options = [])
+    {
+        if (!isset($options['headers'])) {
+            $options['headers'] = [];
+        }
+        $options['headers']['User-Agent'] = self::USER_AGENT;
+        if ($this->token) {
+            $options['headers']['Authorization'] = 'DSNLogin jeton=' . $this->token;
+        }
+        try {
+            return $this->client->request($method, $uri, $options);
+        } catch (\GuzzleHttp\Exception\ClientException $e) {
+            dd($e);
+        }
+    }
+
+    protected function _json($response)
+    {
+        return json_decode($response->getBody()->getContents());
+    }
 }
\ No newline at end of file