Skip to content

Commit c0dd0a5

Browse files
committed
Add itemsAll method, removed requests rate control
1 parent e4eac37 commit c0dd0a5

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/Webflow/Api.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ function __construct(
3535
}
3636

3737
private function request(string $path, string $method, array $data = []) {
38-
if ($this->rateRemaining <= 1) {
39-
$sec = 60 + 30;
40-
// echo "Sleeping {$sec}s, X-RateLimit-Remaining: {$this->rateRemaining}", PHP_EOL;
41-
sleep($sec);
42-
}
4338
$curl = curl_init();
4439
$options = [
4540
CURLOPT_URL => self::WEBFLOW_API_ENDPOINT . $path,
@@ -62,20 +57,6 @@ private function request(string $path, string $method, array $data = []) {
6257
curl_setopt_array($curl, $options);
6358
$response = curl_exec($curl);
6459
curl_close($curl);
65-
list($headers, $body) = explode("\r\n\r\n", $response, 2);
66-
$headers = explode(PHP_EOL, $headers);
67-
foreach($headers as $header) {
68-
if (strpos($header, ': ') > 0) {
69-
list($headerName, $headerValue) = explode(': ', $header, 2);
70-
if ($headerName == 'X-RateLimit-Limit') {
71-
$rateLimit = $headerValue;
72-
}
73-
if ($headerName == 'X-RateLimit-Remaining') {
74-
$rateRemaining = intval($headerValue);
75-
}
76-
}
77-
}
78-
$this->rateRemaining = $rateRemaining ?? $this->rateRemaining - 1;
7960
return $this->parse($body);
8061

8162
}
@@ -140,8 +121,25 @@ public function collection(string $collectionId) {
140121

141122
// Items
142123

143-
public function items(string $collectionId) {
144-
return $this->get("/collections/{$collectionId}/items");
124+
public function items(string $collectionId, int $offset = 0, int $limit = 100) {
125+
$query = http_build_query([
126+
'offset' => $offset,
127+
'limit' => $limit,
128+
]);
129+
return $this->get("/collections/{$collectionId}/items?{$query}");
130+
}
131+
132+
public function itemsAll(string $collectionId): array {
133+
$response = $this->items($collectionId);
134+
$items = $response->items;
135+
$limit = $response->limit;
136+
$total = $response->total;
137+
$pages = ceil($total / $limit);
138+
for ($page = 1; $page < $pages; $page++){
139+
$offset = $response->limit * $page;
140+
$items = array_merge($items, $this->items($collectionId, $offset, $limit)->items);
141+
}
142+
return $items;
145143
}
146144

147145
public function item(string $collectionId, string $itemId) {
@@ -173,7 +171,7 @@ public function findOrCreateItemByName(string $collectionId, array $fields) {
173171
$cacheKey = "collection-{$collectionId}-items";
174172
$instance = $this;
175173
$items = $this->cache($cacheKey, function () use ($instance, $collectionId) {
176-
return $instance->items($collectionId)->items;
174+
return $instance->itemsAll($collectionId);
177175
});
178176
foreach ($items as $item) {
179177
if (strcasecmp($item->name, $fields['name']) === 0) {

0 commit comments

Comments
 (0)