<?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)
<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