1 | <?php |
||
2 | |||
3 | namespace BristolSU\AirTable; |
||
4 | |||
5 | use GuzzleHttp\Psr7\Response; |
||
6 | use Psr\Http\Message\ResponseInterface; |
||
7 | |||
8 | class AirTable |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * The ID of the Progress base to use |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private string $baseId; |
||
17 | |||
18 | /** |
||
19 | * The name of the table to use |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private string $tableName; |
||
24 | |||
25 | /** |
||
26 | * API key to use for authentication |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private string $apiKey; |
||
31 | |||
32 | /** |
||
33 | * @var \GuzzleHttp\Client |
||
34 | */ |
||
35 | private \GuzzleHttp\Client $client; |
||
36 | |||
37 | public static $rateLimitCooldown = 30; |
||
38 | |||
39 | 10 | public function __construct(\GuzzleHttp\Client $client) |
|
40 | { |
||
41 | 10 | $this->client = $client; |
|
42 | 10 | } |
|
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | 1 | public function getBaseId(): string |
|
48 | { |
||
49 | 1 | return $this->baseId; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $baseId |
||
54 | */ |
||
55 | 8 | public function setBaseId(string $baseId): void |
|
56 | { |
||
57 | 8 | $this->baseId = $baseId; |
|
58 | 8 | } |
|
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | 1 | public function getTableName(): string |
|
64 | { |
||
65 | 1 | return $this->tableName; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $tableName |
||
70 | */ |
||
71 | 8 | public function setTableName(string $tableName): void |
|
72 | { |
||
73 | 8 | $this->tableName = $tableName; |
|
74 | 8 | } |
|
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | 1 | public function getApiKey(): string |
|
80 | { |
||
81 | 1 | return $this->apiKey; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $apiKey |
||
86 | */ |
||
87 | 8 | public function setApiKey(string $apiKey): void |
|
88 | { |
||
89 | 8 | $this->apiKey = $apiKey; |
|
90 | 8 | } |
|
91 | |||
92 | /** |
||
93 | * @param string $method |
||
94 | * @param array|null $data |
||
95 | * |
||
96 | * @return ResponseInterface |
||
97 | * |
||
98 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
99 | */ |
||
100 | 7 | protected function request(string $method = 'get', array $data = null): ResponseInterface |
|
101 | { |
||
102 | $options = [ |
||
103 | 'headers' => [ |
||
104 | 7 | 'Authorization' => 'Bearer ' . $this->apiKey |
|
105 | ] |
||
106 | ]; |
||
107 | 7 | if($data !== null) { |
|
108 | 7 | if($method === 'get' || $method === 'delete') { |
|
109 | 4 | $options['query'] = $data; |
|
110 | } else { |
||
111 | 3 | $options['json'] = $data; |
|
112 | } |
||
113 | } |
||
114 | 7 | return $this->client->request($method, |
|
115 | 7 | sprintf('https://api.airtable.com/v0/%s/%s', $this->baseId, $this->tableName), |
|
116 | $options |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | 7 | protected function execute($dataChunk, \Closure $execution) |
|
121 | { |
||
122 | try { |
||
123 | 7 | return $execution->call($this, $dataChunk); |
|
124 | 1 | } catch (\GuzzleHttp\Exception\ClientException $exception) { |
|
125 | 1 | if($exception->getCode() === 429) { |
|
126 | 1 | sleep(static::$rateLimitCooldown); |
|
127 | 1 | return $this->execute($dataChunk, $execution); |
|
128 | } else { |
||
129 | throw $exception; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | 5 | protected function chunkAndThrottle(array $data, \Closure $execution, int $delay = 1, int $chunkSize = 10) |
|
135 | { |
||
136 | 5 | $chunkedData = array_chunk($data, $chunkSize, false); |
|
137 | 5 | foreach($chunkedData as $key => $dataChunk) { |
|
138 | 5 | $this->execute($dataChunk, $execution); |
|
139 | 5 | if($key !== array_key_last($chunkedData)) { |
|
140 | 3 | sleep($delay); |
|
141 | } |
||
142 | } |
||
143 | 5 | } |
|
144 | |||
145 | /** |
||
146 | * Create Rows |
||
147 | * |
||
148 | * @param array $rows An array of arrays with the key as the field name and the value as the field value. E.g. |
||
149 | * [ |
||
150 | * ['Field1' => 'Val1', 'Field2' => 'Val2'], |
||
151 | * ['Field1' => 'Val3', 'Field2' => 'Val3'] |
||
152 | * ] |
||
153 | * @param bool $typecast |
||
154 | * |
||
155 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
156 | */ |
||
157 | 3 | public function createRows(array $rows, bool $typecast = true) |
|
158 | { |
||
159 | 3 | $data = []; |
|
160 | 3 | foreach($rows as $row) { |
|
161 | 3 | $data[] = ['fields' => $row]; |
|
162 | } |
||
163 | |||
164 | $this->chunkAndThrottle($data, function($rowsToCreate) use ($typecast) { |
||
165 | 3 | $this->request('post', [ |
|
166 | 3 | 'records' => $rowsToCreate, 'typecast' => $typecast |
|
167 | ]); |
||
168 | 3 | }); |
|
169 | 3 | } |
|
170 | |||
171 | /** |
||
172 | * Delete Rows |
||
173 | * |
||
174 | * @param array $rowIds An array of row IDs to delete. E.g. ['rec123', 'rec456'] |
||
175 | * |
||
176 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
177 | */ |
||
178 | 2 | public function deleteRows(array $rowIds) |
|
179 | { |
||
180 | $this->chunkAndThrottle($rowIds, function($rowIds) { |
||
181 | 2 | $this->request('delete', [ |
|
182 | 2 | 'records' => $rowIds |
|
183 | ]); |
||
184 | 2 | }); |
|
185 | 2 | } |
|
186 | |||
187 | 3 | public function retrieveRecords() |
|
188 | { |
||
189 | 3 | $records = []; |
|
190 | 3 | $offset = null; |
|
191 | do { |
||
192 | $response = $this->execute([], function($data) use ($offset) { |
||
0 ignored issues
–
show
|
|||
193 | 3 | return $this->request('get', [ |
|
194 | 3 | 'offset' => $offset |
|
195 | ]); |
||
196 | 3 | }); |
|
197 | 3 | $responseData = json_decode($response->getBody()->getContents(), true); |
|
198 | |||
199 | try { |
||
200 | 3 | $newRecords = $responseData['records']; |
|
201 | } catch (\Exception $e) { |
||
202 | $newRecords = []; |
||
203 | } |
||
204 | |||
205 | 3 | $records = array_merge($records, $newRecords); |
|
206 | 3 | if(array_key_exists('offset', $responseData)) { |
|
207 | 3 | $offset = $responseData['offset']; |
|
208 | } else { |
||
209 | 3 | $offset = null; |
|
210 | } |
||
211 | 3 | } while($offset !== null); |
|
212 | 3 | return $records; |
|
213 | } |
||
214 | |||
215 | 2 | public function getIdsFromTable(): array |
|
216 | { |
||
217 | 2 | $ids = []; |
|
218 | 2 | foreach($this->retrieveRecords() as $record) { |
|
219 | 2 | $ids[] = $record['id']; |
|
220 | } |
||
221 | 2 | return $ids; |
|
222 | } |
||
223 | |||
224 | 1 | public function flushTable() |
|
225 | { |
||
226 | 1 | $this->deleteRows( |
|
227 | 1 | $this->getIdsFromTable() |
|
228 | ); |
||
229 | 1 | } |
|
230 | |||
231 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.