-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathApi.php
More file actions
259 lines (215 loc) · 6.83 KB
/
Api.php
File metadata and controls
259 lines (215 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace Webflow;
use Webflow\WebflowException;
class Api
{
const WEBFLOW_API_ENDPOINT = 'https://api.webflow.com';
const WEBFLOW_API_USERAGENT = 'Expertlead Webflow PHP SDK (https://github.com/expertlead/webflow-php-sdk)';
private $client;
private $token;
private $requests;
private $start;
private $finish;
private $cache = [];
public function __construct(
$token,
$version = '1.0.0'
) {
if (empty($token)) {
throw new WebflowException('token');
}
$this->token = $token;
$this->version = $version;
$this->rateRemaining = 60;
return $this;
}
private function request(string $path, string $method, array $data = [])
{
$curl = curl_init();
$options = [
CURLOPT_URL => self::WEBFLOW_API_ENDPOINT . $path,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_USERAGENT => self::WEBFLOW_API_USERAGENT,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->token}",
"accept-version: {$this->version}",
"Accept: application/json",
"Content-Type: application/json",
],
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
];
if (!empty($data)) {
$json = json_encode($data);
$options[CURLOPT_POSTFIELDS] = $json;
$options[CURLOPT_HTTPHEADER][] = "Content-Length: " . strlen($json);
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
list($headers, $body) = explode("\r\n\r\n", $response, 2);
$body = mb_substr($response, curl_getinfo($curl, CURLINFO_HEADER_SIZE));
curl_close($curl);
return $this->parse($body);
}
private function get($path)
{
return $this->request($path, "GET");
}
private function post($path, $data)
{
return $this->request($path, "POST", $data);
}
private function put($path, $data)
{
return $this->request($path, "PUT", $data);
}
private function delete($path)
{
return $this->request($path, "DELETE");
}
private function parse($response)
{
$json = json_decode($response);
if (isset($json->code) && isset($json->msg)) {
$error = $json->msg;
if (isset($json->problems)) {
$error .= PHP_EOL . implode(PHP_EOL, $json->problems);
}
throw new \Exception($error, $json->code);
}
return $json;
}
// Meta
public function info()
{
return $this->get('/info');
}
public function sites()
{
return $this->get('/sites');
}
public function site(string $siteId)
{
return $this->get("/sites/{$siteId}");
}
public function domains(string $siteId)
{
return $this->get("/sites/{$siteId}/domains");
}
public function publishSite(string $siteId, array $domains)
{
return $this->post("/sites/${siteId}/publish", [
'domains' => $domains
]);
}
// Collections
public function collections(string $siteId)
{
return $this->get("/sites/{$siteId}/collections");
}
public function collection(string $collectionId)
{
return $this->get("/collections/{$collectionId}");
}
// Items
public function items(string $collectionId, int $offset = 0, int $limit = 100)
{
$query = http_build_query([
'offset' => $offset,
'limit' => $limit,
]);
return $this->get("/collections/{$collectionId}/items?{$query}");
}
public function itemsAll(string $collectionId): array
{
$response = $this->items($collectionId);
$items = $response->items;
$limit = $response->limit;
$total = $response->total;
$pages = ceil($total / $limit);
for ($page = 1; $page < $pages; $page++) {
$offset = $response->limit * $page;
$items = array_merge($items, $this->items($collectionId, $offset, $limit)->items);
}
return $items;
}
public function item(string $collectionId, string $itemId)
{
return $this->get("/collections/{$collectionId}/items/{$itemId}");
}
public function createItem(string $collectionId, array $fields)
{
$defaults = [
"_archived" => false,
"_draft" => false,
];
return $this->post("/collections/{$collectionId}/items", [
'fields' => array_merge($defaults, $fields),
]);
}
public function updateItem(string $collectionId, string $itemId, array $fields)
{
return $this->put("/collections/{$collectionId}/items/{$itemId}", [
'fields' => $fields,
]);
}
public function removeItem(string $collectionId, $itemId)
{
return $this->delete("/collections/{$collectionId}/items/{$itemId}");
}
public function findOrCreateItemByName(string $collectionId, array $fields)
{
if (!isset($fields['name'])) {
throw new WebflowException('name');
}
$cacheKey = "collection-{$collectionId}-items";
$instance = $this;
$items = $this->cache($cacheKey, function () use ($instance, $collectionId) {
return $instance->itemsAll($collectionId);
});
foreach ($items as $item) {
if (strcasecmp($item->name, $fields['name']) === 0) {
return $item;
}
}
$newItem = $this->createItem($collectionId, $fields);
$items[] = $newItem;
$this->cacheSet($cacheKey, $items);
return $newItem;
}
public function createOrUpdateItemByName(string $collectionId, array $fields)
{
if (!isset($fields['name'])) {
throw new WebflowException('name');
}
$items = $this->itemsAll($collectionId);
if(count($items) == 0){
throw new WebflowException('No items found');
}
foreach ($items as $item) {
if (strcasecmp($item->name, $fields['name']) === 0) {
//Add required fields
if(!array_key_exists('slug', $fields)){
$fields['slug'] = $item->slug;
}
//Update existing item
$this->updateItem($collectionId, $item->_id, $fields);
return $item;
}
}
//Create a new item
$newItem = $this->createItem($collectionId, $fields);
return $newItem;
}
private function cache($key, callable $callback)
{
if (!isset($this->cache[$key])) {
$this->cache[$key] = $callback();
}
return $this->cache[$key];
}
private function cacheSet($key, $value)
{
$this->cache[$key] = $value;
}
}