-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloris-api-wrapper.php
492 lines (444 loc) · 12 KB
/
loris-api-wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
class LorisApiWrapper {
/**
* A client object
* @var GuzzleHttp\Client
*/
private $client;
/**
* A key value array of options
* @var Array
*/
private $options;
/**
*
* Constructor
*
* @param string $api_url
* The url to the Loris api
* e.g. http://192.168.1.244/api/v0.0.2/
*
*/
public function __construct($api_url,$options=NULL){
$this->client = new Client([
'base_uri' => $api_url,
'timeout' => 2.0,
'options' =>$options
]);
}
/**
* A Login onto Loris
* GET /login
*
* @param string $username
* A Loris username
*
* @param string $password
* The password
*
* @return string $token | NULL
* A JWT (Json Web Token)
*/
public function login($username, $password){
$endpoint = "login";
$data = array(
'username' => $username,
'password' => $password
);
$result = $this->makePostRequest(NULL, $endpoint, $data);
if(!empty($result->token) && $result->token){
return $result->token;
} else{
// Problem: Some other problem has occurred. Return exception of some sort
return $result;
}
}
/**
* Get list of all projects in Loris
* GET /projects
*
* @param string $token
* A JWT token
*
* @return mixed $result
* The list of projects as objects created from the decoded json result
*/
public function getProjects($token){
$endpoint = "projects";
$params = array($token);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Get a specific project in Loris
* GET /projects/$ProjectName
*
* @param string $token
* A JWT token
*
* @param string $ProjectName
* A Loris project name
*
* @return mixed $result
* The project data as an object created from the decoded json result
*/
public function getProject($token, $ProjectName){
$endpoint = "projects/$ProjectName";
$params = array($token,$ProjectName);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Get a list of all candidates in a specific project
* GET /projects/$ProjectName/candidates/
*
* @param string $token
* A JWT token
*
* @param string $ProjectName
* A Loris project name
*
* @return mixed $result
* The object with a Candidates member array created from the decoded json result
*/
public function getProjectCandidates($token, $ProjectName=''){
$endpoint = "projects/$ProjectName/candidates";
$params = array($token,$ProjectName);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Get a specific candidate
* GET /candidates/$CandID
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @return mixed $result
* An object of all the images data information created from the decoded json result
*/
public function getCandidate($token, $CandID){
$endpoint = "candidates/$CandID";
$params = array($token, $CandID);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Get a specific candidate
* GET /candidates/$CandID
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @return mixed $result
* An object of all the images data information created from the decoded json result
*/
public function createCandidate($token, $data){
$endpoint = "candidates/";
$params = array($token);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
if(!isset($data['Candidate'],$data['Candidate']['Project'],$data['Candidate']['Gender'],$data['Candidate']['DoB'])){
echo $data['Project'], $data['Gender'], $data['DoB'];
return "Error: Invalid parametres. Verify that Project, Gender , DoB are set";
}
return $this->makePostRequest($token, $endpoint, $data);
}
/**
* Get a specific candidate visit data
* GET /candidates/$CandID/$VisitLabel
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @param string $VisitLabel
* The visit label
*
* @return mixed $result
* An object of all the images data information created from the decoded json result
*/
public function getCandidateVisit($token, $CandID, $VisitLabel){
$endpoint = "candidates/$CandID/$VisitLabel";
$params = array($token, $CandID, $VisitLabel);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Create a Visit Label for a specific candidate
* PUT /candidates/$CandID/$VisitLabel
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @param string $VisitLabel
* The visit label
*
* @param string $Battery
* The name of the sub-project
*
* @return mixed $result
* An object of all the images data information created from the decoded json result
*/
public function createCandidateVisit($token, $CandID, $VisitLabel, $Battery){
$endpoint = "candidates/$CandID/$VisitLabel";
$params = array($token, $CandID, $VisitLabel);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
$visit = array("CandID"=>$CandID, "Visit"=>$VisitLabel, "Battery"=>$Battery);
$meta = array('Meta'=>$visit);
return $this->makePutRequest($token, $endpoint, $meta);
}
/**
* Get all the images which have been acquired for a specific visit for a specific candidate
* GET /candidates/$CandID/$Visit/images
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @param string $Visit
* The visit label
*
* @return mixed $result
* An object of all the images data information created from the decoded json result
*/
public function getCandidateImages($token, $CandID, $Visit){
$endpoint = "candidates/$CandID/$Visit/images";
$params = array($token,$CandID,$Visit);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Return a raw file of an image with the appropriate MimeType headers for each Filename
* GET /candidates/$CandID/$VisitLabel/images/$Filename
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @param string $VisitLabel
* The visit label
*
* @param string $Filename
* The Filename to return
*
* @return mixed $body
* A raw file of an image with the appropriate MimeType headers for each Filename
*/
public function getImageData($token, $CandID, $VisitLabel, $Filename){
if(($is_valid = $this->isValidParams(array($token))) !== TRUE){
return $is_valid;
}
try{
$endpoint = "candidates/$CandID/$VisitLabel/images/$Filename";
$response = $this->client->get($endpoint,['headers'=>['Authorization'=>"Bearer $token"]]);
return $response->getBody();
}catch(RequestException $e){
$error_message = Psr7\str($e->getResponse());
return $error_message;
};
}
/**
* Get session level imaging QC data for a visit
* GET /candidates/$CandID/$Visit/images
* NOTE: API returns 404 and says requested url not found
*
* @param string $token
* A JWT token
*
* @param string $CandID
* The candidate ID
*
* @param string $Visit
* The visit label
*
* @return mixed $result
* An object of session level imaging QC data created from the decoded json result
*/
public function getSessionImagingQc($token, $CandID, $Visit){
$endpoint = "/candidates/$CandID/$Visit/qc/imaging";
$params = array($token,$CandID,$Visit);
if(($is_valid = $this->isValidParams($params)) !== TRUE){
return $is_valid;
}
return $this->makeGetRequest($token, $endpoint);
}
/**
* Make a request to the Loris api
*
* @param string $token
* A JWT token
*
* @param array $endpoint
* An api endpoint to send a request to
*
* @return mixed $result | Exception or Error
* $result IFF the request succeeds. An Exception / Error otherwise
*/
private function makeGetRequest($token, $endpoint){
$request = "GET";
return $this->makeRequest($request, $token, $endpoint);
}
/**
* Make a request to the Loris api
*
* @param string $token
* A JWT token
*
* @param array $endpoint
* An api endpoint to send a request to
*
* @param array $data
* The data to send in the post request
*
* @return mixed $result | Exception or Error
* $result IFF the request succeeds. An Exception / Error otherwise
*/
private function makePostRequest($token, $endpoint, $data){
$request = "POST";
return $this->makeRequest($request, $token, $endpoint, $data);
}
/**
* Make a request to the Loris api
*
* @param string $token
* A JWT token
*
* @param array $endpoint
* An api endpoint to send a request to
*
* @param array $data
* The data to send in the post request
*
* @return mixed $result | Exception or Error
* $result IFF the request succeeds. An Exception / Error otherwise
*/
private function makePutRequest($token, $endpoint, $data){
$request = "PUT";
return $this->makeRequest($request, $token, $endpoint, $data);
}
/**
* Make a request to the Loris api
*
* @param string $request
* The HTTP request verb (GET, POST, PUT, PATCH, DELETE)
*
* @param string $token
* A JWT token
*
* @param array $endpoint
* An api endpoint to send a request to
*
* @param array $data
* The data to send in the post request
*
* @return mixed $result | Exception or Error
* $result IFF the request succeeds. An Exception / Error otherwise
*/
private function makeRequest($request, $token, $endpoint, $data=NULL){
$options = [];
if(!empty($data)){
if($endpoint == "login"){
//$options = array('json' => $data);
$options['json'] = $data;
}else{
$data = json_encode($data);
//$options = array('body' => $data);
$options['body'] = $data;
}
}
echo "request: $request\n";
var_dump($options);
try{
if(!empty($token)){
$options['headers'] = ['Authorization'=>"Bearer $token"];
}
switch ($request) {
case 'GET':
$response = $this->client->get($endpoint, $options);
break;
case 'POST':
$response = $this->client->post($endpoint, $options);
break;
case 'PUT':
$response = $this->client->put($endpoint, $options);
break;
case 'PATCH':
$response = $this->client->patch($endpoint, $options);
break;
case 'DELETE':
$response = $this->client->delete($endpoint, $options);
break;
default:
# code...
break;
}
$body = $response->getBody();
if($body){
$result = json_decode($body);
return $result;
}
}catch(RequestException $e){
$response = $e->getResponse();
if(!empty($response)){
$error_message = Psr7\str($response);
return $error_message;
}
return $response;
};
}
/**
* A basic check or the parametres using isset() to see if they are set
* This does not check if the parametre value is legitimate or not
*
* @param array $params
* An array of parametres to check from the calling function
*
* @return mixed TRUE | An Error message
* TRUE IFF all the parametres are set, otherwise an error message
*/
public function isValidParams($params){
foreach($params as $param){
if(!isset($param)){
$error_message = "Error: Some or all parametres to the method have invalid values";
return $error_message;
}
}
return TRUE;
}
}
?>