]> _ Git - fluidbook_toolboxapiclient.git/commitdiff
wip #7062 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 29 Oct 2024 13:37:45 +0000 (14:37 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 29 Oct 2024 13:37:45 +0000 (14:37 +0100)
src/Client.php

index fe4b19dce99bbfc8dfc72f92e83cd0643d5ecb8f..c95bb091631520d455e22173338b06a5a330dc19 100644 (file)
@@ -2,14 +2,15 @@
 
 namespace Fluidbook\ToolboxApiClient;
 
-use GuzzleHttp\Exception\GuzzleException;
 use GuzzleHttp\Psr7\Utils;
 use Psr\Http\Message\ResponseInterface;
 
-class Client extends \GuzzleHttp\Client
+class Client
 {
     const BASE_URI = 'https://https://toolbox.fluidbook.com/api/';
     protected $_apiToken;
+    /** @var \GuzzleHttp\Client */
+    protected $_client;
 
     public function __construct($apiToken, $uri = null)
     {
@@ -18,7 +19,23 @@ class Client extends \GuzzleHttp\Client
         if (null === $uri) {
             $uri = self::BASE_URI;
         }
-        parent::__construct(['base_uri' => $uri]);
+        $this->setClient(new \GuzzleHttp\Client(['base_uri' => $uri]));
+    }
+
+    /**
+     * @return \GuzzleHttp\Client
+     */
+    public function getClient(): \GuzzleHttp\Client
+    {
+        return $this->_client;
+    }
+
+    /**
+     * @param \GuzzleHttp\Client $client
+     */
+    public function setClient(\GuzzleHttp\Client $client): void
+    {
+        $this->_client = $client;
     }
 
     /**
@@ -29,12 +46,12 @@ class Client extends \GuzzleHttp\Client
         return $this->_apiToken;
     }
 
-    public static function getResponseBody(ResponseInterface $response): string
+    public static function getResponseContents(ResponseInterface $response): string
     {
         return $response->getBody()->getContents();
     }
 
-    public function createFluidbook(\SplFileInfo $file, $base, $title, $installServer, $installPath)
+    public function createFluidbook(\SplFileInfo $file, $base, $title, $installServer, $installPath): string
     {
         $data = ['base' => $base, 'title' => $title, 'export[server]' => $installServer, 'export[path]' => $installPath];
         $multipart = [];
@@ -43,7 +60,12 @@ class Client extends \GuzzleHttp\Client
         }
         $multipart[] = ['name' => 'file', 'contents' => Utils::tryFopen($file->getPathname(), 'r'), 'filename' => $file->getFilename()];
 
-        return self::getResponseBody($this->post('/fluidbook-publication/create', ['multipart' => $multipart]));
+        return $this->_request('post', '/fluidbook-publication/create', ['multipart' => $multipart]);
+    }
+
+    protected function _request($method, $uri, $options = [])
+    {
+        return self::getResponseContents($this->getClient()->request($method, $uri, $options));
     }