]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6775 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 14:27:31 +0000 (15:27 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 14:27:31 +0000 (15:27 +0100)
app/Models/ToolWebflow.php
app/Services/Webflow.php

index 699f150a160def432f4ac1638bd02bdafe80d3b1..c7e4f4e9ef698a3b2591fb8c91dfd08948e95d1e 100644 (file)
@@ -61,7 +61,7 @@ class ToolWebflow extends ToolboxModel
     protected function _parsePages()
     {
         Webflow::setToken($this->webflow_api_token);
-        dd(Webflow::listPages($this->webflow));
+        dd(Webflow::getAllData($this->webflow));
     }
 
     public function getLocales()
@@ -88,6 +88,10 @@ class ToolWebflow extends ToolboxModel
      */
     public function mirror($slow = true, $force = false)
     {
+        Webflow::setToken($this->webflow_api_token);
+        Webflow::clearCache();
+        Webflow::getAllData($this->webflow);
+
         $path = $this->getMirrorPath();
         if ($force) {
             $path = Files::emptyDir($path);
index 04de2d278c4fc99395402b6267dd66d0f3c0b9b0..b73d2e82530894c53b4957f5b0d04435594ec14a 100644 (file)
@@ -40,16 +40,20 @@ class Webflow
         return $response->json()['access_token'];
     }
 
-    public static function request($url, $data = [], $method = 'get', $ttl = 600, $force = false)
+    public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false)
     {
         $cacheKey = 'webflow_' . static::getToken() . '_' . $method . '_' . $url . '_' . sha1(print_r($data, true));
         if ($force) {
             Cache::forget($cacheKey);
         }
-        $res = Cache::tags('webflow')->remember($cacheKey, $ttl, function () use ($url, $method, $data) {
+        $res = Cache::tags('webflow_' . static::getToken())->remember($cacheKey, $ttl, function () use ($url, $method, $data) {
+            while (Cache::get('webflow_' . static::getToken() . '_wait', false)) {
+                sleep(1);
+            }
             /** @var Response $response */
             $response = Http::withToken(self::getToken())->$method(self::BASE_URL . $url, $data);
-            if ((int)$response->header('X-RateLimit-Remaining') <= 2) {
+            if ((int)$response->header('X-RateLimit-Remaining') <= 1) {
+                Cache::set('webflow_' . static::getToken() . '_wait', true, 60);
                 sleep(60);
             }
             return $response->json();
@@ -62,7 +66,7 @@ class Webflow
 
     public static function clearCache()
     {
-        return Cache::tags('webflow')->flush();
+        return Cache::tags('webflow_' . static::getToken())->flush();
     }
 
     public static function listSites()
@@ -92,8 +96,45 @@ class Webflow
     {
         $res = [];
         foreach (self::listPages($shortname) as $page) {
-            $res[$page['id']] = self::getPageContents($page['id']);
+            $res[$page['id']] = [
+                'details' => $page,
+                'contents' => self::getPageContents($page['id'])
+            ];
         }
+        return $res;
+    }
+
+    public static function getAllCMSContents($shortname)
+    {
+        $res = [];
+        foreach (self::listCMSCollections($shortname) as $collection) {
+            $res[$collection['id']] = [
+                'details' => self::getCMSCollectionDetails($collection['id']),
+                'contents' => self::getCMSCollectionContents($collection['id']),
+            ];
+        }
+
+        return $res;
+    }
+
+    public static function getAllData($shortname)
+    {
+        return ['pages' => self::getAllPagesContents($shortname), 'cms' => self::getAllCMSContents($shortname)];
+    }
+
+    public static function getCMSCollectionDetails($collectionID)
+    {
+        return self::request('collections/' . $collectionID);
+    }
+
+    public static function getCMSCollectionContents($collectionID)
+    {
+        return self::request('collections/' . $collectionID . '/items')['items'];
+    }
+
+    public static function listCMSCollections($shortname)
+    {
+        return self::request('sites/' . self::getSiteId($shortname) . '/collections')['collections'];
     }
 
     public static function getPageContents($pageID)