1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Directus\SDK; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HTTPClient; |
6
|
|
|
|
7
|
|
|
abstract class BaseClient |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Directus Server base endpoint |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $baseEndpoint; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* API Version |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $apiVersion; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Directus Hosted endpoint format. |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $hostedBaseEndpointFormat; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Directus Hosted Instance Key |
29
|
|
|
* @var int|string |
30
|
|
|
*/ |
31
|
|
|
protected $instanceKey; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Authentication Token |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $accessToken; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HTTP Client |
41
|
|
|
* @var \GuzzleHttp\Client |
42
|
|
|
*/ |
43
|
|
|
protected $httpClient; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* HTTP Client request timeout |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $timeout = 60; |
50
|
|
|
|
51
|
|
|
const TABLE_ENTRIES_ENDPOINT = 'tables/%s/rows'; |
52
|
|
|
const TABLE_ENTRY_ENDPOINT = 'tables/%s/rows/%s'; |
53
|
|
|
const TABLE_LIST_ENDPOINT = 'tables'; |
54
|
|
|
const TABLE_INFORMATION_ENDPOINT = 'tables/%s'; |
55
|
|
|
const TABLE_PREFERENCES_ENDPOINT = 'tables/%s/preferences'; |
56
|
|
|
|
57
|
|
|
const COLUMN_LIST_ENDPOINT = 'tables/%s/columns'; |
58
|
|
|
const COLUMN_INFORMATION_ENDPOINT = 'tables/%s/columns/%s'; |
59
|
|
|
|
60
|
|
|
const GROUP_LIST_ENDPOINT = 'groups'; |
61
|
|
|
const GROUP_INFORMATION_ENDPOINT = 'groups/%s'; |
62
|
|
|
const GROUP_PRIVILEGES_ENDPOINT = 'privileges/%s'; |
63
|
|
|
|
64
|
|
|
const FILE_LIST_ENDPOINT = 'files'; |
65
|
|
|
const FILE_INFORMATION_ENDPOINT = 'files/%s'; |
66
|
|
|
|
67
|
|
|
const SETTING_LIST_ENDPOINT = 'settings'; |
68
|
|
|
const SETTING_COLLECTION_ENDPOINT = 'settings/%s'; |
69
|
|
|
|
70
|
38 |
|
public function __construct($accessToken, $options = []) |
71
|
|
|
{ |
72
|
38 |
|
$this->accessToken = $accessToken; |
73
|
|
|
|
74
|
38 |
|
if (isset($options['base_url'])) { |
75
|
4 |
|
$this->baseEndpoint = $options['base_url']; |
76
|
4 |
|
} |
77
|
|
|
|
78
|
38 |
|
$instanceKey = isset($options['instance_key']) ? $options['instance_key'] : false; |
79
|
38 |
|
if ($instanceKey) { |
80
|
2 |
|
$this->instanceKey = $instanceKey; |
81
|
2 |
|
$this->baseEndpoint = sprintf($this->hostedBaseEndpointFormat, $instanceKey); |
82
|
2 |
|
} |
83
|
|
|
|
84
|
38 |
|
$this->apiVersion = isset($options['version']) ? $options['version'] : 1; |
85
|
38 |
|
$this->baseEndpoint = rtrim(rtrim($this->baseEndpoint, '/').'/'.$this->apiVersion, '/').'/'; |
86
|
|
|
|
87
|
38 |
|
$this->setHTTPClient($this->getDefaultHTTPClient()); |
88
|
38 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the base endpoint url |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
4 |
|
public function getBaseEndpoint() |
95
|
|
|
{ |
96
|
4 |
|
return $this->baseEndpoint; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get API Version |
101
|
|
|
* @return int|string |
102
|
|
|
*/ |
103
|
4 |
|
public function getAPIVersion() |
104
|
|
|
{ |
105
|
4 |
|
return $this->apiVersion; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the authentication access token |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
2 |
|
public function getAccessToken() |
113
|
|
|
{ |
114
|
2 |
|
return $this->accessToken; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set a new authentication access token |
119
|
|
|
* @param $newAccessToken |
120
|
|
|
*/ |
121
|
2 |
|
public function setAccessToken($newAccessToken) |
122
|
|
|
{ |
123
|
2 |
|
$this->accessToken = $newAccessToken; |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the Directus hosted instance key |
128
|
|
|
* @return null|string |
129
|
|
|
*/ |
130
|
4 |
|
public function getInstanceKey() |
131
|
|
|
{ |
132
|
4 |
|
return $this->instanceKey; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the HTTP Client |
137
|
|
|
* @param HTTPClient $httpClient |
138
|
|
|
*/ |
139
|
38 |
|
public function setHTTPClient(HTTPClient $httpClient) |
140
|
|
|
{ |
141
|
38 |
|
$this->httpClient = $httpClient; |
142
|
38 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the HTTP Client |
146
|
|
|
* @return HTTPClient|null |
147
|
|
|
*/ |
148
|
38 |
|
public function getHTTPClient() |
149
|
|
|
{ |
150
|
38 |
|
return $this->httpClient; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the default HTTP Client |
155
|
|
|
* @return HTTPClient |
156
|
|
|
*/ |
157
|
38 |
|
public function getDefaultHTTPClient() |
158
|
|
|
{ |
159
|
38 |
|
return new HTTPClient(array('base_url' => $this->baseEndpoint)); |
160
|
|
|
} |
161
|
|
|
|
162
|
28 |
|
public function performRequest($method, $pathFormat, $variables = []) |
163
|
|
|
{ |
164
|
28 |
|
$request = $this->buildRequest($method, $pathFormat, $variables); |
165
|
28 |
|
$response = $this->httpClient->send($request); |
166
|
|
|
|
167
|
28 |
|
return json_decode($response->getBody()->getContents()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Build a request object |
172
|
|
|
* @param $method |
173
|
|
|
* @param $pathFormat |
174
|
|
|
* @param $variables |
175
|
|
|
* @return \GuzzleHttp\Message\Request |
176
|
|
|
*/ |
177
|
30 |
|
public function buildRequest($method, $pathFormat, $variables = []) |
178
|
|
|
{ |
179
|
30 |
|
$request = $this->httpClient->createRequest($method, $this->buildPath($pathFormat, $variables), [ |
180
|
30 |
|
'auth' => [$this->accessToken, ''] |
181
|
30 |
|
]); |
182
|
|
|
|
183
|
30 |
|
return $request; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Build a endpoint path based on a format |
188
|
|
|
* @param string $pathFormat |
189
|
|
|
* @param array $variables |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
32 |
|
public function buildPath($pathFormat, $variables = []) |
193
|
|
|
{ |
194
|
32 |
|
return vsprintf(ltrim($pathFormat, '/'), $variables); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|