-
Notifications
You must be signed in to change notification settings - Fork 11
/
UcrmApi.php
239 lines (217 loc) · 5.95 KB
/
UcrmApi.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
<?php
/*
* This file is part of UCRM Plugin SDK.
*
* Copyright (c) 2019 Ubiquiti Inc.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ubnt\UcrmPluginSdk\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Ubnt\UcrmPluginSdk\Exception\ConfigurationException;
use Ubnt\UcrmPluginSdk\Exception\InvalidPluginRootPathException;
use Ubnt\UcrmPluginSdk\Exception\JsonException;
use Ubnt\UcrmPluginSdk\Util\Helpers;
use Ubnt\UcrmPluginSdk\Util\Json;
/**
* This class can be used to call UCRM API.
*
* You can find API documentation at https://help.ubnt.com/hc/en-us/articles/115003906007-UCRM-API-Usage
*/
class UcrmApi
{
private const HEADER_AUTH_APP_KEY = 'x-auth-app-key';
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $appKey;
public function __construct(Client $client, string $appKey)
{
$this->client = $client;
$this->appKey = $appKey;
}
/**
* Creates instance of UcrmApi class, using automatically generated plugin configuration
* to setup API URL and App Key.
*
* Example usage:
*
* $ucrmApi = UcrmApi::create();
*
* The `$pluginRootPath` parameter can be used to change root directory of plugin.
*
* @see AbstractOptionsManager::__construct() for more information.
*
* @throws ConfigurationException
* @throws InvalidPluginRootPathException
* @throws JsonException
*/
public static function create(?string $pluginRootPath = null): self
{
$ucrmOptionsManager = new UcrmOptionsManager($pluginRootPath);
$options = $ucrmOptionsManager->loadOptions();
$ucrmUrl = $options->ucrmLocalUrl ?? $options->ucrmPublicUrl ?? '';
if ($ucrmUrl === '') {
throw new ConfigurationException('UCRM URL is missing in plugin configuration.');
}
$ucrmApiUrl = sprintf(
'%s/api/v1.0/',
rtrim($ucrmUrl, '/')
);
$client = new Client(
[
'base_uri' => $ucrmApiUrl,
// If the URL is localhost over HTTPS, do not verify SSL certificate.
'verify' => ! Helpers::isUrlSecureLocalhost($ucrmApiUrl),
]
);
return new self($client, $options->pluginAppKey);
}
/**
* Sends a GET request to UCRM API.
*
* Example usage to get array of clients ordered by ID in descending order:
*
* $clients = $ucrmApi->get(
* 'clients',
* [
* 'order' => 'client.id',
* 'direction' => 'DESC'
* ]
* );
*
* @param mixed[] $query
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function get(string $endpoint, array $query = [])
{
$response = $this->request(
'GET',
$endpoint,
[
'query' => $query,
]
);
return $this->handleResponse($response);
}
/**
* Sends a POST request to UCRM API.
*
* Example usage to create a new client:
*
* $ucrmApi->post(
* 'clients',
* [
* 'firstName' => 'John',
* 'lastName' => 'Doe',
* ]
* );
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function post(string $endpoint, array $data = [])
{
$response = $this->request(
'POST',
$endpoint,
[
'json' => $data,
]
);
return $this->handleResponse($response);
}
/**
* Sends a PATCH request to UCRM API.
*
* Example usage to change first name of client with ID 42 to James:
*
* $ucrmApi->patch(
* 'clients/42',
* [
* 'firstName' => 'James',
* ]
* );
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function patch(string $endpoint, array $data = [])
{
$response = $this->request(
'PATCH',
$endpoint,
[
'json' => $data,
]
);
return $this->handleResponse($response);
}
/**
* Sends a DELETE request to UCRM API.
*
* Example usage to delete client with ID 42:
*
* $ucrmApi->delete('clients/42');
*
* @throws GuzzleException
*/
public function delete(string $endpoint): void
{
$this->request('DELETE', $endpoint);
}
/**
* @param mixed[] $options
*
* @throws GuzzleException
*/
private function request(string $method, string $endpoint, array $options = []): ResponseInterface
{
return $this->client->request(
$method,
// strip slash character from beginning of endpoint to make sure base API URL is included correctly
ltrim($endpoint, '/'),
array_merge(
$options,
[
'headers' => [
self::HEADER_AUTH_APP_KEY => $this->appKey,
],
]
)
);
}
/**
* @return mixed[]|string
*
* @throws JsonException
*/
private function handleResponse(ResponseInterface $response)
{
$responseBody = $response->getBody()->getContents();
if (stripos($response->getHeaderLine('content-type'), 'application/json') !== false) {
return $responseBody !== '' ? Json::decode($responseBody) : $responseBody;
}
return $responseBody;
}
}