@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function testProjectsListDetectInvalidAuthentication() |
48 | 48 | { |
49 | - $this->api->projects('list', ['project_group_id' => 123]); |
|
49 | + $this->api->projects('list', [ 'project_group_id' => 123 ]); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function testProjectsShowDetectInvalidAuthentication() |
67 | 67 | { |
68 | - $this->api->projects('show', ['project_id' => 123]); |
|
68 | + $this->api->projects('show', [ 'project_id' => 123 ]); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public function testProjectsCreateDetectMissingParam() |
85 | 85 | { |
86 | - $this->api->projects('create', ['project_group_id' => 123]); |
|
86 | + $this->api->projects('create', [ 'project_group_id' => 123 ]); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public function testProjectsUpdateDetectInvalidAuthentication() |
103 | 103 | { |
104 | - $this->api->projects('update', ['project_id' => 123]); |
|
104 | + $this->api->projects('update', [ 'project_id' => 123 ]); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | */ |
120 | 120 | public function testProjectsDeleteDetectInvalidAuthentication() |
121 | 121 | { |
122 | - $this->api->projects('delete', ['project_id' => 123]); |
|
122 | + $this->api->projects('delete', [ 'project_id' => 123 ]); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -137,6 +137,6 @@ discard block |
||
137 | 137 | */ |
138 | 138 | public function testProjectsLanguagesDetectInvalidAuthentication() |
139 | 139 | { |
140 | - $this->api->projects('languages', ['project_id' => 123]); |
|
140 | + $this->api->projects('languages', [ 'project_id' => 123 ]); |
|
141 | 141 | } |
142 | 142 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function testTranslationsExportDetectInvalidAuthentication() |
48 | 48 | { |
49 | - $this->api->translations('export', ['project_id' => 123]); |
|
49 | + $this->api->translations('export', [ 'project_id' => 123 ]); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function testTranslationsStatusDetectInvalidAuthentication() |
67 | 67 | { |
68 | - $this->api->translations('status', ['project_id' => 123]); |
|
68 | + $this->api->translations('status', [ 'project_id' => 123 ]); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function testFilesListDetectInvalidAuthentication() |
48 | 48 | { |
49 | - $this->api->files('list', ['project_id' => 123]); |
|
49 | + $this->api->files('list', [ 'project_id' => 123 ]); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function testFilesUploadDetectInvalidAuthentication() |
67 | 67 | { |
68 | - $this->api->files('upload', ['project_id' => 123]); |
|
68 | + $this->api->files('upload', [ 'project_id' => 123 ]); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -83,6 +83,6 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public function testFilesDeleteDetectInvalidAuthentication() |
85 | 85 | { |
86 | - $this->api->files('delete', ['project_id' => 123]); |
|
86 | + $this->api->files('delete', [ 'project_id' => 123 ]); |
|
87 | 87 | } |
88 | 88 | } |
@@ -1,421 +1,421 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - namespace Onesky\Api; |
|
4 | - |
|
5 | - /** |
|
6 | - * Onesky API wrapper PHP5 library |
|
7 | - */ |
|
8 | - class Client |
|
9 | - { |
|
10 | - /** |
|
11 | - * Onesky API endpoint |
|
12 | - */ |
|
13 | - protected $endpoint = 'https://platform.api.onesky.io/1'; |
|
14 | - |
|
15 | - /** |
|
16 | - * Client authenticate token |
|
17 | - */ |
|
18 | - protected $apiKey = null; |
|
19 | - |
|
20 | - /** |
|
21 | - * Client authenticate secret |
|
22 | - */ |
|
23 | - protected $secret = null; |
|
24 | - |
|
25 | - /** |
|
26 | - * Resources with actions |
|
27 | - */ |
|
28 | - protected $resources = array( |
|
29 | - 'project_groups' => array( |
|
30 | - 'list' => '/project-groups', |
|
31 | - 'show' => '/project-groups/:project_group_id', |
|
32 | - 'create' => '/project-groups', |
|
33 | - 'delete' => '/project-groups/:project_group_id', |
|
34 | - 'languages' => '/project-groups/:project_group_id/languages', |
|
35 | - ), |
|
36 | - 'projects' => array( |
|
37 | - 'list' => '/project-groups/:project_group_id/projects', |
|
38 | - 'show' => '/projects/:project_id', |
|
39 | - 'create' => '/project-groups/:project_group_id/projects', |
|
40 | - 'update' => '/projects/:project_id', |
|
41 | - 'delete' => '/projects/:project_id', |
|
42 | - 'languages' => '/projects/:project_id/languages', |
|
43 | - ), |
|
44 | - 'files' => array( |
|
45 | - 'list' => '/projects/:project_id/files', |
|
46 | - 'upload' => '/projects/:project_id/files', |
|
47 | - 'delete' => '/projects/:project_id/files', |
|
48 | - ), |
|
49 | - 'translations' => array( |
|
50 | - 'export' => '/projects/:project_id/translations', |
|
51 | - 'status' => '/projects/:project_id/translations/status', |
|
52 | - ), |
|
53 | - 'import_tasks' => array( |
|
54 | - 'list' => '/projects/:project_id/import-tasks/', |
|
55 | - 'show' => '/projects/:project_id/import-tasks/:import_id' |
|
56 | - ), |
|
57 | - 'quotations' => array( |
|
58 | - 'show' => '/projects/:project_id/quotations' |
|
59 | - ), |
|
60 | - 'orders' => array( |
|
61 | - 'list' => '/projects/:project_id/orders', |
|
62 | - 'show' => '/projects/:project_id/orders/:order_id', |
|
63 | - 'create' => '/projects/:project_id/orders' |
|
64 | - ), |
|
65 | - 'locales' => array( |
|
66 | - 'list' => '/locales' |
|
67 | - ), |
|
68 | - 'project_types' => array( |
|
69 | - 'list' => '/project-types' |
|
70 | - ), |
|
71 | - ); |
|
72 | - |
|
73 | - /** |
|
74 | - * Actions to use multipart to upload file |
|
75 | - */ |
|
76 | - protected $multiPartActions = array( |
|
77 | - 'files' => array('upload'), |
|
78 | - ); |
|
79 | - |
|
80 | - /** |
|
81 | - * Actions to use multipart to upload file |
|
82 | - */ |
|
83 | - protected $exportFileActions = array( |
|
84 | - 'translations' => array('export'), |
|
85 | - ); |
|
86 | - |
|
87 | - /** |
|
88 | - * Methods of actions mapping |
|
89 | - */ |
|
90 | - protected $methods = array( |
|
91 | - // 'get' => array('list', 'show', 'languages', 'export', 'status'), |
|
92 | - 'post' => array('create', 'upload'), |
|
93 | - 'put' => array('update'), |
|
94 | - 'delete' => array('delete'), |
|
95 | - ); |
|
96 | - |
|
97 | - /** |
|
98 | - * Default curl settings |
|
99 | - */ |
|
100 | - protected $curlSettings = array( |
|
101 | - CURLOPT_RETURNTRANSFER => true, |
|
102 | - CURLOPT_SSL_VERIFYPEER => true, |
|
103 | - CURLOPT_SSL_VERIFYHOST => 2 |
|
104 | - ); |
|
105 | - |
|
106 | - protected $httpHeaders = array( |
|
107 | - "Onesky-Plugin: php-wrapper", |
|
108 | - ); |
|
109 | - |
|
110 | - public function __construct() |
|
111 | - { |
|
112 | - if (!function_exists('curl_init')) { |
|
113 | - throw new \Exception('OneSky needs the CURL PHP extension.'); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @param string $apiKey |
|
119 | - * @return $this |
|
120 | - */ |
|
121 | - public function setApiKey($apiKey) |
|
122 | - { |
|
123 | - $this->apiKey = $apiKey; |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param string $secret |
|
129 | - * @return $this |
|
130 | - */ |
|
131 | - public function setSecret($secret) |
|
132 | - { |
|
133 | - $this->secret = $secret; |
|
134 | - return $this; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Retrieve resources |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function getResources() |
|
142 | - { |
|
143 | - return array_keys($this->resources); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Retrieve actions of a resource |
|
148 | - * @param string $resource Resource name |
|
149 | - * @return array|null |
|
150 | - */ |
|
151 | - public function getActionsByResource($resource) |
|
152 | - { |
|
153 | - if (!isset($this->resources[$resource])) { |
|
154 | - return null; // no resource found |
|
155 | - } |
|
156 | - |
|
157 | - $actions = array(); |
|
158 | - foreach ($this->resources[$resource] as $action => $path) { |
|
159 | - $actions[] = $action; |
|
160 | - } |
|
161 | - |
|
162 | - return $actions; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Retrieve the corresponding method to use |
|
167 | - * @param string $action Action name |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function getMethodByAction($action) |
|
171 | - { |
|
172 | - foreach ($this->methods as $method => $actions) { |
|
173 | - if (in_array($action, $actions)) { |
|
174 | - return $method; |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - return 'get'; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Determine if using mulit-part to upload file |
|
183 | - * @param string $resource Resource name |
|
184 | - * @param string $action Action name |
|
185 | - * @return boolean |
|
186 | - */ |
|
187 | - public function isMultiPartAction($resource, $action) |
|
188 | - { |
|
189 | - return isset($this->multiPartActions[$resource]) && in_array($action, $this->multiPartActions[$resource]); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Determine if it is to export (download) file |
|
194 | - * @param string $resource Resource name |
|
195 | - * @param string $action Action name |
|
196 | - * @return boolean |
|
197 | - */ |
|
198 | - public function isExportFileAction($resource, $action) |
|
199 | - { |
|
200 | - return isset($this->exportFileActions[$resource]) && in_array($action, $this->exportFileActions[$resource]); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * For developers to initial request to Onesky API |
|
205 | - * |
|
206 | - * Example: |
|
207 | - * $onesky = new Onesky_Api(); |
|
208 | - * $onesky->setApiKey('<api-key>')->setSecret('<api-secret>'); |
|
209 | - * |
|
210 | - * // To list project type |
|
211 | - * $onesky->projectTypes('list'); |
|
212 | - * |
|
213 | - * // To create project |
|
214 | - * $onesky->projects('create', array('project_group_id' => 999)); |
|
215 | - * |
|
216 | - * // To upload string file |
|
217 | - * $onesky->files('upload', array('project_id' => 1099, 'file' => 'path/to/file.yml', 'file_format' => 'YAML')); |
|
218 | - * |
|
219 | - * @param string $fn_name Function name acted as resource name |
|
220 | - * @param array $params Parameters passed in request |
|
221 | - * @return array Response |
|
222 | - */ |
|
223 | - public function __call($fn_name, $params) |
|
224 | - { |
|
225 | - // is valid resource |
|
226 | - $resource = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $fn_name)); // camelcase to underscore |
|
227 | - if (!in_array($resource, $this->getResources())) { |
|
228 | - throw new \BadMethodCallException('Invalid resource'); |
|
229 | - } |
|
230 | - |
|
231 | - // is valid action |
|
232 | - $action = array_shift($params); // action name |
|
233 | - if (!in_array($action, $this->getActionsByResource($resource))) { |
|
234 | - throw new \InvalidArgumentException('Invalid resource action'); |
|
235 | - } |
|
236 | - |
|
237 | - // parameters |
|
238 | - if (count($params) > 0) { |
|
239 | - $params = $this->normalizeParams(array_shift($params)); |
|
240 | - } else { |
|
241 | - $params = array(); |
|
242 | - } |
|
243 | - |
|
244 | - // get request method |
|
245 | - $method = $this->getMethodByAction($action); |
|
246 | - |
|
247 | - // get path |
|
248 | - $path = $this->getRequestPath($resource, $action, $params); |
|
249 | - |
|
250 | - // is multi-part |
|
251 | - $isMultiPart = $this->isMultiPartAction($resource, $action); |
|
252 | - |
|
253 | - // return response |
|
254 | - return $this->callApi($method, $path, $params, $isMultiPart); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Retrieve request path and replace variables with values |
|
259 | - * @param string $resource Resource name |
|
260 | - * @param string $action Action name |
|
261 | - * @param array $params Parameters |
|
262 | - * @return string Request path |
|
263 | - */ |
|
264 | - private function getRequestPath($resource, $action, &$params) |
|
265 | - { |
|
266 | - if (!isset($this->resources[$resource]) || !isset($this->resources[$resource][$action])) { |
|
267 | - throw new \UnexpectedValueException('Resource path not found'); |
|
268 | - } |
|
269 | - |
|
270 | - // get path |
|
271 | - $path = $this->resources[$resource][$action]; |
|
272 | - |
|
273 | - // replace variables |
|
274 | - $matchCount = preg_match_all("/:(\w*)/", $path, $variables); |
|
275 | - if ($matchCount) { |
|
276 | - foreach ($variables[0] as $index => $placeholder) { |
|
277 | - if (!isset($params[$variables[1][$index]])) { |
|
278 | - throw new \InvalidArgumentException('Missing parameter: ' . $variables[1][$index]); |
|
279 | - } |
|
280 | - |
|
281 | - $path = str_replace($placeholder, $params[$variables[1][$index]], $path); |
|
282 | - unset($params[$variables[1][$index]]); // remove parameter from $params |
|
283 | - } |
|
284 | - } |
|
285 | - |
|
286 | - return $path; |
|
287 | - } |
|
288 | - |
|
289 | - protected function verifyTokenAndSecret() |
|
290 | - { |
|
291 | - if (empty($this->apiKey) || empty($this->secret)) { |
|
292 | - throw new \UnexpectedValueException('Invalid authenticate data of api key or secret'); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Initial request to Onesky API |
|
298 | - * @param string $method |
|
299 | - * @param string $path |
|
300 | - * @param array $params |
|
301 | - * @param boolean $isMultiPart |
|
302 | - * @return array |
|
303 | - */ |
|
304 | - private function callApi($method, $path, $params, $isMultiPart) |
|
305 | - { |
|
306 | - // init session |
|
307 | - $ch = curl_init(); |
|
308 | - |
|
309 | - // request settings |
|
310 | - curl_setopt_array($ch, $this->curlSettings); // basic settings |
|
311 | - |
|
312 | - // url |
|
313 | - $url = $this->endpoint . $path; |
|
314 | - $url .= $method == 'get' ? $this->getAuthQueryStringWithParams($params) : $this->getAuthQueryString(); |
|
315 | - |
|
316 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
317 | - curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cert/StarfieldServicesRootCertificateAuthority-G2.pem'); |
|
318 | - |
|
319 | - // http header |
|
320 | - $requestHeaders = $this->httpHeaders; |
|
321 | - if (!$isMultiPart) { |
|
322 | - $requestHeaders[] = "Content-Type: application/json"; |
|
323 | - } |
|
324 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders); |
|
325 | - |
|
326 | - // method specific settings |
|
327 | - switch ($method) { |
|
328 | - case 'post': |
|
329 | - curl_setopt($ch, CURLOPT_POST, 1); |
|
330 | - |
|
331 | - // request body |
|
332 | - if ($isMultiPart) { |
|
333 | - if (version_compare(PHP_VERSION, '5.5.0') === -1) { |
|
334 | - // fallback to old method |
|
335 | - $params['file'] = '@' . $params['file']; |
|
336 | - } else { |
|
337 | - // make use of CURLFile |
|
338 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
339 | - $params['file'] = new \CURLFile($params['file']); |
|
340 | - } |
|
341 | - $postBody = $params; |
|
342 | - } else { |
|
343 | - $postBody = json_encode($params); |
|
344 | - } |
|
345 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); |
|
346 | - break; |
|
347 | - |
|
348 | - case 'put': |
|
349 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
350 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); |
|
351 | - break; |
|
352 | - |
|
353 | - case 'delete': |
|
354 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
355 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); |
|
356 | - break; |
|
357 | - } |
|
358 | - |
|
359 | - // execute request |
|
360 | - $response = curl_exec($ch); |
|
361 | - |
|
362 | - // error handling |
|
363 | - if ($response === false) { |
|
364 | - throw new \UnexpectedValueException(curl_error($ch)); |
|
365 | - } |
|
366 | - |
|
367 | - // close connection |
|
368 | - curl_close($ch); |
|
369 | - |
|
370 | - // return response |
|
371 | - return $response; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * @param $params |
|
376 | - * @return string |
|
377 | - */ |
|
378 | - private function getAuthQueryStringWithParams($params) |
|
379 | - { |
|
380 | - $queryString = $this->getAuthQueryString(); |
|
381 | - |
|
382 | - if (count($params) > 0) { |
|
383 | - $queryString .= '&' . http_build_query($params); |
|
384 | - } |
|
385 | - |
|
386 | - return $queryString; |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * @return string |
|
391 | - */ |
|
392 | - private function getAuthQueryString() |
|
393 | - { |
|
394 | - $this->verifyTokenAndSecret(); |
|
395 | - |
|
396 | - $timestamp = time(); |
|
397 | - $devHash = md5($timestamp . $this->secret); |
|
398 | - |
|
399 | - $queryString = '?api_key=' . $this->apiKey; |
|
400 | - $queryString .= '×tamp=' . $timestamp; |
|
401 | - $queryString .= '&dev_hash=' . $devHash; |
|
402 | - |
|
403 | - return $queryString; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param array $params |
|
408 | - * @return array |
|
409 | - */ |
|
410 | - private function normalizeParams(array $params) |
|
411 | - { |
|
412 | - // change boolean value to integer for curl |
|
413 | - foreach ($params as $key => $value) { |
|
414 | - if (is_bool($value)) { |
|
415 | - $params[$key] = (int)$value; |
|
416 | - } |
|
417 | - } |
|
418 | - |
|
419 | - return $params; |
|
420 | - } |
|
421 | - } |
|
3 | + namespace Onesky\Api; |
|
4 | + |
|
5 | + /** |
|
6 | + * Onesky API wrapper PHP5 library |
|
7 | + */ |
|
8 | + class Client |
|
9 | + { |
|
10 | + /** |
|
11 | + * Onesky API endpoint |
|
12 | + */ |
|
13 | + protected $endpoint = 'https://platform.api.onesky.io/1'; |
|
14 | + |
|
15 | + /** |
|
16 | + * Client authenticate token |
|
17 | + */ |
|
18 | + protected $apiKey = null; |
|
19 | + |
|
20 | + /** |
|
21 | + * Client authenticate secret |
|
22 | + */ |
|
23 | + protected $secret = null; |
|
24 | + |
|
25 | + /** |
|
26 | + * Resources with actions |
|
27 | + */ |
|
28 | + protected $resources = array( |
|
29 | + 'project_groups' => array( |
|
30 | + 'list' => '/project-groups', |
|
31 | + 'show' => '/project-groups/:project_group_id', |
|
32 | + 'create' => '/project-groups', |
|
33 | + 'delete' => '/project-groups/:project_group_id', |
|
34 | + 'languages' => '/project-groups/:project_group_id/languages', |
|
35 | + ), |
|
36 | + 'projects' => array( |
|
37 | + 'list' => '/project-groups/:project_group_id/projects', |
|
38 | + 'show' => '/projects/:project_id', |
|
39 | + 'create' => '/project-groups/:project_group_id/projects', |
|
40 | + 'update' => '/projects/:project_id', |
|
41 | + 'delete' => '/projects/:project_id', |
|
42 | + 'languages' => '/projects/:project_id/languages', |
|
43 | + ), |
|
44 | + 'files' => array( |
|
45 | + 'list' => '/projects/:project_id/files', |
|
46 | + 'upload' => '/projects/:project_id/files', |
|
47 | + 'delete' => '/projects/:project_id/files', |
|
48 | + ), |
|
49 | + 'translations' => array( |
|
50 | + 'export' => '/projects/:project_id/translations', |
|
51 | + 'status' => '/projects/:project_id/translations/status', |
|
52 | + ), |
|
53 | + 'import_tasks' => array( |
|
54 | + 'list' => '/projects/:project_id/import-tasks/', |
|
55 | + 'show' => '/projects/:project_id/import-tasks/:import_id' |
|
56 | + ), |
|
57 | + 'quotations' => array( |
|
58 | + 'show' => '/projects/:project_id/quotations' |
|
59 | + ), |
|
60 | + 'orders' => array( |
|
61 | + 'list' => '/projects/:project_id/orders', |
|
62 | + 'show' => '/projects/:project_id/orders/:order_id', |
|
63 | + 'create' => '/projects/:project_id/orders' |
|
64 | + ), |
|
65 | + 'locales' => array( |
|
66 | + 'list' => '/locales' |
|
67 | + ), |
|
68 | + 'project_types' => array( |
|
69 | + 'list' => '/project-types' |
|
70 | + ), |
|
71 | + ); |
|
72 | + |
|
73 | + /** |
|
74 | + * Actions to use multipart to upload file |
|
75 | + */ |
|
76 | + protected $multiPartActions = array( |
|
77 | + 'files' => array('upload'), |
|
78 | + ); |
|
79 | + |
|
80 | + /** |
|
81 | + * Actions to use multipart to upload file |
|
82 | + */ |
|
83 | + protected $exportFileActions = array( |
|
84 | + 'translations' => array('export'), |
|
85 | + ); |
|
86 | + |
|
87 | + /** |
|
88 | + * Methods of actions mapping |
|
89 | + */ |
|
90 | + protected $methods = array( |
|
91 | + // 'get' => array('list', 'show', 'languages', 'export', 'status'), |
|
92 | + 'post' => array('create', 'upload'), |
|
93 | + 'put' => array('update'), |
|
94 | + 'delete' => array('delete'), |
|
95 | + ); |
|
96 | + |
|
97 | + /** |
|
98 | + * Default curl settings |
|
99 | + */ |
|
100 | + protected $curlSettings = array( |
|
101 | + CURLOPT_RETURNTRANSFER => true, |
|
102 | + CURLOPT_SSL_VERIFYPEER => true, |
|
103 | + CURLOPT_SSL_VERIFYHOST => 2 |
|
104 | + ); |
|
105 | + |
|
106 | + protected $httpHeaders = array( |
|
107 | + "Onesky-Plugin: php-wrapper", |
|
108 | + ); |
|
109 | + |
|
110 | + public function __construct() |
|
111 | + { |
|
112 | + if (!function_exists('curl_init')) { |
|
113 | + throw new \Exception('OneSky needs the CURL PHP extension.'); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @param string $apiKey |
|
119 | + * @return $this |
|
120 | + */ |
|
121 | + public function setApiKey($apiKey) |
|
122 | + { |
|
123 | + $this->apiKey = $apiKey; |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param string $secret |
|
129 | + * @return $this |
|
130 | + */ |
|
131 | + public function setSecret($secret) |
|
132 | + { |
|
133 | + $this->secret = $secret; |
|
134 | + return $this; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Retrieve resources |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function getResources() |
|
142 | + { |
|
143 | + return array_keys($this->resources); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Retrieve actions of a resource |
|
148 | + * @param string $resource Resource name |
|
149 | + * @return array|null |
|
150 | + */ |
|
151 | + public function getActionsByResource($resource) |
|
152 | + { |
|
153 | + if (!isset($this->resources[$resource])) { |
|
154 | + return null; // no resource found |
|
155 | + } |
|
156 | + |
|
157 | + $actions = array(); |
|
158 | + foreach ($this->resources[$resource] as $action => $path) { |
|
159 | + $actions[] = $action; |
|
160 | + } |
|
161 | + |
|
162 | + return $actions; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Retrieve the corresponding method to use |
|
167 | + * @param string $action Action name |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function getMethodByAction($action) |
|
171 | + { |
|
172 | + foreach ($this->methods as $method => $actions) { |
|
173 | + if (in_array($action, $actions)) { |
|
174 | + return $method; |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + return 'get'; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Determine if using mulit-part to upload file |
|
183 | + * @param string $resource Resource name |
|
184 | + * @param string $action Action name |
|
185 | + * @return boolean |
|
186 | + */ |
|
187 | + public function isMultiPartAction($resource, $action) |
|
188 | + { |
|
189 | + return isset($this->multiPartActions[$resource]) && in_array($action, $this->multiPartActions[$resource]); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Determine if it is to export (download) file |
|
194 | + * @param string $resource Resource name |
|
195 | + * @param string $action Action name |
|
196 | + * @return boolean |
|
197 | + */ |
|
198 | + public function isExportFileAction($resource, $action) |
|
199 | + { |
|
200 | + return isset($this->exportFileActions[$resource]) && in_array($action, $this->exportFileActions[$resource]); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * For developers to initial request to Onesky API |
|
205 | + * |
|
206 | + * Example: |
|
207 | + * $onesky = new Onesky_Api(); |
|
208 | + * $onesky->setApiKey('<api-key>')->setSecret('<api-secret>'); |
|
209 | + * |
|
210 | + * // To list project type |
|
211 | + * $onesky->projectTypes('list'); |
|
212 | + * |
|
213 | + * // To create project |
|
214 | + * $onesky->projects('create', array('project_group_id' => 999)); |
|
215 | + * |
|
216 | + * // To upload string file |
|
217 | + * $onesky->files('upload', array('project_id' => 1099, 'file' => 'path/to/file.yml', 'file_format' => 'YAML')); |
|
218 | + * |
|
219 | + * @param string $fn_name Function name acted as resource name |
|
220 | + * @param array $params Parameters passed in request |
|
221 | + * @return array Response |
|
222 | + */ |
|
223 | + public function __call($fn_name, $params) |
|
224 | + { |
|
225 | + // is valid resource |
|
226 | + $resource = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $fn_name)); // camelcase to underscore |
|
227 | + if (!in_array($resource, $this->getResources())) { |
|
228 | + throw new \BadMethodCallException('Invalid resource'); |
|
229 | + } |
|
230 | + |
|
231 | + // is valid action |
|
232 | + $action = array_shift($params); // action name |
|
233 | + if (!in_array($action, $this->getActionsByResource($resource))) { |
|
234 | + throw new \InvalidArgumentException('Invalid resource action'); |
|
235 | + } |
|
236 | + |
|
237 | + // parameters |
|
238 | + if (count($params) > 0) { |
|
239 | + $params = $this->normalizeParams(array_shift($params)); |
|
240 | + } else { |
|
241 | + $params = array(); |
|
242 | + } |
|
243 | + |
|
244 | + // get request method |
|
245 | + $method = $this->getMethodByAction($action); |
|
246 | + |
|
247 | + // get path |
|
248 | + $path = $this->getRequestPath($resource, $action, $params); |
|
249 | + |
|
250 | + // is multi-part |
|
251 | + $isMultiPart = $this->isMultiPartAction($resource, $action); |
|
252 | + |
|
253 | + // return response |
|
254 | + return $this->callApi($method, $path, $params, $isMultiPart); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Retrieve request path and replace variables with values |
|
259 | + * @param string $resource Resource name |
|
260 | + * @param string $action Action name |
|
261 | + * @param array $params Parameters |
|
262 | + * @return string Request path |
|
263 | + */ |
|
264 | + private function getRequestPath($resource, $action, &$params) |
|
265 | + { |
|
266 | + if (!isset($this->resources[$resource]) || !isset($this->resources[$resource][$action])) { |
|
267 | + throw new \UnexpectedValueException('Resource path not found'); |
|
268 | + } |
|
269 | + |
|
270 | + // get path |
|
271 | + $path = $this->resources[$resource][$action]; |
|
272 | + |
|
273 | + // replace variables |
|
274 | + $matchCount = preg_match_all("/:(\w*)/", $path, $variables); |
|
275 | + if ($matchCount) { |
|
276 | + foreach ($variables[0] as $index => $placeholder) { |
|
277 | + if (!isset($params[$variables[1][$index]])) { |
|
278 | + throw new \InvalidArgumentException('Missing parameter: ' . $variables[1][$index]); |
|
279 | + } |
|
280 | + |
|
281 | + $path = str_replace($placeholder, $params[$variables[1][$index]], $path); |
|
282 | + unset($params[$variables[1][$index]]); // remove parameter from $params |
|
283 | + } |
|
284 | + } |
|
285 | + |
|
286 | + return $path; |
|
287 | + } |
|
288 | + |
|
289 | + protected function verifyTokenAndSecret() |
|
290 | + { |
|
291 | + if (empty($this->apiKey) || empty($this->secret)) { |
|
292 | + throw new \UnexpectedValueException('Invalid authenticate data of api key or secret'); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Initial request to Onesky API |
|
298 | + * @param string $method |
|
299 | + * @param string $path |
|
300 | + * @param array $params |
|
301 | + * @param boolean $isMultiPart |
|
302 | + * @return array |
|
303 | + */ |
|
304 | + private function callApi($method, $path, $params, $isMultiPart) |
|
305 | + { |
|
306 | + // init session |
|
307 | + $ch = curl_init(); |
|
308 | + |
|
309 | + // request settings |
|
310 | + curl_setopt_array($ch, $this->curlSettings); // basic settings |
|
311 | + |
|
312 | + // url |
|
313 | + $url = $this->endpoint . $path; |
|
314 | + $url .= $method == 'get' ? $this->getAuthQueryStringWithParams($params) : $this->getAuthQueryString(); |
|
315 | + |
|
316 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
317 | + curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cert/StarfieldServicesRootCertificateAuthority-G2.pem'); |
|
318 | + |
|
319 | + // http header |
|
320 | + $requestHeaders = $this->httpHeaders; |
|
321 | + if (!$isMultiPart) { |
|
322 | + $requestHeaders[] = "Content-Type: application/json"; |
|
323 | + } |
|
324 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders); |
|
325 | + |
|
326 | + // method specific settings |
|
327 | + switch ($method) { |
|
328 | + case 'post': |
|
329 | + curl_setopt($ch, CURLOPT_POST, 1); |
|
330 | + |
|
331 | + // request body |
|
332 | + if ($isMultiPart) { |
|
333 | + if (version_compare(PHP_VERSION, '5.5.0') === -1) { |
|
334 | + // fallback to old method |
|
335 | + $params['file'] = '@' . $params['file']; |
|
336 | + } else { |
|
337 | + // make use of CURLFile |
|
338 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
339 | + $params['file'] = new \CURLFile($params['file']); |
|
340 | + } |
|
341 | + $postBody = $params; |
|
342 | + } else { |
|
343 | + $postBody = json_encode($params); |
|
344 | + } |
|
345 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); |
|
346 | + break; |
|
347 | + |
|
348 | + case 'put': |
|
349 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
350 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); |
|
351 | + break; |
|
352 | + |
|
353 | + case 'delete': |
|
354 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
355 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); |
|
356 | + break; |
|
357 | + } |
|
358 | + |
|
359 | + // execute request |
|
360 | + $response = curl_exec($ch); |
|
361 | + |
|
362 | + // error handling |
|
363 | + if ($response === false) { |
|
364 | + throw new \UnexpectedValueException(curl_error($ch)); |
|
365 | + } |
|
366 | + |
|
367 | + // close connection |
|
368 | + curl_close($ch); |
|
369 | + |
|
370 | + // return response |
|
371 | + return $response; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * @param $params |
|
376 | + * @return string |
|
377 | + */ |
|
378 | + private function getAuthQueryStringWithParams($params) |
|
379 | + { |
|
380 | + $queryString = $this->getAuthQueryString(); |
|
381 | + |
|
382 | + if (count($params) > 0) { |
|
383 | + $queryString .= '&' . http_build_query($params); |
|
384 | + } |
|
385 | + |
|
386 | + return $queryString; |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * @return string |
|
391 | + */ |
|
392 | + private function getAuthQueryString() |
|
393 | + { |
|
394 | + $this->verifyTokenAndSecret(); |
|
395 | + |
|
396 | + $timestamp = time(); |
|
397 | + $devHash = md5($timestamp . $this->secret); |
|
398 | + |
|
399 | + $queryString = '?api_key=' . $this->apiKey; |
|
400 | + $queryString .= '×tamp=' . $timestamp; |
|
401 | + $queryString .= '&dev_hash=' . $devHash; |
|
402 | + |
|
403 | + return $queryString; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param array $params |
|
408 | + * @return array |
|
409 | + */ |
|
410 | + private function normalizeParams(array $params) |
|
411 | + { |
|
412 | + // change boolean value to integer for curl |
|
413 | + foreach ($params as $key => $value) { |
|
414 | + if (is_bool($value)) { |
|
415 | + $params[$key] = (int)$value; |
|
416 | + } |
|
417 | + } |
|
418 | + |
|
419 | + return $params; |
|
420 | + } |
|
421 | + } |
@@ -150,13 +150,13 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function getActionsByResource($resource) |
152 | 152 | { |
153 | - if (!isset($this->resources[$resource])) { |
|
153 | + if (!isset($this->resources[ $resource ])) { |
|
154 | 154 | return null; // no resource found |
155 | 155 | } |
156 | 156 | |
157 | 157 | $actions = array(); |
158 | - foreach ($this->resources[$resource] as $action => $path) { |
|
159 | - $actions[] = $action; |
|
158 | + foreach ($this->resources[ $resource ] as $action => $path) { |
|
159 | + $actions[ ] = $action; |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | return $actions; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | */ |
187 | 187 | public function isMultiPartAction($resource, $action) |
188 | 188 | { |
189 | - return isset($this->multiPartActions[$resource]) && in_array($action, $this->multiPartActions[$resource]); |
|
189 | + return isset($this->multiPartActions[ $resource ]) && in_array($action, $this->multiPartActions[ $resource ]); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | */ |
198 | 198 | public function isExportFileAction($resource, $action) |
199 | 199 | { |
200 | - return isset($this->exportFileActions[$resource]) && in_array($action, $this->exportFileActions[$resource]); |
|
200 | + return isset($this->exportFileActions[ $resource ]) && in_array($action, $this->exportFileActions[ $resource ]); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |
@@ -263,23 +263,23 @@ discard block |
||
263 | 263 | */ |
264 | 264 | private function getRequestPath($resource, $action, &$params) |
265 | 265 | { |
266 | - if (!isset($this->resources[$resource]) || !isset($this->resources[$resource][$action])) { |
|
266 | + if (!isset($this->resources[ $resource ]) || !isset($this->resources[ $resource ][ $action ])) { |
|
267 | 267 | throw new \UnexpectedValueException('Resource path not found'); |
268 | 268 | } |
269 | 269 | |
270 | 270 | // get path |
271 | - $path = $this->resources[$resource][$action]; |
|
271 | + $path = $this->resources[ $resource ][ $action ]; |
|
272 | 272 | |
273 | 273 | // replace variables |
274 | 274 | $matchCount = preg_match_all("/:(\w*)/", $path, $variables); |
275 | 275 | if ($matchCount) { |
276 | - foreach ($variables[0] as $index => $placeholder) { |
|
277 | - if (!isset($params[$variables[1][$index]])) { |
|
278 | - throw new \InvalidArgumentException('Missing parameter: ' . $variables[1][$index]); |
|
276 | + foreach ($variables[ 0 ] as $index => $placeholder) { |
|
277 | + if (!isset($params[ $variables[ 1 ][ $index ] ])) { |
|
278 | + throw new \InvalidArgumentException('Missing parameter: '.$variables[ 1 ][ $index ]); |
|
279 | 279 | } |
280 | 280 | |
281 | - $path = str_replace($placeholder, $params[$variables[1][$index]], $path); |
|
282 | - unset($params[$variables[1][$index]]); // remove parameter from $params |
|
281 | + $path = str_replace($placeholder, $params[ $variables[ 1 ][ $index ] ], $path); |
|
282 | + unset($params[ $variables[ 1 ][ $index ] ]); // remove parameter from $params |
|
283 | 283 | } |
284 | 284 | } |
285 | 285 | |
@@ -310,16 +310,16 @@ discard block |
||
310 | 310 | curl_setopt_array($ch, $this->curlSettings); // basic settings |
311 | 311 | |
312 | 312 | // url |
313 | - $url = $this->endpoint . $path; |
|
313 | + $url = $this->endpoint.$path; |
|
314 | 314 | $url .= $method == 'get' ? $this->getAuthQueryStringWithParams($params) : $this->getAuthQueryString(); |
315 | 315 | |
316 | 316 | curl_setopt($ch, CURLOPT_URL, $url); |
317 | - curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cert/StarfieldServicesRootCertificateAuthority-G2.pem'); |
|
317 | + curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/cert/StarfieldServicesRootCertificateAuthority-G2.pem'); |
|
318 | 318 | |
319 | 319 | // http header |
320 | 320 | $requestHeaders = $this->httpHeaders; |
321 | 321 | if (!$isMultiPart) { |
322 | - $requestHeaders[] = "Content-Type: application/json"; |
|
322 | + $requestHeaders[ ] = "Content-Type: application/json"; |
|
323 | 323 | } |
324 | 324 | curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders); |
325 | 325 | |
@@ -332,11 +332,11 @@ discard block |
||
332 | 332 | if ($isMultiPart) { |
333 | 333 | if (version_compare(PHP_VERSION, '5.5.0') === -1) { |
334 | 334 | // fallback to old method |
335 | - $params['file'] = '@' . $params['file']; |
|
335 | + $params[ 'file' ] = '@'.$params[ 'file' ]; |
|
336 | 336 | } else { |
337 | 337 | // make use of CURLFile |
338 | 338 | curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
339 | - $params['file'] = new \CURLFile($params['file']); |
|
339 | + $params[ 'file' ] = new \CURLFile($params[ 'file' ]); |
|
340 | 340 | } |
341 | 341 | $postBody = $params; |
342 | 342 | } else { |
@@ -380,7 +380,7 @@ discard block |
||
380 | 380 | $queryString = $this->getAuthQueryString(); |
381 | 381 | |
382 | 382 | if (count($params) > 0) { |
383 | - $queryString .= '&' . http_build_query($params); |
|
383 | + $queryString .= '&'.http_build_query($params); |
|
384 | 384 | } |
385 | 385 | |
386 | 386 | return $queryString; |
@@ -394,11 +394,11 @@ discard block |
||
394 | 394 | $this->verifyTokenAndSecret(); |
395 | 395 | |
396 | 396 | $timestamp = time(); |
397 | - $devHash = md5($timestamp . $this->secret); |
|
397 | + $devHash = md5($timestamp.$this->secret); |
|
398 | 398 | |
399 | - $queryString = '?api_key=' . $this->apiKey; |
|
400 | - $queryString .= '×tamp=' . $timestamp; |
|
401 | - $queryString .= '&dev_hash=' . $devHash; |
|
399 | + $queryString = '?api_key='.$this->apiKey; |
|
400 | + $queryString .= '×tamp='.$timestamp; |
|
401 | + $queryString .= '&dev_hash='.$devHash; |
|
402 | 402 | |
403 | 403 | return $queryString; |
404 | 404 | } |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | // change boolean value to integer for curl |
413 | 413 | foreach ($params as $key => $value) { |
414 | 414 | if (is_bool($value)) { |
415 | - $params[$key] = (int)$value; |
|
415 | + $params[ $key ] = (int) $value; |
|
416 | 416 | } |
417 | 417 | } |
418 | 418 |