From 550e6fdacf4d349a6a83fe975d746e063a834b61 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 14 Mar 2024 15:27:31 +0100 Subject: [PATCH] wip #6775 @2 --- app/Models/ToolWebflow.php | 6 ++++- app/Services/Webflow.php | 51 ++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 699f150a1..c7e4f4e9e 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -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); diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index 04de2d278..b73d2e825 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -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) -- 2.39.5