src/Service/WemapClient.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Exception;
  4. use GuzzleHttp\Client as GuzzleClient;
  5. use GuzzleHttp\Exception\ClientException;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use Psr\Http\Message\ResponseInterface;
  8. use stdClass;
  9. class WemapClient
  10. {
  11.     protected string $_url 'https://api.getwemap.com';
  12.     protected GuzzleClient $client;
  13.     protected string $_public;
  14.     protected string $_accessToken;
  15.     protected string $_authToken;
  16.     public function __construct()
  17.     {
  18.         $this->client = new GuzzleClient();
  19.     }
  20.     public function setPublicKey(string $key): void
  21.     {
  22.         $this->_public $key;
  23.     }
  24.     public function setAccessToken(string $token): void
  25.     {
  26.         $this->_accessToken $token;
  27.     }
  28.     public function setAuthToken(string $token): void
  29.     {
  30.         $this->_authToken $token;
  31.     }
  32.     public function get(string $urlmixed $datas = []): ResponseInterface|GuzzleException
  33.     {
  34.         $params = [];
  35.         try {
  36.             $params['headers'] = [
  37.                 'Authorization' => 'Bearer ' $this->_accessToken,
  38.                 //'Content-type' => 'application/json'
  39.             ];
  40.             $rawResponse $this->client->request('get'$this->_url $url$params);
  41.             return $rawResponse;
  42.         } catch (ClientException $e) {
  43.             $response json_decode((string)$e->getResponse()->getBody()->getContents(), null512JSON_THROW_ON_ERROR);
  44.             //return ['error' => $response->error];
  45.             //throw new Exception($response->error);
  46.             throw new Exception($response->message$response->code);
  47.         }
  48.     }
  49.     public function post(string $urlmixed $datas = [], bool $accessToken true): ResponseInterface|GuzzleException
  50.     {
  51.         $params = [];
  52.         try {
  53.             $params['form_params'] = $datas;
  54.             if ($accessToken) {
  55.                 $params['headers'] = [
  56.                     'Authorization' => 'Basic ' $this->_authToken,
  57.                     'Content-type' => 'application/x-www-form-urlencoded'
  58.                 ];
  59.             }
  60.             $rawResponse $this->client->request('post'$this->_url $url$params);
  61.             return $rawResponse;
  62.         } catch (\GuzzleHttp\Exception\RequestException $e) {
  63.             $response json_decode((string)$e->getResponse()->getBody()->getContents(), null512JSON_THROW_ON_ERROR);
  64.             throw new Exception($response->message$response->code);
  65.         }
  66.     }
  67.     public function patch(string $urlmixed $datas = [], bool $accessToken true): ResponseInterface|GuzzleException
  68.     {
  69.         try {
  70.             $params = [
  71.                 'multipart' => $this->_optionsToMultipart($datas),
  72.             ];
  73.             if ($accessToken) {
  74.                 $params['multipart'][] = [
  75.                     'name' => 'access_token',
  76.                     'contents' => $this->_accessToken,
  77.                 ];
  78.                 $params['multipart'][] = [
  79.                     'name' => 'nonce',
  80.                     'contents' => random_int(1_000_0009_999_999),
  81.                 ];
  82.             }
  83.             $rawResponse $this->client->request('patch'$this->_url $url$params);
  84.             $response json_decode((string)$rawResponse->getBody()->getContents(), null512JSON_THROW_ON_ERROR);
  85.             return $response;
  86.         } catch (\GuzzleHttp\Exception\RequestException $e) {
  87.             $response json_decode((string)$e->getResponse()->getBody()->getContents(), null512JSON_THROW_ON_ERROR);
  88.             throw new Exception($response->message$response->code);
  89.         }
  90.     }
  91.     public function delete(string $urlmixed $datas = []): ResponseInterface|GuzzleException
  92.     {
  93.         try {
  94.             $options = [
  95.                 'headers' => [
  96.                     'nonce' => random_int(1000099999),
  97.                     'access-token' => $this->_accessToken,
  98.                 ],
  99.             ];
  100.             $rawResponse $this->client->request('delete'$this->_url $url$options);
  101.             $response json_decode((string)$rawResponse->getBody(), null512JSON_THROW_ON_ERROR);
  102.             return $response;
  103.         } catch (\GuzzleHttp\Exception\RequestException $e) {
  104.             $response json_decode((string)$e->getResponse()->getBody()->getContents(), null512JSON_THROW_ON_ERROR);
  105.             throw new Exception($response->message$response->code);
  106.         }
  107.     }
  108.     private function _optionsToMultipart(mixed $array): mixed
  109.     {
  110.         $return = [];
  111.         foreach ($array as $key => $value) {
  112.             if (!is_array($value)) {
  113.                 $return[] = [
  114.                     'name' => $key,
  115.                     'contents' => $value,
  116.                 ];
  117.             } else {
  118.                 $return[] = $value;
  119.             }
  120.         }
  121.         return $return;
  122.     }
  123. }