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 GuzzleHttp\Client as HTTPClient; |
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract Base Client Remote |
19
|
|
|
* |
20
|
|
|
* @author Welling Guzmán <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class BaseClientRemote extends AbstractClient |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Directus base url |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $baseUrl = 'http://localhost'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Directus hosted base url format |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $hostedBaseUrlFormat = 'https://%s.directus.io'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Directus Server base endpoint |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $baseEndpoint; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* API Version |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $apiVersion; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Directus Hosted endpoint format. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $hostedBaseEndpointFormat; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Directus Hosted Instance Key |
61
|
|
|
* |
62
|
|
|
* @var int|string |
63
|
|
|
*/ |
64
|
|
|
protected $instanceKey; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Authentication Token |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $accessToken; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* HTTP Client |
75
|
|
|
* |
76
|
|
|
* @var \GuzzleHttp\Client |
77
|
|
|
*/ |
78
|
|
|
protected $httpClient; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* HTTP Client request timeout |
82
|
|
|
* |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
protected $timeout = 60; |
86
|
|
|
|
87
|
|
|
const TABLE_ENTRIES_ENDPOINT = 'tables/%s/rows'; |
88
|
|
|
const TABLE_ENTRY_ENDPOINT = 'tables/%s/rows/%s'; |
89
|
|
|
const TABLE_ENTRY_CREATE_ENDPOINT = 'tables/%s/rows'; |
90
|
40 |
|
const TABLE_ENTRY_UPDATE_ENDPOINT = 'tables/%s/rows/%s'; |
91
|
|
|
const TABLE_ENTRY_DELETE_ENDPOINT = 'tables/%s/rows/%s'; |
92
|
40 |
|
const TABLE_LIST_ENDPOINT = 'tables'; |
93
|
|
|
const TABLE_INFORMATION_ENDPOINT = 'tables/%s'; |
94
|
40 |
|
const TABLE_PREFERENCES_ENDPOINT = 'tables/%s/preferences'; |
95
|
4 |
|
|
96
|
4 |
|
const COLUMN_LIST_ENDPOINT = 'tables/%s/columns'; |
97
|
|
|
const COLUMN_INFORMATION_ENDPOINT = 'tables/%s/columns/%s'; |
98
|
40 |
|
|
99
|
40 |
|
const GROUP_LIST_ENDPOINT = 'groups'; |
100
|
2 |
|
const GROUP_INFORMATION_ENDPOINT = 'groups/%s'; |
101
|
2 |
|
const GROUP_PRIVILEGES_ENDPOINT = 'privileges/%s'; |
102
|
2 |
|
|
103
|
|
|
const FILE_LIST_ENDPOINT = 'files'; |
104
|
40 |
|
const FILE_CREATE_ENDPOINT = 'files'; |
105
|
40 |
|
const FILE_INFORMATION_ENDPOINT = 'files/%s'; |
106
|
|
|
|
107
|
40 |
|
const SETTING_LIST_ENDPOINT = 'settings'; |
108
|
40 |
|
const SETTING_COLLECTION_ENDPOINT = 'settings/%s'; |
109
|
|
|
|
110
|
|
|
const MESSAGES_USER_ENDPOINT = 'messages/rows/%s'; |
111
|
|
|
|
112
|
|
|
public function __construct($accessToken, $options = []) |
113
|
|
|
{ |
114
|
|
|
$this->accessToken = $accessToken; |
115
|
4 |
|
|
116
|
|
|
if (isset($options['base_url'])) { |
117
|
4 |
|
$this->baseUrl = rtrim($options['base_url'], '/'); |
118
|
|
|
$this->baseEndpoint = $this->baseUrl . '/api'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$instanceKey = isset($options['instance_key']) ? $options['instance_key'] : false; |
122
|
|
|
if ($instanceKey) { |
123
|
|
|
$this->instanceKey = $instanceKey; |
124
|
|
|
$this->baseUrl = sprintf($this->hostedBaseUrlFormat, $instanceKey); |
125
|
4 |
|
$this->baseEndpoint = $this->baseUrl . '/api'; |
126
|
|
|
} |
127
|
4 |
|
|
128
|
|
|
$this->apiVersion = isset($options['version']) ? $options['version'] : 1; |
129
|
|
|
$this->baseEndpoint .= '/' . $this->getAPIVersion(); |
130
|
|
|
|
131
|
|
|
$this->setHTTPClient($this->getDefaultHTTPClient()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
2 |
|
* Get the base endpoint url |
136
|
|
|
* |
137
|
2 |
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getBaseEndpoint() |
140
|
|
|
{ |
141
|
|
|
return $this->baseEndpoint; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
2 |
|
* Get the base url |
146
|
|
|
* |
147
|
2 |
|
* @return string |
148
|
2 |
|
*/ |
149
|
|
|
public function getBaseUrl() |
150
|
|
|
{ |
151
|
|
|
return $this->baseUrl; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
4 |
|
* Get API Version |
156
|
|
|
* |
157
|
4 |
|
* @return int|string |
158
|
|
|
*/ |
159
|
|
|
public function getAPIVersion() |
160
|
|
|
{ |
161
|
|
|
return $this->apiVersion; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
40 |
|
* Get the authentication access token |
166
|
|
|
* |
167
|
40 |
|
* @return string |
168
|
40 |
|
*/ |
169
|
|
|
public function getAccessToken() |
170
|
|
|
{ |
171
|
|
|
return $this->accessToken; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
40 |
|
* Set a new authentication access token |
176
|
|
|
* |
177
|
40 |
|
* @param $newAccessToken |
178
|
|
|
*/ |
179
|
|
|
public function setAccessToken($newAccessToken) |
180
|
|
|
{ |
181
|
|
|
$this->accessToken = $newAccessToken; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
40 |
|
* Get the Directus hosted instance key |
186
|
|
|
* |
187
|
40 |
|
* @return null|string |
188
|
|
|
*/ |
189
|
|
|
public function getInstanceKey() |
190
|
28 |
|
{ |
191
|
|
|
return $this->instanceKey; |
192
|
28 |
|
} |
193
|
28 |
|
|
194
|
|
|
/** |
195
|
28 |
|
* Set the HTTP Client |
196
|
|
|
* |
197
|
|
|
* @param HTTPClient $httpClient |
198
|
|
|
*/ |
199
|
|
|
public function setHTTPClient(HTTPClient $httpClient) |
200
|
|
|
{ |
201
|
|
|
$this->httpClient = $httpClient; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the HTTP Client |
206
|
|
|
* |
207
|
30 |
|
* @return HTTPClient|null |
208
|
|
|
*/ |
209
|
30 |
|
public function getHTTPClient() |
210
|
30 |
|
{ |
211
|
30 |
|
return $this->httpClient; |
212
|
|
|
} |
213
|
30 |
|
|
214
|
|
|
/** |
215
|
|
|
* Get the default HTTP Client |
216
|
|
|
* |
217
|
|
|
* @return HTTPClient |
218
|
|
|
*/ |
219
|
|
|
public function getDefaultHTTPClient() |
220
|
|
|
{ |
221
|
|
|
return new HTTPClient(array('base_url' => rtrim($this->baseEndpoint, '/') . '/')); |
222
|
|
|
} |
223
|
|
|
|
224
|
32 |
|
public function performRequest($method, $pathFormat, $variables = [], array $body = [], array $query = []) |
225
|
|
|
{ |
226
|
32 |
|
$request = $this->buildRequest($method, $pathFormat, $variables, $body, $query); |
227
|
|
|
|
228
|
|
|
try { |
229
|
2 |
|
$response = $this->httpClient->send($request); |
230
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
231
|
|
|
return $this->createResponseFromData($content); |
232
|
|
|
} catch (ClientException $ex) { |
233
|
|
|
if ($ex->getResponse()->getStatusCode() == 401) { |
234
|
|
|
$message = sprintf('Unauthorized %s Request to %s', $request->getMethod(), $request->getUrl()); |
235
|
|
|
throw new UnauthorizedRequestException($message); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
throw $ex; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Build a request object |
244
|
|
|
* |
245
|
|
|
* @param $method |
246
|
|
|
* @param $pathFormat |
247
|
|
|
* @param $variables |
248
|
|
|
* @param $body |
249
|
|
|
* @param $query |
250
|
|
|
* |
251
|
|
|
* @return \GuzzleHttp\Message\Request |
252
|
|
|
*/ |
253
|
|
|
public function buildRequest($method, $pathFormat, $variables = [], array $body = [], array $query = []) |
254
|
|
|
{ |
255
|
|
|
$options = [ |
256
|
|
|
'auth' => [$this->accessToken, ''] |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
if ($method == 'POST' && $body) { |
|
|
|
|
260
|
|
|
$options['body'] = $body; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$request = $this->httpClient->createRequest($method, $this->buildPath($pathFormat, $variables), $options); |
264
|
|
|
|
265
|
|
|
if ($query) { |
|
|
|
|
266
|
|
|
$q = $request->getQuery(); |
267
|
|
|
foreach($query as $key => $value) { |
268
|
|
|
$q->set($key, $value); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $request; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Build a endpoint path based on a format |
277
|
|
|
* |
278
|
|
|
* @param string $pathFormat |
279
|
|
|
* @param array $variables |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function buildPath($pathFormat, $variables = []) |
284
|
|
|
{ |
285
|
|
|
return vsprintf(ltrim($pathFormat, '/'), $variables); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.