AdditionalDomains.php
1 year ago
Base.php
8 months ago
Cache.php
1 year ago
ExcludedUrls.php
1 year ago
Excludes.php
1 year ago
Integration.php
1 year ago
RemoteConfigFetcher.php
1 year ago
RequestMaker.php
1 year ago
Response.php
1 year ago
ResponseStatus.php
1 year ago
SafeMode.php
1 year ago
SecureRequestMaker.php
1 year ago
SignedBase.php
1 year ago
Stats.php
1 year ago
Tagger.php
1 year ago
Url.php
1 year ago
VariationCookie.php
1 year ago
Varnish.php
1 year ago
Warmup.php
1 year ago
Webhook.php
1 year ago
Base.php
189 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 | // If CF-Access-Client-Id and CF-Access-Client-Secret are defined in wordpress, add them to headers |
| 40 | if (defined('NITROPACK_CF_ACCESS_CLIENT_ID') && defined('NITROPACK_CF_ACCESS_CLIENT_SECRET')) { |
| 41 | $headers['CF-Access-Client-Id'] = NITROPACK_CF_ACCESS_CLIENT_ID; |
| 42 | $headers['CF-Access-Client-Secret'] = NITROPACK_CF_ACCESS_CLIENT_SECRET; |
| 43 | } |
| 44 | |
| 45 | $backlogEntry = array( |
| 46 | "path" => $path, |
| 47 | "headers" => $headers, |
| 48 | "cookies" => $cookies, |
| 49 | "type" => $type, |
| 50 | "bodyData" => $bodyData, |
| 51 | "async" => $async, |
| 52 | "verifySSL" => $verifySSL |
| 53 | ); |
| 54 | |
| 55 | if ($this->nitropack && $this->nitropack->getHealthStatus() !== HealthStatus::HEALTHY) { |
| 56 | $unhealthyMsg = "Connection to NitroPack is not reliable at the moment. Please try again in a few minutes."; |
| 57 | if ($this->isBacklogEnabled) { |
| 58 | $this->addToBacklog($backlogEntry); |
| 59 | $unhealthyMsg .= " Request has been added to the backlog for delayed processing."; |
| 60 | } |
| 61 | throw new ServiceDownException($unhealthyMsg); |
| 62 | } |
| 63 | |
| 64 | $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 |
| 65 | $http->connect_timeout = 3; // in seconds |
| 66 | $http->ssl_timeout = 3; // in seconds |
| 67 | $http->timeout = 30; // in seconds |
| 68 | |
| 69 | foreach ($headers as $name => $value) { |
| 70 | $http->setHeader($name, $value); |
| 71 | } |
| 72 | |
| 73 | foreach ($cookies as $name => $value) { |
| 74 | $http->setCookie($name, $value); |
| 75 | } |
| 76 | |
| 77 | if (in_array($type, array('POST', 'PUT'))) { |
| 78 | $http->setPostData($bodyData); |
| 79 | } |
| 80 | |
| 81 | $http->setVerifySSL($verifySSL); |
| 82 | |
| 83 | if ($this->isBacklogEnabled) { |
| 84 | $http->backlogEntry = $backlogEntry; |
| 85 | } |
| 86 | |
| 87 | if ($async) { |
| 88 | $http->fetch(true, $type, $async); |
| 89 | } else { |
| 90 | $retries = 1; |
| 91 | $isRequestProcessed = false; |
| 92 | while ($retries--) { |
| 93 | try { |
| 94 | $http->fetch(true, $type, $async); |
| 95 | if ($http->getStatusCode() < 500) { |
| 96 | $isRequestProcessed = true; |
| 97 | break; |
| 98 | } |
| 99 | } catch (\Exception $e) { |
| 100 | if ($retries == 0) { |
| 101 | if (!$isRequestProcessed) { |
| 102 | if ($this->nitropack) { |
| 103 | if ($this->isBacklogEnabled) { |
| 104 | $this->nitropack->setHealthStatus(HealthStatus::SICK); |
| 105 | } else { |
| 106 | $now = microtime(true); |
| 107 | $issuesFirstAppearedAt = $this->nitropack->getTimeMark("service-status"); |
| 108 | if ($issuesFirstAppearedAt === NULL) { |
| 109 | $this->nitropack->setTimeMark("service-status", $now); |
| 110 | } else { |
| 111 | if ($now - $issuesFirstAppearedAt > 60) { |
| 112 | $this->nitropack->setHealthStatus(HealthStatus::SICK); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ($this->isBacklogEnabled) { |
| 119 | $this->addToBacklog($backlogEntry); |
| 120 | } |
| 121 | } |
| 122 | throw $e; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($retries > 0) { |
| 127 | usleep(500000); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if ($isRequestProcessed) { |
| 132 | $this->nitropack && $this->nitropack->unsetTimeMark("service-status"); |
| 133 | } else { // In case all response codes were 500+ |
| 134 | $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER); |
| 135 | if ($this->isBacklogEnabled) { |
| 136 | $this->addToBacklog($backlogEntry); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return $http; |
| 142 | } |
| 143 | |
| 144 | protected function makeRequestAsync($path, $headers = array(), $cookies = array(), $type = 'GET', $bodyData=array(), $verifySSL = false) { |
| 145 | return $this->makeRequest($path, $headers, $cookies, $type, $bodyData, true, $verifySSL); |
| 146 | } |
| 147 | |
| 148 | protected function throwException($httpResponse, $template) { |
| 149 | try { |
| 150 | $err = json_decode($httpResponse->getBody(), true); |
| 151 | $errorMessage = isset($err['error']) ? $err['error'] : 'Unknown'; |
| 152 | } catch (\Exception $e) { |
| 153 | $errorMessage = 'Unknown'; |
| 154 | } |
| 155 | |
| 156 | $isServiceUnavailable = false; |
| 157 | |
| 158 | if ($errorMessage == 'Unknown') { // Fallback to known HTTP errors |
| 159 | $statusCode = $httpResponse->getStatusCode(); |
| 160 | switch ($statusCode) { |
| 161 | case ResponseStatus::BAD_REQUEST: |
| 162 | $errorMessage = "Bad Request"; |
| 163 | break; |
| 164 | case ResponseStatus::FORBIDDEN: |
| 165 | $errorMessage = "Forbidden"; |
| 166 | break; |
| 167 | case ResponseStatus::NOT_FOUND: |
| 168 | $errorMessage = "Not Found"; |
| 169 | break; |
| 170 | case ResponseStatus::RUNTIME_ERROR: |
| 171 | $errorMessage = "Runtime Error"; |
| 172 | break; |
| 173 | case ResponseStatus::SERVICE_UNAVAILABLE: |
| 174 | $isServiceUnavailable = true; |
| 175 | $errorMessage = "Service Unavailable"; |
| 176 | break; |
| 177 | default: |
| 178 | $errorMessage = 'Unknown'; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if ($isServiceUnavailable) { |
| 184 | throw new ServiceDownException(sprintf($template, $errorMessage)); |
| 185 | } else { |
| 186 | throw new \RuntimeException(sprintf($template, $errorMessage)); |
| 187 | } |
| 188 | } |
| 189 | } |