PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.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 / Base.php
nitropack / nitropack-sdk / NitroPack / SDK / Api Last commit date
Base.php 5 years ago Cache.php 5 years ago Integration.php 6 years ago RemoteConfigFetcher.php 6 years ago RequestMaker.php 5 years ago Response.php 6 years ago ResponseStatus.php 6 years ago SecureRequestMaker.php 5 years ago SignedBase.php 5 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
167 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3 use \NitroPack\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.";
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 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::SICK);
97 if ($this->isBacklogEnabled) {
98 $this->addToBacklog($backlogEntry);
99 }
100 }
101 throw $e;
102 }
103 }
104
105 if ($retries > 0) {
106 usleep(500000);
107 }
108 }
109
110 if (!$isRequestProcessed) { // In case all response codes were 500+
111 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER);
112 if ($this->isBacklogEnabled) {
113 $this->addToBacklog($backlogEntry);
114 }
115 }
116 }
117
118 return $http;
119 }
120
121 protected function makeRequestAsync($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $verifySSL = false) {
122 return $this->makeRequest($path, $headers, $cookies, $type, $bodyData, true, $verifySSL);
123 }
124
125 protected function throwException($httpResponse, $template) {
126 try {
127 $err = json_decode($httpResponse->getBody(), true);
128 $errorMessage = isset($err['error']) ? $err['error'] : 'Unknown';
129 } catch (\Exception $e) {
130 $errorMessage = 'Unknown';
131 }
132
133 $isServiceUnavailable = false;
134
135 if ($errorMessage == 'Unknown') { // Fallback to known HTTP errors
136 $statusCode = $httpResponse->getStatusCode();
137 switch ($statusCode) {
138 case ResponseStatus::BAD_REQUEST:
139 $errorMessage = "Bad Request";
140 break;
141 case ResponseStatus::FORBIDDEN:
142 $errorMessage = "Forbidden";
143 break;
144 case ResponseStatus::NOT_FOUND:
145 $errorMessage = "Not Found";
146 break;
147 case ResponseStatus::RUNTIME_ERROR:
148 $errorMessage = "Runtime Error";
149 break;
150 case ResponseStatus::SERVICE_UNAVAILABLE:
151 $isServiceUnavailable = true;
152 $errorMessage = "Service Unavailable";
153 break;
154 default:
155 $errorMessage = 'Unknown';
156 break;
157 }
158 }
159
160 if ($isServiceUnavailable) {
161 throw new ServiceDownException(sprintf($template, $errorMessage));
162 } else {
163 throw new \RuntimeException(sprintf($template, $errorMessage));
164 }
165 }
166 }
167