From 5012eaf8dc339fcd741f925807481dd446403f97 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 14 Mar 2024 10:21:02 +0100 Subject: [PATCH] wip #6775 @1 --- .../Operations/Tools/WebflowOperation.php | 2 +- app/Models/ToolWebflow.php | 4 +- app/Services/Webflow.php | 55 ++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php index 2599a7460..1942b039e 100644 --- a/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php +++ b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php @@ -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')); }); diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 58987cefb..699f150a1 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -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() diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index cdbf2a7d0..dc4729e6e 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -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']; + } } -- 2.39.5