PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.4.1
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.4.1
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 / Base.php
nitropack / nitropack-sdk / NitroPack / SDK / Api Last commit date
Base.php 6 years ago Cache.php 6 years ago Integration.php 6 years ago RemoteConfigFetcher.php 6 years ago Response.php 6 years ago ResponseStatus.php 6 years ago SignedBase.php 6 years ago Stats.php 6 years ago Tagger.php 6 years ago Url.php 6 years ago VariationCookie.php 6 years ago Warmup.php 6 years ago Webhook.php 6 years ago
Base.php
97 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3 use \NitroPack\HttpClient;
4
5 class Base {
6 protected $baseUrl = 'https://api.getnitropack.com/';
7 protected $siteId;
8
9 public function __construct($siteId) {
10 $this->siteId = $siteId;
11
12 if (defined('NITROPACK_API_BASE_URL')) {
13 $this->baseUrl = NITROPACK_API_BASE_URL;
14 }
15 }
16
17 protected function makeRequest($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $async = false, $verifySSL = false) {
18 $http = new HttpClient($this->baseUrl . $path); // HttpClient keeps a cache of the opened connections, so creating a new instance every time is not an issue
19 $http->connect_timeout = 3; // in seconds
20 $http->ssl_timeout = 3; // in seconds
21 $http->timeout = 30; // in seconds
22
23 foreach ($headers as $name => $value) {
24 $http->setHeader($name, $value);
25 }
26
27 foreach ($cookies as $name => $value) {
28 $http->setCookie($name, $value);
29 }
30
31 if (in_array($type, array('POST', 'PUT'))) {
32 $http->setPostData($bodyData);
33 }
34
35 $http->setVerifySSL($verifySSL);
36
37 if ($async) {
38 $http->fetch(true, $type, $async);
39 } else {
40 $retries = 1;
41 while ($retries--) {
42 try {
43 $http->fetch(true, $type, $async);
44 if ($http->getStatusCode() < 500) break;
45 } catch (\Exception $e) {
46 if ($retries == 0) throw $e;
47 }
48
49 if ($retries > 0) {
50 usleep(500000);
51 }
52 }
53 }
54
55 return $http;
56 }
57
58 protected function makeRequestAsync($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $verifySSL = false) {
59 return $this->makeRequest($path, $headers, $cookies, $type, $bodyData, true, $verifySSL);
60 }
61
62 protected function throwException($httpResponse, $template) {
63 try {
64 $err = json_decode($httpResponse->getBody(), true);
65 $errorMessage = isset($err['error']) ? $err['error'] : 'Unknown';
66 } catch (\Exception $e) {
67 $errorMessage = 'Unknown';
68 }
69
70 if ($errorMessage == 'Unknown') { // Fallback to known HTTP errors
71 $statusCode = $httpResponse->getStatusCode();
72 switch ($statusCode) {
73 case ResponseStatus::BAD_REQUEST:
74 $errorMessage = "Bad Request";
75 break;
76 case ResponseStatus::FORBIDDEN:
77 $errorMessage = "Forbidden";
78 break;
79 case ResponseStatus::NOT_FOUND:
80 $errorMessage = "Not Found";
81 break;
82 case ResponseStatus::RUNTIME_ERROR:
83 $errorMessage = "Runtime Error";
84 break;
85 case ResponseStatus::SERVICE_UNAVAILABLE:
86 $errorMessage = "Service Unavailable";
87 break;
88 default:
89 $errorMessage = 'Unknown';
90 break;
91 }
92 }
93
94 throw new \RuntimeException(sprintf($template, $errorMessage));
95 }
96 }
97