-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
74 lines (63 loc) · 1.54 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
<?php declare(strict_types=1);
/**
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* MIT License
*
* Written by Matt Saladna <[email protected]>, July 2018
*/
namespace Opcenter\Dns\Providers\Linode;
use GuzzleHttp\Psr7\Response;
class Api
{
protected const ENDPOINT = 'https://api.linode.com/v4/';
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* @var string
*/
protected $key;
/**
* @var Response
*/
protected $lastResponse;
/**
* Api constructor.
*
* @param string $key API key
*/
public function __construct(string $key)
{
$this->key = $key;
$this->client = new \GuzzleHttp\Client([
'base_uri' => static::ENDPOINT,
]);
}
public function do(string $method, string $endpoint, array $params = null): array
{
$method = strtoupper($method);
if (!\in_array($method, ['GET', 'POST', 'PUT', 'DELETE'])) {
error("Unknown method `%s'", $method);
return [];
}
if ($endpoint[0] === '/') {
warn("Stripping `/' from endpoint `%s'", $endpoint);
$endpoint = ltrim($endpoint, '/');
}
$this->lastResponse = $this->client->request($method, $endpoint, [
'headers' => [
'User-Agent' => PANEL_BRAND . ' ' . APNSCP_VERSION,
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->key
],
'json' => $params
]);
return \json_decode($this->lastResponse->getBody()->getContents(), true) ?? [];
}
public function getResponse(): Response
{
return $this->lastResponse;
}
}