PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.6
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
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
Base.php
183 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3 use \NitroPack\HttpClient\HttpClient;
4 use \NitroPack\SDK\NitroPack;
5 use \NitroPack\SDK\HealthStatus;
6 use \NitroPack\SDK\ServiceDownException;
7
8 class Base {
9 protected $baseUrl = 'https://api.getnitropack.com/';
10 protected $siteId;
11 protected $isBacklogEnabled;
12 protected $backlog;
13 protected $nitropack;
14
15 public function __construct($siteId) {
16 $this->siteId = $siteId;
17 $this->isBacklogEnabled = false;
18 $this->backlog = NULL;
19 $this->nitropack = NULL;
20
21 if (defined('NITROPACK_API_BASE_URL')) {
22 $this->baseUrl = NITROPACK_API_BASE_URL;
23 }
24 }
25
26 public function setBacklog($backlog) {
27 $this->backlog = $backlog;
28 }
29
30 public function setNitroPack($nitropack) {
31 $this->nitropack = $nitropack;
32 }
33
34 protected function addToBacklog($entry) {
35 $this->backlog->append($entry);
36 }
37
38 protected function makeRequest($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $async = false, $verifySSL = false) {
39 $backlogEntry = array(
40 "path" => $path,
41 "headers" => $headers,
42 "cookies" => $cookies,
43 "type" => $type,
44 "bodyData" => $bodyData,
45 "async" => $async,
46 "verifySSL" => $verifySSL
47 );
48
49 if ($this->nitropack && $this->nitropack->getHealthStatus() !== HealthStatus::HEALTHY) {
50 $unhealthyMsg = "Connection to NitroPack is not reliable at the moment. Please try again in a few minutes.";
51 if ($this->isBacklogEnabled) {
52 $this->addToBacklog($backlogEntry);
53 $unhealthyMsg .= " Request has been added to the backlog for delayed processing.";
54 }
55 throw new ServiceDownException($unhealthyMsg);
56 }
57
58 $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
59 $http->connect_timeout = 3; // in seconds
60 $http->ssl_timeout = 3; // in seconds
61 $http->timeout = 30; // in seconds
62
63 foreach ($headers as $name => $value) {
64 $http->setHeader($name, $value);
65 }
66
67 foreach ($cookies as $name => $value) {
68 $http->setCookie($name, $value);
69 }
70
71 if (in_array($type, array('POST', 'PUT'))) {
72 $http->setPostData($bodyData);
73 }
74
75 $http->setVerifySSL($verifySSL);
76
77 if ($this->isBacklogEnabled) {
78 $http->backlogEntry = $backlogEntry;
79 }
80
81 if ($async) {
82 $http->fetch(true, $type, $async);
83 } else {
84 $retries = 1;
85 $isRequestProcessed = false;
86 while ($retries--) {
87 try {
88 $http->fetch(true, $type, $async);
89 if ($http->getStatusCode() < 500) {
90 $isRequestProcessed = true;
91 break;
92 }
93 } catch (\Exception $e) {
94 if ($retries == 0) {
95 if (!$isRequestProcessed) {
96 if ($this->nitropack) {
97 if ($this->isBacklogEnabled) {
98 $this->nitropack->setHealthStatus(HealthStatus::SICK);
99 } else {
100 $now = microtime(true);
101 $issuesFirstAppearedAt = $this->nitropack->getTimeMark("service-status");
102 if ($issuesFirstAppearedAt === NULL) {
103 $this->nitropack->setTimeMark("service-status", $now);
104 } else {
105 if ($now - $issuesFirstAppearedAt > 60) {
106 $this->nitropack->setHealthStatus(HealthStatus::SICK);
107 }
108 }
109 }
110 }
111
112 if ($this->isBacklogEnabled) {
113 $this->addToBacklog($backlogEntry);
114 }
115 }
116 throw $e;
117 }
118 }
119
120 if ($retries > 0) {
121 usleep(500000);
122 }
123 }
124
125 if ($isRequestProcessed) {
126 $this->nitropack && $this->nitropack->unsetTimeMark("service-status");
127 } else { // In case all response codes were 500+
128 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER);
129 if ($this->isBacklogEnabled) {
130 $this->addToBacklog($backlogEntry);
131 }
132 }
133 }
134
135 return $http;
136 }
137
138 protected function makeRequestAsync($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $verifySSL = false) {
139 return $this->makeRequest($path, $headers, $cookies, $type, $bodyData, true, $verifySSL);
140 }
141
142 protected function throwException($httpResponse, $template) {
143 try {
144 $err = json_decode($httpResponse->getBody(), true);
145 $errorMessage = isset($err['error']) ? $err['error'] : 'Unknown';
146 } catch (\Exception $e) {
147 $errorMessage = 'Unknown';
148 }
149
150 $isServiceUnavailable = false;
151
152 if ($errorMessage == 'Unknown') { // Fallback to known HTTP errors
153 $statusCode = $httpResponse->getStatusCode();
154 switch ($statusCode) {
155 case ResponseStatus::BAD_REQUEST:
156 $errorMessage = "Bad Request";
157 break;
158 case ResponseStatus::FORBIDDEN:
159 $errorMessage = "Forbidden";
160 break;
161 case ResponseStatus::NOT_FOUND:
162 $errorMessage = "Not Found";
163 break;
164 case ResponseStatus::RUNTIME_ERROR:
165 $errorMessage = "Runtime Error";
166 break;
167 case ResponseStatus::SERVICE_UNAVAILABLE:
168 $isServiceUnavailable = true;
169 $errorMessage = "Service Unavailable";
170 break;
171 default:
172 $errorMessage = 'Unknown';
173 break;
174 }
175 }
176
177 if ($isServiceUnavailable) {
178 throw new ServiceDownException(sprintf($template, $errorMessage));
179 } else {
180 throw new \RuntimeException(sprintf($template, $errorMessage));
181 }
182 }
183 }