|
@@ -0,0 +1,71 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\File\BIM\Glendale;
|
|
|
+
|
|
|
+
|
|
|
+use App\Services\File\BIM\Exceptions\FailedException;
|
|
|
+use Psr\Http\Message\ResponseInterface;
|
|
|
+
|
|
|
+
|
|
|
+class Client {
|
|
|
+
|
|
|
+ protected static ?Client $instance = null;
|
|
|
+
|
|
|
+ protected \GuzzleHttp\Client $client;
|
|
|
+
|
|
|
+ protected function __construct()
|
|
|
+ {
|
|
|
+ $this->client = new \GuzzleHttp\Client([
|
|
|
+ 'base_uri' => config("bim.glendale.host"),
|
|
|
+ 'http_errors' => false,
|
|
|
+ 'debug' => false,
|
|
|
+ 'headers' => [
|
|
|
+ 'Accept' => "application/json",
|
|
|
+ 'Token' => config("bim.glendale.token"),
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function getInstance(): Client
|
|
|
+ {
|
|
|
+ if (! self::$instance) {
|
|
|
+ self::$instance = new self();
|
|
|
+ }
|
|
|
+
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function request(string $method, string $uri, array $options = []): ?array
|
|
|
+ {
|
|
|
+ return $this->parseResponse($this->client->request($method, $uri, $options));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function post(string $uri, array $options = []) {
|
|
|
+ return $this->request("POST", $uri, $options);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function parseResponse(ResponseInterface $response): ?array
|
|
|
+ {
|
|
|
+ $body = json_decode((string)$response->getBody(), true);
|
|
|
+
|
|
|
+ return match ($response->getStatusCode()) {
|
|
|
+ 200, 201, 204 => $this->parseStatusSuccessResponse($body),
|
|
|
+ default => throw new FailedException($body['message'] ?? $response->getReasonPhrase()),
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function parseStatusSuccessResponse(array $body)
|
|
|
+ {
|
|
|
+ $isJson = json_last_error() == JSON_ERROR_NONE;
|
|
|
+
|
|
|
+ if (! $isJson) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($body['code'] != 1) {
|
|
|
+ throw new FailedException($body['codeMsg']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $body;
|
|
|
+ }
|
|
|
+}
|