-
Notifications
You must be signed in to change notification settings - Fork 635
/
Api.php
137 lines (122 loc) · 3.62 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\helpers\Api as ApiHelper;
use craft\helpers\ArrayHelper;
use craft\helpers\Json;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use yii\base\Component;
/**
* The API service provides APIs for calling the Craft API (api.craftcms.com).
*
* An instance of the service is available via [[\craft\base\ApplicationTrait::getApi()|`Craft::$app->api`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
* @internal
*/
class Api extends Component
{
/**
* @var Client
*/
public Client $client;
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if (!isset($this->client)) {
$this->client = Craft::createGuzzleClient([
'base_uri' => Craft::$app->baseApiUrl,
]);
}
}
/**
* Returns info about the current Craft license.
*
* @param string[] $include
* @return array
* @throws RequestException if the API gave a non-2xx response
*/
public function getLicenseInfo(array $include = []): array
{
$response = $this->request('GET', 'cms-licenses', [
'query' => ['include' => implode(',', $include)],
]);
$body = Json::decode((string)$response->getBody());
return $body['license'];
}
/**
* Checks for Craft and plugin updates.
*
* @param string[] $maxVersions The maximum versions that should be allowed
* @return array
*/
public function getUpdates(array $maxVersions = []): array
{
$options = [];
if ($maxVersions) {
$maxVersionsStr = [];
foreach ($maxVersions as $name => $version) {
$maxVersionsStr[] = "$name:$version";
}
$options[RequestOptions::QUERY] = [
'maxVersions' => implode(',', $maxVersionsStr),
];
}
$response = $this->request('GET', 'updates', $options);
return Json::decode((string)$response->getBody());
}
/**
* Returns all country data.
*
* @return array
* @throws RequestException if the API gave a non-2xx response
*/
public function getCountries(): array
{
$cacheKey = 'countries';
$cache = Craft::$app->getCache();
if ($cache->exists($cacheKey)) {
return $cache->get($cacheKey);
}
$response = $this->request('GET', 'countries');
$countries = Json::decode((string)$response->getBody())['countries'];
$cache->set($cacheKey, $countries, 60 * 60 * 24 * 7);
return $countries;
}
/**
* @param string $method
* @param string $uri
* @param array $options
* @return ResponseInterface
* @throws RequestException
*/
public function request(string $method, string $uri, array $options = []): ResponseInterface
{
$options = ArrayHelper::merge($options, [
'headers' => ApiHelper::headers(),
]);
try {
$response = $this->client->request($method, $uri, $options);
} catch (RequestException $e) {
$response = $e->getResponse();
throw $e;
} finally {
if (isset($response) && $response->getStatusCode() !== 500) {
ApiHelper::processResponseHeaders($response->getHeaders());
}
}
return $response;
}
}