|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tapp\Airtable\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class AirtableApiClient implements ApiClient |
|
9
|
|
|
{ |
|
10
|
|
|
private $client; |
|
11
|
|
|
|
|
12
|
|
|
private $base; |
|
13
|
|
|
private $table; |
|
14
|
|
|
|
|
15
|
|
|
private $pageSize = 100; |
|
16
|
|
|
private $maxRecords = 100; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($base, $table, $access_token) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->base = $base; |
|
21
|
|
|
$this->table = $table; |
|
22
|
|
|
$this->client = $this->buildClient($access_token); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private function buildClient($access_token) |
|
26
|
|
|
{ |
|
27
|
|
|
return new Client([ |
|
28
|
|
|
'base_uri' => 'https://api.airtable.com', |
|
29
|
|
|
'headers' => [ |
|
30
|
|
|
'Authorization' => "Bearer {$access_token}", |
|
31
|
|
|
'content-type' => 'application/json', |
|
32
|
|
|
], |
|
33
|
|
|
]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function get(string $id = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$url = $this->getEndpointUrl($id); |
|
39
|
|
|
|
|
40
|
|
|
return $this->jsonToObject($this->client->get($url)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getAllPages() |
|
44
|
|
|
{ |
|
45
|
|
|
$url = $this->getEndpointUrl(); |
|
46
|
|
|
|
|
47
|
|
|
$response = $this->client->get($url, [ |
|
48
|
|
|
'query' => [ |
|
49
|
|
|
'pageSize' => $this->pageSize, |
|
50
|
|
|
'maxRecords' => $this->maxRecords, |
|
51
|
|
|
], |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
//TODO: loop through offset to get more than one page when more than 100 records exist |
|
55
|
|
|
|
|
56
|
|
|
return $this->jsonToObject($response); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function post($contents = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$url = $this->getEndpointUrl(); |
|
62
|
|
|
|
|
63
|
|
|
$params = ['json' => ['fields' => (object) $contents]]; |
|
64
|
|
|
|
|
65
|
|
|
return $this->jsonToObject($this->client->post($url, $params)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function put(string $id, $contents = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$url = $this->getEndpointUrl($id); |
|
71
|
|
|
|
|
72
|
|
|
$params = ['json' => ['fields' => (object) $contents]]; |
|
73
|
|
|
|
|
74
|
|
|
return $this->jsonToObject($this->client->put($url, $params)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function patch(string $id, $contents = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$url = $this->getEndpointUrl($id); |
|
80
|
|
|
|
|
81
|
|
|
$params = ['json' => ['fields' => (object) $contents]]; |
|
82
|
|
|
|
|
83
|
|
|
return $this->jsonToObject($this->client->patch($url, $params)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function delete(string $id) |
|
87
|
|
|
{ |
|
88
|
|
|
$url = $this->getEndpointUrl($id); |
|
89
|
|
|
|
|
90
|
|
|
return $this->jsonToObject($this->client->delete($url)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function responseToJson($response) |
|
94
|
|
|
{ |
|
95
|
|
|
$body = (string) $response->getBody(); |
|
96
|
|
|
|
|
97
|
|
|
return $body; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function jsonToObject($response) |
|
101
|
|
|
{ |
|
102
|
|
|
$body = (string) $response->getBody(); |
|
103
|
|
|
|
|
104
|
|
|
if ($body === '') { |
|
105
|
|
|
return collect([]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return collect(json_decode($body)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function jsonToArray($response) |
|
112
|
|
|
{ |
|
113
|
|
|
$body = (string) $response->getBody(); |
|
114
|
|
|
|
|
115
|
|
|
if ($body === '') { |
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return json_decode($body, true); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function getEndpointUrl(?string $id = null): string |
|
123
|
|
|
{ |
|
124
|
|
|
if ($id) { |
|
125
|
|
|
$url = '/v0/?/?/?'; |
|
126
|
|
|
|
|
127
|
|
|
return Str::replaceArray('?', [ |
|
128
|
|
|
$this->base, |
|
129
|
|
|
$this->table, |
|
130
|
|
|
$id, |
|
131
|
|
|
], $url); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$url = '/v0/?/?'; |
|
135
|
|
|
|
|
136
|
|
|
return Str::replaceArray('?', [ |
|
137
|
|
|
$this->base, |
|
138
|
|
|
$this->table, |
|
139
|
|
|
], $url); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|