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 |