1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FondOf\Airtable; |
4
|
|
|
|
5
|
|
|
use FondOf\Airtable\Exception\MarshalFieldsException; |
6
|
|
|
use FondOf\Airtable\Exception\NoWriteDataException; |
7
|
|
|
use FondOf\Airtable\Exception\MissingConfigException; |
8
|
|
|
use FondOf\Airtable\Service\HttpInterface; |
9
|
|
|
|
10
|
|
|
class ApiClient implements ApiClientInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string Airtable Api |
14
|
|
|
*/ |
15
|
|
|
protected $apiUrl = 'https://api.airtable.com'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string Airtable API Version |
19
|
|
|
*/ |
20
|
|
|
protected $apiVersion = 'v0'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Airtable base project |
24
|
|
|
*/ |
25
|
|
|
protected $base = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string Table in base project |
29
|
|
|
*/ |
30
|
|
|
protected $table = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int Max numbers of records to return |
34
|
|
|
*/ |
35
|
|
|
protected $limit = 10; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string Page offset if records > limit |
39
|
|
|
*/ |
40
|
|
|
protected $offset = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Airtable API key |
44
|
|
|
*/ |
45
|
|
|
private $apiKey = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Service\HttpInterface |
49
|
|
|
*/ |
50
|
|
|
private $service; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param HttpInterface $service |
55
|
|
|
* @param array<mixed> $config |
56
|
|
|
* |
57
|
|
|
* @throws MissingConfigException |
58
|
|
|
*/ |
59
|
6 |
|
public function __construct(HttpInterface $service, array $config) |
60
|
|
|
{ |
61
|
6 |
|
if (!isset($config['apiKey'])) { |
62
|
1 |
|
throw new MissingConfigException('Airtable API Key is missing from the Config'); |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
foreach ($config as $key => $value) { |
66
|
5 |
|
if (property_exists(self::class, (string)$key)) { |
67
|
5 |
|
$this->{$key} = $value; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
$this->service = $service; |
72
|
5 |
|
$this->service->addHeaders(['Authorization' => 'Bearer ' . $this->apiKey]); |
73
|
5 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $base |
77
|
|
|
* @param string $table |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function listRecords(string $base, string $table): string |
82
|
|
|
{ |
83
|
1 |
|
$params = '?maxRecords=' . $this->limit; |
84
|
1 |
|
return $this->service->get(sprintf('%s/%s/%s%s', $this->apiUrl(), $base, $table, $params)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $base Airtable base id |
89
|
|
|
* @param string $table Airtable table id |
90
|
|
|
* @param string $id Table record id |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
1 |
|
public function listRecord(string $base, string $table, string $id): string |
95
|
|
|
{ |
96
|
1 |
|
return $this->service->get(sprintf('%s/%s/%s/%s', $this->apiUrl(), $base, $table, $id)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $base Airtable base id |
101
|
|
|
* @param string $table Airtable table id |
102
|
|
|
* |
103
|
|
|
* @param array<string> $fields Array containing data for the new record |
104
|
|
|
* @return string |
105
|
|
|
* |
106
|
|
|
* @throws \FondOf\Airtable\Exception\NoWriteDataException |
107
|
|
|
* @throws \FondOf\Airtable\Exception\MarshalFieldsException |
108
|
|
|
*/ |
109
|
2 |
|
public function createRecord(string $base, string $table, array $fields): string |
110
|
|
|
{ |
111
|
2 |
|
if (empty($fields)) { |
112
|
1 |
|
throw new NoWriteDataException('Didn\'t receive data for write.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$data = [ |
116
|
1 |
|
'fields' => $fields |
117
|
|
|
]; |
118
|
|
|
|
119
|
1 |
|
$body = json_encode($data); |
120
|
|
|
|
121
|
1 |
|
if ($body === false) { |
122
|
|
|
throw new MarshalFieldsException('Couldn\'t encode fields.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $this->service->post(sprintf('%s/%s/%s', $this->apiUrl(), $base, $table), $body); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $limit Max number of records to return |
130
|
|
|
*/ |
131
|
|
|
public function setLimit(int $limit): void |
132
|
|
|
{ |
133
|
|
|
$this->limit = $limit; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
3 |
|
private function apiUrl(): string |
140
|
|
|
{ |
141
|
3 |
|
return $this->apiUrl . '/' . $this->apiVersion; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|