-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathHetznerAPIClient.php
More file actions
343 lines (298 loc) · 7.13 KB
/
HetznerAPIClient.php
File metadata and controls
343 lines (298 loc) · 7.13 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
namespace LKDev\HetznerCloud;
use GuzzleHttp\Client;
use LKDev\HetznerCloud\Clients\GuzzleClient;
use LKDev\HetznerCloud\Models\Actions\Actions;
use LKDev\HetznerCloud\Models\Certificates\Certificates;
use LKDev\HetznerCloud\Models\Datacenters\Datacenters;
use LKDev\HetznerCloud\Models\Firewalls\Firewalls;
use LKDev\HetznerCloud\Models\FloatingIps\FloatingIps;
use LKDev\HetznerCloud\Models\Images\Images;
use LKDev\HetznerCloud\Models\ISOs\ISOs;
use LKDev\HetznerCloud\Models\LoadBalancers\LoadBalancers;
use LKDev\HetznerCloud\Models\LoadBalancerTypes\LoadBalancerTypes;
use LKDev\HetznerCloud\Models\Networks\Networks;
use LKDev\HetznerCloud\Models\PlacementGroups\PlacementGroups;
use LKDev\HetznerCloud\Models\Prices\Prices;
use LKDev\HetznerCloud\Models\PrimaryIps\PrimaryIps;
use LKDev\HetznerCloud\Models\Servers\Servers;
use LKDev\HetznerCloud\Models\Servers\Types\ServerTypes;
use LKDev\HetznerCloud\Models\SSHKeys\SSHKeys;
use LKDev\HetznerCloud\Models\Volumes\Volumes;
use LKDev\HetznerCloud\Models\Zones\Zones;
use Psr\Http\Message\ResponseInterface;
/**
* Class HetznerAPIClient.
*/
class HetznerAPIClient
{
/**
* Version of the API Client.
*/
const VERSION = '2.8.2';
const MAX_ENTITIES_PER_PAGE = 50;
/**
* @var string
*/
protected $apiToken;
/**
* @var string
*/
protected $baseUrl;
/**
* @var string
*/
protected $userAgent;
/**
* The default instance of the HTTP client, for easily getting it in the child models.
*
* @var HetznerAPIClient
*/
public static $instance;
/**
* @var \LKDev\HetznerCloud\Clients\GuzzleClient
*/
protected GuzzleClient $httpClient;
/**
* @param string $apiToken
* @param string $baseUrl
* @param string $userAgent
*/
public function __construct(string $apiToken, $baseUrl = 'https://api.hetzner.cloud/v1/', $userAgent = '')
{
$this->apiToken = $apiToken;
$this->baseUrl = $baseUrl;
$this->userAgent = $userAgent;
$this->httpClient = new GuzzleClient($this);
self::$instance = $this;
}
/**
* @return string
*/
public function getUserAgent(): string
{
return $this->userAgent;
}
/**
* @return string
*/
public function getApiToken(): string
{
return $this->apiToken;
}
/**
* @return string
*/
public function getBaseUrl(): string
{
return $this->baseUrl;
}
/**
* @param string $userAgent
* @return HetznerAPIClient
*/
public function setUserAgent(string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}
/**
* @param string $baseUrl
* @return HetznerAPIClient
*/
public function setBaseUrl(string $baseUrl): self
{
$this->baseUrl = $baseUrl;
return $this;
}
/**
* @return GuzzleClient
*/
public function getHttpClient(): GuzzleClient
{
return $this->httpClient;
}
/**
* @return GuzzleClient
*/
public function setHttpClient(GuzzleClient $client): self
{
$this->httpClient = $client;
return $this;
}
/**
* @param \Psr\Http\Message\ResponseInterface $response
*
* @throws \LKDev\HetznerCloud\APIException
*/
public static function throwError(ResponseInterface $response)
{
$body = (string) $response->getBody();
if (strlen($body) > 0) {
$error = \GuzzleHttp\json_decode($body);
throw new APIException(APIResponse::create([
'error' => $error->error,
]), $error->error->message);
}
throw new APIException(APIResponse::create([
'response' => $response,
]), 'The response is not parseable');
}
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @return bool
*
* @throws \LKDev\HetznerCloud\APIException
*/
public static function hasError(ResponseInterface $response)
{
$responseDecoded = json_decode((string) $response->getBody());
if (strlen((string) $response->getBody()) > 0) {
if (property_exists($responseDecoded, 'error')) {
self::throwError($response);
return true;
}
} elseif ($response->getStatusCode() <= 200 && $response->getStatusCode() >= 300) {
self::throwError($response);
return true;
}
return false;
}
/**
* @return Actions
*/
public function actions()
{
return new Actions($this->httpClient);
}
/**
* @return Servers
*/
public function servers()
{
return new Servers($this->httpClient);
}
/**
* @return Volumes
*/
public function volumes()
{
return new Volumes($this->httpClient);
}
/**
* @return ServerTypes
*/
public function serverTypes()
{
return new ServerTypes($this->httpClient);
}
/**
* @return Datacenters
*/
public function datacenters()
{
return new Datacenters($this->httpClient);
}
/**
* @return Models\Locations\Locations
*/
public function locations()
{
return new Models\Locations\Locations($this->httpClient);
}
/**
* @return Images
*/
public function images()
{
return new Images($this->httpClient);
}
/**
* @return SSHKeys
*/
public function sshKeys()
{
return new SSHKeys($this->httpClient);
}
/**
* @return Prices
*/
public function prices()
{
return new Prices($this->httpClient);
}
/**
* @return ISOs
*/
public function isos()
{
return new ISOs($this->httpClient);
}
/**
* @return FloatingIps
*/
public function floatingIps()
{
return new FloatingIps($this->httpClient);
}
/**
* @return PrimaryIps
*/
public function primaryIps()
{
return new PrimaryIps($this->httpClient);
}
/**
* @return Networks
*/
public function networks()
{
return new Networks($this->httpClient);
}
/**
* @return PlacementGroups
*/
public function placementGroups()
{
return new PlacementGroups($this->httpClient);
}
/**
* @return Certificates
*/
public function certificates()
{
return new Certificates($this->httpClient);
}
/**
* @return Firewalls
*/
public function firewalls()
{
return new Firewalls($this->httpClient);
}
/**
* @return LoadBalancers
*/
public function loadBalancers()
{
return new LoadBalancers($this->httpClient);
}
/**
* @return LoadBalancerTypes
*/
public function loadBalancerTypes()
{
return new LoadBalancerTypes($this->httpClient);
}
public function zones()
{
return new Zones($this->httpClient);
}
/**
* @return GuzzleClient
*/
public function httpClient()
{
return $this->httpClient;
}
}