-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
113 lines (112 loc) · 4.65 KB
/
api.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
<?php
/******************************************************************************
* Icinga 2 API: PHP API client *
* Copyright (C) 2016 Icinga Development Team (https://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
# inspired by Icinga Director, ./library/Director/Core/RestApiClient.php
class ApiClient {
// http://php.net/manual/de/function.json-last-error.php#119985
protected $errorReference = [
JSON_ERROR_NONE => 'No error has occurred.',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.',
JSON_ERROR_SYNTAX => 'Syntax error.',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.',
// These last 3 messages are only supported on PHP >= 5.5.
// See http://php.net/json_last_error#refsect1-function.json-last-error-returnvalues
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.',
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.',
];
const JSON_UNKNOWN_ERROR = 'Unknown error.';
protected $version = 'v1';
protected $peer;
protected $port;
protected $user;
protected $pass;
protected $curl;
public function __construct($peer, $port = 5665) {
$this->peer = $peer;
$this->port = $port;
}
public function setCredentials($user, $pass) {
$this->user = $user;
$this->pass = $pass;
return $this;
}
protected function url($url) {
return sprintf('https://%s:%d/%s/%s', $this->peer, $this->port, $this->version, $url);
}
protected function curl() {
if ($this->curl === null) {
$this->curl = curl_init(sprintf('https://%s:%d', $this->peer, $this->port));
if (!$this->curl) {
throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl));
}
}
return $this->curl;
}
public function request($method, $url, $headers, $body) {
$auth = sprintf('%s:%s', $this->user, $this->pass);
$curlHeaders[] = "Accept: application/json";
if ($body !== null) {
$body = json_encode($body);
$curlHeaders[] = 'Content-Type: application/json';
}
if ($headers !== null) {
$curlFinalHeaders = array_merge($curlHeaders, $headers);
}
$curl = $this->curl();
$opts = array(
CURLOPT_URL => $this->url($url),
CURLOPT_HTTPHEADER => $curlFinalHeaders,
CURLOPT_USERPWD => $auth,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
);
if ($body !== null) {
$opts[CURLOPT_POSTFIELDS] = $body;
}
curl_setopt_array($curl, $opts);
$res = curl_exec($curl);
if ($res === false) {
throw new Exception('CURL ERROR: ' . curl_error($curl));
}
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode === 401) {
throw new Exception('Unable to authenticate, please check your API credentials');
}
return $this->fromJsonResult($res);
}
public function fromJsonResult($json) {
$result = @json_decode($json);
if ($result === null) {
throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error()));
}
return $result->results;
}
public function getLastJsonErrorMessage($errorCode) {
if (!array_key_exists($errorCode, $this->errorReference)) {
return self::JSON_UNKNOWN_ERROR;
}
return $this->errorReference[$errorCode];
}
}