-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.php
67 lines (57 loc) · 2.34 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
<?php
namespace Mango;
require_once "errors.php";
class Client {
const BASE_URL = "https://api.getmango.com/v1";
public function request($method, $url, $api_key, $data = NULL, $headers= array("Content-Type" => "application/json", "Accept" => "application/json") ) {
try {
$options = array(
'auth' => new \Requests_Auth_Basic(array($api_key, ''))
);
if($method == "GET") {
$url_params = is_array($data) ? '?' . http_build_query($data) : '';
$response = \Requests::get(Client::BASE_URL . $url . $url_params, $headers, $options);
} else if($method == "POST") {
$response = \Requests::post(Client::BASE_URL . $url, $headers, json_encode($data), $options);
} else if($method == "PATCH") {
$response = \Requests::patch(Client::BASE_URL . $url, $headers, json_encode($data), $options);
} else if($method == "DELETE") {
$response = \Requests::delete(Client::BASE_URL . $url, $headers, $options);
}
} catch (\Exception $e) {
throw new UnableToConnect();
}
if ($response->status_code >= 200 && $response->status_code <= 206) {
if ($method == "DELETE") {
return $response->status_code == 204 || $response->status_code == 200;
}
return json_decode($response->body);
}
if ($response->status_code == 400) {
$code = 0;
$message = "";
try {
$error = (array) json_decode($response->body)->errors[0];
$code = key($error);
$message = current($error);
} catch (\Exception $e) {
throw new UnhandledError($response->body, $response->status_code);
}
throw new InputValidationError($message, $code);
}
if ($response->status_code == 401) {
throw new AuthenticationError();
}
if ($response->status_code == 404) {
throw new NotFound();
}
if ($response->status_code == 403) {
throw new InvalidApiKey();
}
if ($response->status_code == 405) {
throw new MethodNotAllowed();
}
throw new UnhandledError($response->body, $response->status_code);
}
}
?>