|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Directus – <http://getdirectus.com> |
|
5
|
|
|
* |
|
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
|
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
|
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Directus\SDK; |
|
12
|
|
|
|
|
13
|
|
|
use Directus\SDK\Exception\UnauthorizedRequestException; |
|
14
|
|
|
use Directus\SDK\Response\Entry; |
|
15
|
|
|
use Directus\SDK\Response\EntryCollection; |
|
16
|
|
|
use Directus\Util\ArrayUtils; |
|
17
|
|
|
use GuzzleHttp\Client as HTTPClient; |
|
18
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Abstract Base Client Remote |
|
22
|
|
|
* |
|
23
|
|
|
* @author Welling Guzmán <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class BaseClientRemote extends AbstractClient |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Directus base url |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $baseUrl = 'http://localhost'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Directus hosted base url format |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $hostedBaseUrlFormat = 'https://%s.directus.io'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Directus Server base endpoint |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $baseEndpoint; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* API Version |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $apiVersion; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Directus Hosted endpoint format. |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $hostedBaseEndpointFormat; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Directus Hosted Instance Key |
|
64
|
|
|
* |
|
65
|
|
|
* @var int|string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $instanceKey; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Authentication Token |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $accessToken; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* HTTP Client |
|
78
|
|
|
* |
|
79
|
|
|
* @var \GuzzleHttp\Client |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $httpClient; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* HTTP Client request timeout |
|
85
|
|
|
* |
|
86
|
|
|
* @var int |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $timeout = 60; |
|
89
|
|
|
|
|
90
|
|
|
const ACTIVITY_GET_ENDPOINT = 'activity'; |
|
91
|
|
|
|
|
92
|
|
|
const BOOKMARKS_CREATE_ENDPOINT = 'bookmarks'; |
|
93
|
|
|
const BOOKMARKS_READ_ENDPOINT = 'bookmarks/%s'; |
|
94
|
|
|
const BOOKMARKS_DELETE_ENDPOINT = 'bookmarks/%s'; |
|
95
|
|
|
const BOOKMARKS_ALL_ENDPOINT = 'bookmarks'; |
|
96
|
|
|
const BOOKMARKS_USER_ENDPOINT = 'bookmarks/user/%s'; |
|
97
|
|
|
|
|
98
|
|
|
const TABLE_ENTRIES_ENDPOINT = 'tables/%s/rows'; |
|
99
|
|
|
const TABLE_ENTRY_ENDPOINT = 'tables/%s/rows/%s'; |
|
100
|
|
|
const TABLE_ENTRY_CREATE_ENDPOINT = 'tables/%s/rows'; |
|
101
|
|
|
const TABLE_ENTRY_UPDATE_ENDPOINT = 'tables/%s/rows/%s'; |
|
102
|
|
|
const TABLE_ENTRY_DELETE_ENDPOINT = 'tables/%s/rows/%s'; |
|
103
|
|
|
const TABLE_LIST_ENDPOINT = 'tables'; |
|
104
|
|
|
const TABLE_INFORMATION_ENDPOINT = 'tables/%s'; |
|
105
|
|
|
const TABLE_PREFERENCES_ENDPOINT = 'tables/%s/preferences'; |
|
106
|
|
|
const TABLE_CREATE_ENDPOINT = 'privileges/1'; // ID not being used but required @TODO: REMOVE IT |
|
107
|
|
|
const TABLE_DELETE_ENDPOINT = 'tables/%s'; |
|
108
|
|
|
|
|
109
|
|
|
const COLUMN_LIST_ENDPOINT = 'tables/%s/columns'; |
|
110
|
|
|
const COLUMN_CREATE_ENDPOINT = 'tables/%s/columns'; |
|
111
|
|
|
const COLUMN_DELETE_ENDPOINT = 'tables/%s/columns/%s'; |
|
112
|
|
|
const COLUMN_INFORMATION_ENDPOINT = 'tables/%s/columns/%s'; |
|
113
|
|
|
const COLUMN_OPTIONS_CREATE_ENDPOINT = 'tables/%s/columns/%s/%s'; |
|
114
|
|
|
|
|
115
|
|
|
const GROUP_LIST_ENDPOINT = 'groups'; |
|
116
|
|
|
const GROUP_CREATE_ENDPOINT = 'groups'; |
|
117
|
|
|
const GROUP_INFORMATION_ENDPOINT = 'groups/%s'; |
|
118
|
|
|
const GROUP_PRIVILEGES_ENDPOINT = 'privileges/%s'; |
|
119
|
|
|
const GROUP_PRIVILEGES_CREATE_ENDPOINT = 'privileges/%s'; |
|
120
|
|
|
|
|
121
|
|
|
const FILE_LIST_ENDPOINT = 'files'; |
|
122
|
|
|
const FILE_CREATE_ENDPOINT = 'files'; |
|
123
|
|
|
const FILE_UPDATE_ENDPOINT = 'files/%s'; |
|
124
|
|
|
const FILE_INFORMATION_ENDPOINT = 'files/%s'; |
|
125
|
|
|
|
|
126
|
|
|
const SETTING_LIST_ENDPOINT = 'settings'; |
|
127
|
|
|
const SETTING_COLLECTION_GET_ENDPOINT = 'settings/%s'; |
|
128
|
|
|
const SETTING_COLLECTION_UPDATE_ENDPOINT = 'settings/%s'; |
|
129
|
|
|
|
|
130
|
|
|
const MESSAGES_CREATE_ENDPOINT = 'messages/rows'; |
|
131
|
|
|
const MESSAGES_LIST_ENDPOINT = 'messages/rows'; |
|
132
|
|
|
const MESSAGES_GET_ENDPOINT = 'messages/rows/%s'; |
|
133
|
|
|
const MESSAGES_USER_LIST_ENDPOINT = 'messages/user/%s'; |
|
134
|
|
|
|
|
135
|
38 |
|
public function __construct($accessToken, $options = []) |
|
136
|
|
|
{ |
|
137
|
38 |
|
$this->accessToken = $accessToken; |
|
138
|
|
|
|
|
139
|
38 |
|
if (isset($options['base_url'])) { |
|
140
|
4 |
|
$this->baseUrl = rtrim($options['base_url'], '/'); |
|
141
|
4 |
|
$this->baseEndpoint = $this->baseUrl . '/api'; |
|
142
|
4 |
|
} |
|
143
|
|
|
|
|
144
|
38 |
|
$instanceKey = isset($options['instance_key']) ? $options['instance_key'] : false; |
|
145
|
38 |
|
if ($instanceKey) { |
|
146
|
2 |
|
$this->instanceKey = $instanceKey; |
|
147
|
2 |
|
$this->baseUrl = sprintf($this->hostedBaseUrlFormat, $instanceKey); |
|
148
|
2 |
|
$this->baseEndpoint = $this->baseUrl . '/api'; |
|
149
|
2 |
|
} |
|
150
|
|
|
|
|
151
|
38 |
|
$this->apiVersion = isset($options['version']) ? $options['version'] : 1; |
|
152
|
38 |
|
$this->baseEndpoint .= '/' . $this->getAPIVersion(); |
|
153
|
|
|
|
|
154
|
38 |
|
$this->setHTTPClient($this->getDefaultHTTPClient()); |
|
155
|
38 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get the base endpoint url |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
4 |
|
public function getBaseEndpoint() |
|
163
|
|
|
{ |
|
164
|
4 |
|
return $this->baseEndpoint; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the base url |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function getBaseUrl() |
|
173
|
|
|
{ |
|
174
|
2 |
|
return $this->baseUrl; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get API Version |
|
179
|
|
|
* |
|
180
|
|
|
* @return int|string |
|
181
|
|
|
*/ |
|
182
|
38 |
|
public function getAPIVersion() |
|
183
|
|
|
{ |
|
184
|
38 |
|
return $this->apiVersion; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get the authentication access token |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
2 |
|
public function getAccessToken() |
|
193
|
|
|
{ |
|
194
|
2 |
|
return $this->accessToken; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set a new authentication access token |
|
199
|
|
|
* |
|
200
|
|
|
* @param $newAccessToken |
|
201
|
|
|
*/ |
|
202
|
2 |
|
public function setAccessToken($newAccessToken) |
|
203
|
|
|
{ |
|
204
|
2 |
|
$this->accessToken = $newAccessToken; |
|
205
|
2 |
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get the Directus hosted instance key |
|
209
|
|
|
* |
|
210
|
|
|
* @return null|string |
|
211
|
|
|
*/ |
|
212
|
4 |
|
public function getInstanceKey() |
|
213
|
|
|
{ |
|
214
|
4 |
|
return $this->instanceKey; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Set the HTTP Client |
|
219
|
|
|
* |
|
220
|
|
|
* @param HTTPClient $httpClient |
|
221
|
|
|
*/ |
|
222
|
38 |
|
public function setHTTPClient(HTTPClient $httpClient) |
|
223
|
|
|
{ |
|
224
|
38 |
|
$this->httpClient = $httpClient; |
|
225
|
38 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get the HTTP Client |
|
229
|
|
|
* |
|
230
|
|
|
* @return HTTPClient|null |
|
231
|
|
|
*/ |
|
232
|
38 |
|
public function getHTTPClient() |
|
233
|
|
|
{ |
|
234
|
38 |
|
return $this->httpClient; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get the default HTTP Client |
|
239
|
|
|
* |
|
240
|
|
|
* @return HTTPClient |
|
241
|
|
|
*/ |
|
242
|
38 |
|
public function getDefaultHTTPClient() |
|
243
|
|
|
{ |
|
244
|
38 |
|
return new HTTPClient(array('base_url' => rtrim($this->baseEndpoint, '/') . '/')); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Perform a HTTP Request |
|
249
|
|
|
* |
|
250
|
|
|
* @param $method |
|
251
|
|
|
* @param $path |
|
252
|
|
|
* @param array $params |
|
253
|
|
|
* |
|
254
|
|
|
* @return Entry|EntryCollection |
|
255
|
|
|
* |
|
256
|
|
|
* @throws UnauthorizedRequestException |
|
257
|
|
|
*/ |
|
258
|
28 |
|
public function performRequest($method, $path, array $params = []) |
|
259
|
|
|
{ |
|
260
|
28 |
|
$request = $this->buildRequest($method, $path, $params); |
|
261
|
|
|
|
|
262
|
|
|
try { |
|
263
|
28 |
|
$response = $this->httpClient->send($request); |
|
264
|
28 |
|
$content = json_decode($response->getBody()->getContents(), true); |
|
265
|
28 |
|
return $this->createResponseFromData($content); |
|
266
|
|
|
} catch (ClientException $ex) { |
|
267
|
|
|
if ($ex->getResponse()->getStatusCode() == 401) { |
|
268
|
|
|
$message = sprintf('Unauthorized %s Request to %s', $request->getMethod(), $request->getUrl()); |
|
269
|
|
|
throw new UnauthorizedRequestException($message); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
throw $ex; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Build a request object |
|
278
|
|
|
* |
|
279
|
|
|
* @param $method |
|
280
|
|
|
* @param $path |
|
281
|
|
|
* @param $params |
|
282
|
|
|
* |
|
283
|
|
|
* @return \GuzzleHttp\Message\Request |
|
284
|
|
|
*/ |
|
285
|
30 |
|
public function buildRequest($method, $path, array $params = []) |
|
286
|
|
|
{ |
|
287
|
30 |
|
$body = ArrayUtils::get($params, 'body', []); |
|
288
|
30 |
|
$query = ArrayUtils::get($params, 'query', []); |
|
289
|
|
|
|
|
290
|
|
|
$options = [ |
|
291
|
30 |
|
'auth' => [$this->accessToken, ''] |
|
292
|
30 |
|
]; |
|
293
|
|
|
|
|
294
|
30 |
|
if (in_array($method, ['POST', 'PUT']) && $body) { |
|
295
|
|
|
$options['body'] = $body; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
30 |
|
$request = $this->httpClient->createRequest($method, $path, $options); |
|
299
|
|
|
|
|
300
|
30 |
|
if ($query) { |
|
301
|
|
|
$q = $request->getQuery(); |
|
302
|
|
|
foreach($query as $key => $value) { |
|
303
|
|
|
$q->set($key, $value); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
30 |
|
return $request; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Build a endpoint path based on a format |
|
312
|
|
|
* |
|
313
|
|
|
* @param string $pathFormat |
|
314
|
|
|
* @param string|array $variables |
|
315
|
|
|
* |
|
316
|
|
|
* @return string |
|
317
|
|
|
*/ |
|
318
|
24 |
|
public function buildPath($pathFormat, $variables = []) |
|
319
|
|
|
{ |
|
320
|
24 |
|
return vsprintf(ltrim($pathFormat, '/'), $variables); |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|