Skip to content

Commit 482ce83

Browse files
authored
Get all records implementation (#21)
* Implementation of all() and updated README.md * fixed styling * another fix
1 parent 6793ee7 commit 482ce83

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,21 @@ If you need to support multiple tables, add them to the tables config in the con
6060
use Airtable;
6161
```
6262

63-
#### Get all records from that table.
63+
#### Get records from that table
64+
- This will only return the first 100 records due to Airtable page size limiation
65+
6466
``` php
6567
Airtable::table('tasks')->get();
6668
```
6769

70+
#### Get all records from that table.
71+
- This will get all records by sending multiple requests until all record are fetched.
72+
- Optional Parameter which is the delay between requests in microseconds as API is limited to 5 requests per second per base, defaults to 0.2 second.
73+
``` php
74+
Airtable::table('tasks')->all();
75+
Airtable::table('tasks')->all(500000); // 0.5 seconds
76+
```
77+
6878
#### Get one record from the default table.
6979
``` php
7080
Airtable::find('id_string');

src/Airtable.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public function get()
4141
return $this->toCollection($this->api->get());
4242
}
4343

44-
public function all()
44+
// There is delay between requests to avoid 429 error
45+
// 1000000 = 1 second
46+
// "The API is limited to 5 requests per second per base. If you exceed this rate, you will receive a 429 status code
47+
// and will need to wait 30 seconds before subsequent requests will succeed."
48+
public function all($delayBetweenRequestsInMicroseconds = 200000)
4549
{
46-
return $this->toCollection($this->api->getAllPages());
50+
return $this->toCollection($this->api->getAllPages($delayBetweenRequestsInMicroseconds));
4751
}
4852

4953
public function table($table)

src/Api/AirtableApiClient.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,39 @@ public function get(?string $id = null)
6868
return $this->decodeResponse($this->client->get($url));
6969
}
7070

71-
public function getAllPages()
71+
public function getAllPages($delayBetweenRequestsInMicroseconds)
7272
{
7373
$url = $this->getEndpointUrl();
7474

75-
$response = $this->client->get($url, [
76-
'query' => [
77-
'pageSize' => $this->pageSize,
78-
'maxRecords' => $this->maxRecords,
79-
],
80-
]);
75+
$records = [];
76+
$offset = false;
77+
78+
do {
79+
$query = []; // page sizes defaults to 100
80+
81+
if ($offset) {
82+
$query['offset'] = $offset;
83+
}
84+
85+
$response = $this->client->get($url, [
86+
'query' => $query,
87+
]);
88+
89+
$response = $this->decodeResponse($response);
90+
91+
if (isset($response['records'])) {
92+
$records = array_merge($response['records'], $records);
93+
}
8194

82-
//TODO: loop through offset to get more than one page when more than 100 records exist
95+
if (isset($response['offset'])) {
96+
$offset = $response['offset'];
97+
usleep($delayBetweenRequestsInMicroseconds);
98+
} else {
99+
$offset = false;
100+
}
101+
} while ($offset);
83102

84-
return $this->decodeResponse($response);
103+
return collect($records);
85104
}
86105

87106
public function post($contents = null)

0 commit comments

Comments
 (0)