2
2
3
3
namespace Tapp \Airtable \Api ;
4
4
5
+ use Illuminate \Support \Collection ;
5
6
use Illuminate \Support \Facades \Http ;
6
7
use Illuminate \Support \Str ;
7
8
@@ -18,10 +19,8 @@ class AirtableApiClient implements ApiClient
18
19
private $ fields = [];
19
20
private $ sorts = [];
20
21
private $ offset = false ;
21
- private $ pageSize = 100 ;
22
- private $ maxRecords = 100 ;
23
22
24
- public function __construct ($ base , $ table , $ access_token , $ httpLogFormat = null , Http $ client = null , $ typecast = false , $ delayBetweenRequests = 200000 )
23
+ public function __construct ($ base , $ table , $ access_token , Http $ client = null , $ typecast = false , $ delayBetweenRequests = 200000 )
25
24
{
26
25
$ this ->base = $ base ;
27
26
$ this ->table = $ table ;
@@ -42,14 +41,14 @@ private function buildClient($access_token)
42
41
]);
43
42
}
44
43
45
- public function addFilter ($ column , $ operation , $ value )
44
+ public function addFilter ($ column , $ operation , $ value ): AirtableApiClient
46
45
{
47
46
$ this ->filters [] = "{ {$ column }} {$ operation }\"{$ value }\"" ;
48
47
49
48
return $ this ;
50
49
}
51
50
52
- public function addSort (string $ column , string $ direction = 'asc ' )
51
+ public function addSort (string $ column , string $ direction = 'asc ' ): AirtableApiClient
53
52
{
54
53
if ($ direction === 'desc ' ) {
55
54
$ this ->sorts [] = ['field ' => $ column , 'direction ' => $ direction ];
@@ -60,7 +59,7 @@ public function addSort(string $column, string $direction = 'asc')
60
59
return $ this ;
61
60
}
62
61
63
- public function setTable ($ table )
62
+ public function setTable ($ table ): AirtableApiClient
64
63
{
65
64
$ this ->table = $ table ;
66
65
@@ -74,7 +73,7 @@ public function get(?string $id = null)
74
73
return $ this ->decodeResponse ($ this ->client ->get ($ url ));
75
74
}
76
75
77
- public function getAllPages ($ delayBetweenRequestsInMicroseconds )
76
+ public function getAllPages ($ delayBetweenRequestsInMicroseconds ): Collection
78
77
{
79
78
$ records = [];
80
79
@@ -100,7 +99,7 @@ public function post($contents = null)
100
99
{
101
100
$ url = $ this ->getEndpointUrl ();
102
101
103
- $ params = ['json ' => [ ' fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ] ];
102
+ $ params = ['fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ];
104
103
105
104
return $ this ->decodeResponse ($ this ->client ->post ($ url , $ params ));
106
105
}
@@ -109,7 +108,7 @@ public function put(string $id, $contents = null)
109
108
{
110
109
$ url = $ this ->getEndpointUrl ($ id );
111
110
112
- $ params = ['json ' => [ ' fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ] ];
111
+ $ params = ['fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ];
113
112
114
113
return $ this ->decodeResponse ($ this ->client ->put ($ url , $ params ));
115
114
}
@@ -118,20 +117,20 @@ public function patch(string $id, $contents = null)
118
117
{
119
118
$ url = $ this ->getEndpointUrl ($ id );
120
119
121
- $ params = ['json ' => [ ' fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ] ];
120
+ $ params = ['fields ' => (object ) $ contents , 'typecast ' => $ this ->typecast ];
122
121
123
122
return $ this ->decodeResponse ($ this ->client ->patch ($ url , $ params ));
124
123
}
125
124
126
- public function massUpdate (string $ method , array $ data )
125
+ public function massUpdate (string $ method , array $ data ): array
127
126
{
128
127
$ url = $ this ->getEndpointUrl ();
129
128
$ records = [];
130
129
131
130
// Update & Patch request body can include an array of up to 10 record objects
132
131
$ chunks = array_chunk ($ data , 10 );
133
132
foreach ($ chunks as $ key => $ data_chunk ) {
134
- $ params = ['json ' => [ ' records ' => $ data_chunk , 'typecast ' => $ this ->typecast ] ];
133
+ $ params = ['records ' => $ data_chunk , 'typecast ' => $ this ->typecast ];
135
134
136
135
$ response = $ this ->decodeResponse ($ this ->client ->$ method ($ url , $ params ));
137
136
$ records += $ response ['records ' ];
@@ -151,11 +150,9 @@ public function delete(string $id)
151
150
return $ this ->decodeResponse ($ this ->client ->delete ($ url ));
152
151
}
153
152
154
- public function responseToJson ($ response )
153
+ public function responseToJson ($ response ): string
155
154
{
156
- $ body = (string ) $ response ->getBody ();
157
-
158
- return $ body ;
155
+ return (string ) $ response ->getBody ();
159
156
}
160
157
161
158
public function responseToCollection ($ response )
@@ -182,7 +179,7 @@ public function decodeResponse($response)
182
179
return collect (json_decode ($ body , true ));
183
180
}
184
181
185
- public function setFields (?array $ fields )
182
+ public function setFields (?array $ fields ): AirtableApiClient
186
183
{
187
184
$ this ->fields = $ fields ;
188
185
0 commit comments