PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.0
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / nitropack-sdk / NitroPack / SDK / Api / Integration.php
nitropack / nitropack-sdk / NitroPack / SDK / Api Last commit date
AdditionalDomains.php 3 years ago Base.php 3 years ago Cache.php 2 years ago ExcludedUrls.php 3 years ago Excludes.php 2 years ago Integration.php 2 years ago RemoteConfigFetcher.php 4 years ago RequestMaker.php 5 years ago Response.php 6 years ago ResponseStatus.php 6 years ago SafeMode.php 3 years ago SecureRequestMaker.php 5 years ago SignedBase.php 3 years ago Stats.php 6 years ago Tagger.php 3 years ago Url.php 6 years ago VariationCookie.php 6 years ago Varnish.php 2 years ago Warmup.php 6 years ago Webhook.php 6 years ago
Integration.php
282 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3
4 use \NitroPack\SDK\IntegrationUrl;
5 use \NitroPack\SDK\Website;
6 use \NitroPack\SDK\Crypto;
7
8 class Integration extends Base {
9 private $siteSecret;
10 private $keys;
11
12 public function __construct($siteId, $siteSecret) {
13 parent::__construct($siteId);
14
15 $this->baseUrl = IntegrationUrl::getBaseUrl();
16 $this->siteSecret = $siteSecret;
17 }
18
19 /**
20 * Generates a private/public key pair. This is a slow operation! Run it no more than once only whenever needed!
21 * @return stdClass Pair of private and public keys
22 */
23 protected function keysInstance() {
24 // This must be executed only once per request.
25 if (empty($this->keys)) {
26 $this->keys = Crypto::generateKeyPair();
27 }
28
29 return $this->keys;
30 }
31
32 protected function websiteFromStruct($data, $errorTemplate, $privateKey) {
33 $site = new Website;
34
35 $json = Crypto::decrypt($data->credentials, $privateKey);
36
37 if ($json == "" || null === @json_decode($json)) {
38 throw new \RuntimeException(sprintf($errorTemplate, "Cannot decrypt website credentials!"), ResponseStatus::BAD_REQUEST);
39 }
40
41 $credentials = @json_decode($json);
42
43 $site->setName($data->name);
44 $site->setURL($data->url);
45 $site->setAPIKey($credentials->apikey);
46 $site->setAPISecret($credentials->apisecret);
47 $site->setUsedDiskSpaceBytes((int)$data->used_disk_space_bytes);
48 $site->setUsedOptimizations((int)$data->used_optimizations);
49 $site->setLastQuotaResetTimestamp((int)$data->last_quota_reset_timestamp);
50 $site->setStatus(!!$data->status);
51 $site->setCreatedTimestamp((int)$data->created_timestamp);
52 $site->setModifiedTimestamp((int)$data->created_timestamp);
53
54 return $site;
55 }
56
57 public function create(Website $website) {
58 // Prepare keys
59 $keys = $this->keysInstance();
60
61 // Prepare the request URL
62 $url = new IntegrationUrl("website_create", $this->siteId, $this->siteSecret);
63
64 // Error template
65 $errorTemplate = "Error while creating website: %s";
66
67 // Request headers
68 $headers = array();
69
70 $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey);
71
72 // Request data
73 $data = array();
74
75 $data['website_url'] = $website->getURL();
76 $data['website_name'] = $website->getName();
77
78 // Do the request
79 $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "POST", $data, false, true);
80
81 // Read the response body
82 if (null === $responseBody = @json_decode($httpResponse->getBody())) {
83 $errorMessage = "No response body!";
84
85 throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR);
86 }
87
88 // React according to the status code
89 $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode());
90
91 switch ($statusCode) {
92 case ResponseStatus::OK:
93 // All is well, return a result
94 return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey);
95 default:
96 // An error has occurred, throw an exception with the status code
97 throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode);
98 }
99 }
100
101 public function update(Website $website) {
102 // Prepare keys
103 $keys = $this->keysInstance();
104
105 // Provide target site_id
106 $additional_params = array(
107 'target_site_id' => $website->getAPIKey()
108 );
109
110 // Prepare the request URL
111 $url = new IntegrationUrl("website_update", $this->siteId, $this->siteSecret, null, $additional_params);
112
113 // Error template
114 $errorTemplate = "Error while updating website: %s";
115
116 // Request headers
117 $headers = array();
118
119 $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey);
120
121 // Request data
122 $data = array();
123
124 $data['website_url'] = $website->getURL();
125 $data['website_name'] = $website->getName();
126
127 // Do the request
128 $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "POST", $data, false, true);
129
130 // Read the response body
131 if (null === $responseBody = @json_decode($httpResponse->getBody())) {
132 $errorMessage = "No response body!";
133
134 throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR);
135 }
136
137 // React according to the status code
138 $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode());
139
140 switch ($statusCode) {
141 case ResponseStatus::OK:
142 // All is well, return a result
143 return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey);
144 default:
145 // An error has occurred, throw an exception with the status code
146 throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode);
147 }
148 }
149
150 public function remove($apikey) {
151 // Provide target site_id
152 $additional_params = array(
153 'target_site_id' => $apikey
154 );
155
156 // Prepare the request URL
157 $url = new IntegrationUrl("website_remove", $this->siteId, $this->siteSecret, null, $additional_params);
158
159 // Error template
160 $errorTemplate = "Error while removing website: %s";
161
162 // Do the request
163 $httpResponse = $this->makeRequest($url->getPath(), array(), array(), "DELETE", array(), false, true);
164
165 // Read the response body
166 if (null === $responseBody = @json_decode($httpResponse->getBody())) {
167 $errorMessage = "No response body!";
168
169 throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR);
170 }
171
172 // React according to the status code
173 $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode());
174
175 switch ($statusCode) {
176 case ResponseStatus::OK:
177 // All is well, return a result
178 return true;
179 default:
180 // An error has occurred, throw an exception with the status code
181 throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode);
182 }
183 }
184
185 public function readByAPIKey($apikey) {
186 // Prepare keys
187 $keys = $this->keysInstance();
188
189 // Provide target site_id
190 $additional_params = array(
191 'target_site_id' => $apikey
192 );
193
194 // Prepare the request URL
195 $url = new IntegrationUrl("website_read", $this->siteId, $this->siteSecret, null, $additional_params);
196
197 // Error template
198 $errorTemplate = "Error while reading website: %s";
199
200 // Request headers
201 $headers = array();
202
203 $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey);
204
205 // Do the request
206 $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "GET", array(), false, true);
207
208 // Read the response body
209 if (null === $responseBody = @json_decode($httpResponse->getBody())) {
210 $errorMessage = "No response body!";
211
212 throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR);
213 }
214
215 // React according to the status code
216 $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode());
217
218 switch ($statusCode) {
219 case ResponseStatus::OK:
220 // All is well, return a result
221 return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey);
222 default:
223 // An error has occurred, throw an exception with the status code
224 throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode);
225 }
226 }
227
228 public function readPaginated($page, $limit = 250) {
229 // Prepare keys
230 $keys = $this->keysInstance();
231
232 // Provide target site_id
233 $additional_params = array(
234 'page' => $page,
235 'limit' => $limit
236 );
237
238 // Prepare the request URL
239 $url = new IntegrationUrl("website_read", $this->siteId, $this->siteSecret, null, $additional_params);
240
241 // Error template
242 $errorTemplate = "Error while reading website: %s";
243
244 // Request headers
245 $headers = array();
246
247 $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey);
248
249 // Do the request
250 $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "GET", array(), false, true);
251
252 // Read the response body
253 if (null === $responseBody = @json_decode($httpResponse->getBody())) {
254 $errorMessage = "No response body!";
255
256 throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR);
257 }
258
259 // React according to the status code
260 $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode());
261
262 switch ($statusCode) {
263 case ResponseStatus::OK:
264 // All is well, return a result
265 $result = new \stdClass;
266
267 $result->websites = array();
268
269 foreach ($responseBody->data->sites as $site) {
270 $result->websites[] = $this->websiteFromStruct($site, $errorTemplate, $keys->privateKey);
271 }
272
273 $result->pagination = $responseBody->data->pagination;
274
275 return $result;
276 default:
277 // An error has occurred, throw an exception with the status code
278 throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode);
279 }
280 }
281 }
282