]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6775 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 09:21:02 +0000 (10:21 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 09:21:02 +0000 (10:21 +0100)
app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php
app/Models/ToolWebflow.php
app/Services/Webflow.php

index 2599a746000851be2afa1fd3ecc306069764600e..1942b039e0b00196e0dbf85d57e29031a7f75e58 100644 (file)
@@ -19,7 +19,7 @@ trait WebflowOperation
         Route::match(['get'], 'webflow/auth', function () {
             $id = request()->get('state');
             $wf = ToolWebflow::withoutGlobalScopes()->find($id);
-            $wf->webflow_api_token = Webflow::getToken(request()->get('code'));
+            $wf->webflow_api_token = Webflow::getTokenFromCode(request()->get('code'));
             $wf->save();
             return redirect(backpack_url('tool-webflow/' . $id . '/edit'));
         });
index 58987cefb9b6756632725f87b5b668a03a89a2a9..699f150a160def432f4ac1638bd02bdafe80d3b1 100644 (file)
@@ -6,6 +6,7 @@ use App\Http\Controllers\Admin\Operations\Tools\StaticSiteUploader;
 use App\Http\Controllers\Admin\Operations\Tools\WebflowOperation;
 use App\Jobs\WebflowPublish;
 use App\Models\Base\ToolboxModel;
+use App\Services\Webflow;
 use Cubist\Backpack\Magic\Fields\Code;
 use Cubist\Backpack\Magic\Fields\SelectFromArray;
 use Cubist\Backpack\Magic\Fields\Table;
@@ -59,7 +60,8 @@ class ToolWebflow extends ToolboxModel
 
     protected function _parsePages()
     {
-
+        Webflow::setToken($this->webflow_api_token);
+        dd(Webflow::listPages($this->webflow));
     }
 
     public function getLocales()
index cdbf2a7d0dfed4639f27e8c0ed9bb59bbeb851d6..dc4729e6ef74d51f9a44b37b87d81b93289a28da 100644 (file)
@@ -2,11 +2,28 @@
 
 namespace App\Services;
 
+
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Http;
 
 class Webflow
 {
-    public static function getToken($code, $data = [])
+
+    const BASE_URL = 'https://api.webflow.com/beta/';
+
+    protected static $_token = null;
+
+    public static function setToken($token)
+    {
+        self::$_token = $token;
+    }
+
+    public static function getToken()
+    {
+        return self::$_token;
+    }
+
+    public static function getTokenFromCode($code, $data = [])
     {
         $response = Http::post('https://api.webflow.com/oauth/access_token',
             array_merge(
@@ -21,4 +38,40 @@ class Webflow
 
         return $response->json()['access_token'];
     }
+
+    public static function request($url, $data = [], $method = 'get', $ttl = 7200)
+    {
+        return Cache::remember('webflow_' . static::getToken() . '_' . $method . '_' . $url . '_' . sha1(print_r($data, true)), $ttl, function () use ($url, $method, $data) {
+            $response = Http::withToken(self::getToken())->$method(self::BASE_URL . $url, $data);
+            return $response->json();
+        });
+    }
+
+    public static function listSites()
+    {
+        return self::request('sites')['sites'];
+    }
+
+    public static function getSiteId($shortname)
+    {
+        foreach (self::listSites() as $s) {
+            if ($s['shortName'] === $shortname) {
+                return $s['id'];
+            }
+        }
+    }
+
+    public static function getPageMeta($shortname, $pageID)
+    {
+        foreach (self::listPages($shortname) as $page) {
+            if($page['id']==$pageID){
+                return $page;
+            }
+        }
+    }
+
+    public static function listPages($shortname)
+    {
+        return self::request('sites/' . self::getSiteId($shortname) . '/pages')['pages'];
+    }
 }