-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBalihooRestClient.php
256 lines (206 loc) · 6.55 KB
/
BalihooRestClient.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
<?php
/*
* Balihoo Rest Helper lib
*
* Verion 0.1 Beta
*
* Licensed under the Apache license. See attached license file.
*/
class BalihooRestException extends Exception {}
class BalihooRestClient
{
const COMMAND_LOAD = 'load';
const COMMAND_COUNT = 'count';
const COMMAND_DISTINCT = 'distinct';
const COMMAND_QUERY = 'query';
const COMMAND_STATUS = 'status';
const COMMAND_GET = 'get';
const COMMAND_DELETE = 'delete';
const COMMAND_FIELDS = 'fields';
const COMMAND_COMMUNICATION = 'communication';
const VIEW_EMAIL = 'Email';
const VIEW_DIRECTMAIL = 'DirectMail';
const VIEW_MAINPHONE = 'MainPhone';
const VIEW_MOBILE = 'MobilePhone';
var $authKey;
var $proxy;
var $url;
var $insecure = false;
var $raw_http_response;
public function __construct($authKey, $url, $proxy= null)
{
$this->authKey = $authKey;
$this->proxy = $proxy;
$this->url = $url;
}
/**
* WARNING: only call this in sample or test environments with sample/test authKeys.
* do not send production authKeys over unencrypted channels.
* @return void
*/
public function setInsecure()
{
$this->insecure = true;
}
public function fields()
{
$command = BalihooRestClient::COMMAND_FIELDS;
$query = array('command'=>$command);
$result = $this->runQuery($command,$query, $this->getFullUrl());
return $result;
}
public function load($data)
{
$command = BalihooRestClient::COMMAND_LOAD;
$queryEncode = json_encode($data);
$query = array('command'=>$command, 'contacts'=>$queryEncode);
$result = $this->runQuery($command, $query, $this->getFullUrl());
return $result;
}
public function count($inQuery, $view)
{
$command = BalihooRestClient::COMMAND_COUNT;
$queryEncode = json_encode($inQuery);
$query = array('command'=>$command, 'query'=>$queryEncode, 'view'=>$view);
$result = $this->runQuery($command, $query, $this->getFullUrl());
return $result;
}
public function query($inQuery, $view, $limit = null)
{
$command = BalihooRestClient::COMMAND_QUERY;
$queryEncode = json_encode($inQuery);
$query = array('command'=>$command, 'query'=>$queryEncode, 'view'=>$view, 'limit'=>$limit);
$result = $this->runQuery($command, $query, $this->getFullUrl());
return $result;
}
public function status($id)
{
$command = BalihooRestClient::COMMAND_STATUS;
$overrideurl = $this->getFullUrl()."/".$command."/".$id;
$result = $this->runQuery($command, null, $overrideurl);
return $result;
}
public function get($id)
{
$command = BalihooRestClient::COMMAND_STATUS;
$overrideurl = $this->getFullUrl()."/".$id;
$result = $this->runQuery($command, null, $overrideurl);
return $result;
}
public function delete($id)
{
$command = BalihooRestClient::COMMAND_DELETE;
$overrideurl = $this->getFullUrl()."/".$id;
$result = $this->runQuery($command, null, $overrideurl);
return $result;
}
public function distinct($keys)
{
$command = self::COMMAND_DISTINCT;
$keysEncode = json_encode($keys);
$data = array('command'=>$command, 'keys'=>$keysEncode);
$result = $this->runQuery($command, $data, $this->getFullUrl());
return $result;
}
public function logCommunication($view, $communicationId, $keyCodes) {
$command = self::COMMAND_COMMUNICATION;
$keyCodesEncode = json_encode($keyCodes);
$data = array('command'=>$command, 'view'=>$view, 'communicationId'=>$communicationId, 'keyCodes'=>$keyCodesEncode);
$result = $this->runQuery($command, $data, $this->getFullUrl());
return $result;
}
private function runQuery($command, $data, $url)
{
$command = strtolower($command);
$header = array("X-Auth-Token:".$this->authKey);
$method = $this->getMethod($command);
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_HTTPHEADER,$header);
if (isset($this->proxy))
curl_setopt($s, CURLOPT_PROXY, $this->proxy);
curl_setopt($s,CURLOPT_HEADER, true);
curl_setopt($s,CURLOPT_TIMEOUT,100);
switch(strtoupper($method)) {
case "GET":
curl_setopt($s, CURLOPT_HTTPGET, TRUE);
break;
case "POST":
curl_setopt($s, CURLOPT_POST, 1);
curl_setopt($s, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($s, CURLOPT_CUSTOMREQUEST, $method);
break;
default:
// PUT/DELETE/etc need implementation
throw new BalihooRestException("unimplemented method");
}
curl_setopt($s,CURLOPT_MAXREDIRS,4);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($s);
$error = curl_error($s);
$result = array();
$header_size = curl_getinfo($s,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($s,CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($s,CURLINFO_EFFECTIVE_URL);
$this->checkError($error, $result);
$resultsDecode = json_decode($result['body'], true);
if (!$resultsDecode || !is_array($resultsDecode)) {
// make sure the results look like a json decoded object
$resultsDecode = array();
$resultsDecode['result'] = $result['body'];
}
$this->raw_http_response = $result;
curl_close($s);
return $resultsDecode;
}
private function checkError($error, $result)
{
if ( $error != "" )
{
throw new BalihooRestException("Curl error -- ".print_r($error,true));
}
// result code starting in 4
if (substr((string)$result['http_code'],0,1) == "4") {
throw new BalihooRestException("Error using Balihoo Rest Client :".$result['body']);
}
}
private function getFullUrl()
{
if (strstr($this->url, 'http') === false) {
if ($this->insecure)
$fullUrl = "http://".$this->url;
else
$fullUrl = "https://".$this->url;
} else {
$fullUrl = $this->url;
}
return $fullUrl;
}
private function getMethod($command)
{
switch(strtolower($command)) {
case BalihooRestClient::COMMAND_LOAD:
case BalihooRestClient::COMMAND_DISTINCT:
case BalihooRestClient::COMMAND_QUERY:
case BalihooRestClient::COMMAND_COUNT:
case BalihooRestClient::COMMAND_FIELDS:
case BalihooRestClient::COMMAND_COMMUNICATION:
$method = 'POST';
break;
case BalihooRestClient::COMMAND_STATUS:
case BalihooRestClient::COMMAND_GET:
$method = 'GET';
break;
case BalihooRestClient::COMMAND_DELETE:
$method = 'DELETE';
break;
default:
throw new BalihooRestException("No method associated with Command: ".$command);
}
return $method;
}
}