forked from EvilScott/junior
/
Client.php
160 lines (134 loc) · 4.65 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
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
<?php
namespace Junior;
use Junior\Clientside\Request,
Junior\Clientside\Response,
Junior\Clientside\Exception;
foreach(array('Request', 'Response', 'Exception') as $file) {
require_once('Junior'. DIRECTORY_SEPARATOR . 'Clientside' . DIRECTORY_SEPARATOR . $file . '.php');
}
class Client {
// create new client connection
public function __construct($uri)
{
$this->uri = $uri;
}
// shortcut to call a single, non-notification request
public function __call($method, $params)
{
$req = new Request($method, $params);
return $this->sendRequest($req);
}
// send a single request object
public function sendRequest($req)
{
$response = $this->send($req->getJSON());
if ($response->id != $req->id) {
throw new Clientside\Exception("Mismatched request id");
}
if(isset($response->error_code)) {
throw new Clientside\Exception("{$response->error_code} {$response->error_message}", $response->error_code);
}
return $response->result;
}
// send a single notify request object
public function sendNotify($req)
{
if (property_exists($req, 'id') && $req->id != null) {
throw new Clientside\Exception("Notify requests must not have ID set");
}
$this->send($req->getJSON(), true);
return true;
}
// send an array of request objects as a batch
public function sendBatch($reqs)
{
$arr = array();
$ids = array();
$all_notify = true;
foreach ($reqs as $req) {
if ($req->id) {
$all_notify = false;
$ids[] = $req->id;
}
$arr[] = $req->getArray();
}
$response = $this->send(json_encode($arr), $all_notify);
// no response if batch is all notifications
if ($all_notify) {
return true;
}
// check for missing ids and return responses in order of requests
$ordered_response = array();
foreach ($ids as $id) {
if (array_key_exists($id, $response)) {
$ordered_response[] = $response[$id];
unset($response[$id]);
} else {
throw new Clientside\Exception("Missing id in response");
}
}
// check for extra ids in response
if (count($response) > 0) {
throw new Clientside\Exception("Extra id(s) in response");
}
return $ordered_response;
}
// send raw json to the server
public function send($json, $notify = false)
{
// prepare data to be sent
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json\r\n',
'content' => $json));
$context = stream_context_create($opts);
// try to physically send data to destination
try {
$response = file_get_contents($this->uri, false, $context);
} catch (\Exception $e) {
$message = "Unable to connect to {$this->uri}";
$message .= PHP_EOL . $e->getMessage();
throw new Clientside\Exception($message);
}
// handle communication errors
if ($response === false) {
throw new Clientside\Exception("Unable to connect to {$this->uri}");
}
// notify has no response
if ($notify) {
return true;
}
// try to decode json
$json_response = $this->decodeJSON($response);
// handle response, create response object and return it
return $this->handleResponse($json_response);
}
// decode json throwing exception if unable
function decodeJSON($json)
{
$json_response = json_decode($json);
if ($json_response === null) {
throw new Clientside\Exception("Unable to decode JSON response from: {$json}");
}
return $json_response;
}
// handle the response and return a result or an error
public function handleResponse($response)
{
// recursion for batch
if (is_array($response)) {
$response_arr = array();
foreach ($response as $res) {
$response_arr[$res->id] = $this->handleResponse($res);
}
return $response_arr;
}
// return error response
if (property_exists($response, 'error')) {
return new Response(null, $response->id, $response->error->code, $response->error->message);
}
// return successful response
return new Response($response->result, $response->id);
}
}