<?php
namespace App\Service;
use Exception;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use stdClass;
class WemapClient
{
protected string $_url = 'https://api.getwemap.com';
protected GuzzleClient $client;
protected string $_public;
protected string $_accessToken;
protected string $_authToken;
public function __construct()
{
$this->client = new GuzzleClient();
}
public function setPublicKey(string $key): void
{
$this->_public = $key;
}
public function setAccessToken(string $token): void
{
$this->_accessToken = $token;
}
public function setAuthToken(string $token): void
{
$this->_authToken = $token;
}
public function get(string $url, mixed $datas = []): ResponseInterface|GuzzleException
{
$params = [];
try {
$params['headers'] = [
'Authorization' => 'Bearer ' . $this->_accessToken,
//'Content-type' => 'application/json'
];
$rawResponse = $this->client->request('get', $this->_url . $url, $params);
return $rawResponse;
} catch (ClientException $e) {
$response = json_decode((string)$e->getResponse()->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
//return ['error' => $response->error];
//throw new Exception($response->error);
throw new Exception($response->message, $response->code);
}
}
public function post(string $url, mixed $datas = [], bool $accessToken = true): ResponseInterface|GuzzleException
{
$params = [];
try {
$params['form_params'] = $datas;
if ($accessToken) {
$params['headers'] = [
'Authorization' => 'Basic ' . $this->_authToken,
'Content-type' => 'application/x-www-form-urlencoded'
];
}
$rawResponse = $this->client->request('post', $this->_url . $url, $params);
return $rawResponse;
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = json_decode((string)$e->getResponse()->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
throw new Exception($response->message, $response->code);
}
}
public function patch(string $url, mixed $datas = [], bool $accessToken = true): ResponseInterface|GuzzleException
{
try {
$params = [
'multipart' => $this->_optionsToMultipart($datas),
];
if ($accessToken) {
$params['multipart'][] = [
'name' => 'access_token',
'contents' => $this->_accessToken,
];
$params['multipart'][] = [
'name' => 'nonce',
'contents' => random_int(1_000_000, 9_999_999),
];
}
$rawResponse = $this->client->request('patch', $this->_url . $url, $params);
$response = json_decode((string)$rawResponse->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
return $response;
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = json_decode((string)$e->getResponse()->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
throw new Exception($response->message, $response->code);
}
}
public function delete(string $url, mixed $datas = []): ResponseInterface|GuzzleException
{
try {
$options = [
'headers' => [
'nonce' => random_int(10000, 99999),
'access-token' => $this->_accessToken,
],
];
$rawResponse = $this->client->request('delete', $this->_url . $url, $options);
$response = json_decode((string)$rawResponse->getBody(), null, 512, JSON_THROW_ON_ERROR);
return $response;
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = json_decode((string)$e->getResponse()->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
throw new Exception($response->message, $response->code);
}
}
private function _optionsToMultipart(mixed $array): mixed
{
$return = [];
foreach ($array as $key => $value) {
if (!is_array($value)) {
$return[] = [
'name' => $key,
'contents' => $value,
];
} else {
$return[] = $value;
}
}
return $return;
}
}