-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClient.php
118 lines (99 loc) · 2.98 KB
/
Client.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
<?php
namespace Clue\React\SolusVM\Api;
use Clue\React\Buzz\Browser;
use Clue\React\Buzz\Message\Response;
class Client
{
private $browser;
private $key;
private $hash;
public function __construct(Browser $browser, $key, $hash)
{
$this->browser = $browser;
$this->key = $key;
$this->hash = $hash;
}
public function reboot()
{
return $this->request('reboot');
}
public function boot()
{
return $this->request('boot');
}
public function shutdown()
{
return $this->request('shutdown');
}
public function status()
{
return $this->request('status');
}
public function info($ipaddr = true, $hdd = true, $mem = true, $bw = true)
{
$args = array();
if ($ipaddr) {
$args['ipaddr'] = 'true';
}
if ($hdd) {
$args['hdd'] = 'true';
}
if ($mem) {
$args['mem'] = 'true';
}
if ($bw) {
$args['bw'] = 'true';
}
return $this->request('info', $args)->then(function ($data) {
$e = function ($d) {
return explode(',', $d);
};
$u = function ($d){
return array(
'total' => (float)$d[0],
'used' => (float)$d[1],
'free' => (float)$d[2],
'percentused' => (float)$d[3]
);
};
if (isset($data['ipaddr'])) {
$data['ipaddr'] = $e($data['ipaddr']);
}
foreach (array('hdd', 'mem', 'bw') as $key) {
if (isset($data[$key])) {
$data[$key] = $u($e($data[$key]));
}
}
return $data;
});
}
private function request($action, array $args = array())
{
return $this->browser->get(
$this->browser->resolve(
'{?key,hash,action,args*}',
array(
'action' => $action,
'args' => $args,
'key' => $this->key,
'hash' => $this->hash
)
)
)->then(
function (Response $response) {
preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', (string)$response->getBody(), $match);
$result = array();
foreach ($match[1] as $x => $y) {
$result[$y] = $match[2][$x];
}
if ($result['status'] === 'error') {
throw new \RuntimeException('Received error message "' . $result['statusmsg'] . '" from API', 0, new \RuntimeException($result['statusmsg']));
}
return $result;
},
function (\Exception $error) {
throw new \RuntimeException('Transport error "' . $error->getMessage() . '" while trying to access API', 0, $error);
}
);
}
}