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