-
Notifications
You must be signed in to change notification settings - Fork 0
/
DispatcherProxy.php
98 lines (83 loc) · 2.81 KB
/
DispatcherProxy.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
<?php
/**
* User: Danilo G. Zutin
* Date: 11.11.15
* Time: 09:45
*/
namespace ExperimentEngine;
use \Httpful\Request;
class DispatcherProxy
{
private $base_url;
private $username;
private $password;
private $apikey;
public function __construct($base_url, $username, $password, $apikey)
{
$this->base_url = $base_url;
$this->username = $username;
$this->password = $password;
$this->apikey = $apikey;
}
public function searchNextExperiment()
{
try{
$response= Request::get($this->base_url.'/status')
->authenticateWith($this->username,$this->password)
->addHeader('X-apikey',$this->apikey)
->send();
}
catch (Exception $e) {
return array('is_Exception' => true,
'error_message' => $e->getMessage());
}
if ($response->code != 200){
return array('is_exception' => true,
'error_message' => "Request failed with HTTP Error ".$response->code);
}
return array('is_exception' => false,
'result' => (array)json_decode($response->body));
}
public function dequeueExperiment()
{
try{
$response= Request::get($this->base_url.'/experiment')
->authenticateWith($this->username,$this->password)
->addHeader('X-apikey',$this->apikey)
->send();
}
catch (Exception $e) {
return array('is_Exception' => true,
'error_message' => $e->getMessage());
}
if ($response->code != 200){
return array('is_exception' => true,
'error_message' => "Request failed with HTTP Error ".$response->code);
}
return array('is_exception' => false,
'result' => (array)json_decode($response->body));
}
public function sendResults($success, $results, $errorReport)
{
$httpBody = array('success' => $success,
'results' => $results,
'errorReport' => $errorReport);
try{
$response= Request::post($this->base_url.'/experiment')
->authenticateWith($this->username,$this->password)
->addHeader('X-apikey',$this->apikey)
->body(json_encode($httpBody))
->send();
}
catch (Exception $e) {
return array('is_Exception' => true,
'error_message' => $e->getMessage());
}
if ($response->code != 200){
return array('is_exception' => true,
'error_message' => "Request failed with HTTP Error ".$response->code);
}
return array('is_exception' => false,
'result' => (array)json_decode($response->body));
}
}