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
Integration.php
282 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK\Api; |
| 3 | |
| 4 | use \NitroPack\SDK\IntegrationUrl; |
| 5 | use \NitroPack\SDK\Website; |
| 6 | use \NitroPack\SDK\Crypto; |
| 7 | |
| 8 | class Integration extends Base { |
| 9 | private $siteSecret; |
| 10 | private $keys; |
| 11 | |
| 12 | public function __construct($siteId, $siteSecret) { |
| 13 | parent::__construct($siteId); |
| 14 | |
| 15 | $this->baseUrl = IntegrationUrl::getBaseUrl(); |
| 16 | $this->siteSecret = $siteSecret; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Generates a private/public key pair. This is a slow operation! Run it no more than once only whenever needed! |
| 21 | * @return stdClass Pair of private and public keys |
| 22 | */ |
| 23 | protected function keysInstance() { |
| 24 | // This must be executed only once per request. |
| 25 | if (empty($this->keys)) { |
| 26 | $this->keys = Crypto::generateKeyPair(); |
| 27 | } |
| 28 | |
| 29 | return $this->keys; |
| 30 | } |
| 31 | |
| 32 | protected function websiteFromStruct($data, $errorTemplate, $privateKey) { |
| 33 | $site = new Website; |
| 34 | |
| 35 | $json = Crypto::decrypt($data->credentials, $privateKey); |
| 36 | |
| 37 | if ($json == "" || null === @json_decode($json)) { |
| 38 | throw new \RuntimeException(sprintf($errorTemplate, "Cannot decrypt website credentials!"), ResponseStatus::BAD_REQUEST); |
| 39 | } |
| 40 | |
| 41 | $credentials = @json_decode($json); |
| 42 | |
| 43 | $site->setName($data->name); |
| 44 | $site->setURL($data->url); |
| 45 | $site->setAPIKey($credentials->apikey); |
| 46 | $site->setAPISecret($credentials->apisecret); |
| 47 | $site->setUsedDiskSpaceBytes((int)$data->used_disk_space_bytes); |
| 48 | $site->setUsedOptimizations((int)$data->used_optimizations); |
| 49 | $site->setLastQuotaResetTimestamp((int)$data->last_quota_reset_timestamp); |
| 50 | $site->setStatus(!!$data->status); |
| 51 | $site->setCreatedTimestamp((int)$data->created_timestamp); |
| 52 | $site->setModifiedTimestamp((int)$data->created_timestamp); |
| 53 | |
| 54 | return $site; |
| 55 | } |
| 56 | |
| 57 | public function create(Website $website) { |
| 58 | // Prepare keys |
| 59 | $keys = $this->keysInstance(); |
| 60 | |
| 61 | // Prepare the request URL |
| 62 | $url = new IntegrationUrl("website_create", $this->siteId, $this->siteSecret); |
| 63 | |
| 64 | // Error template |
| 65 | $errorTemplate = "Error while creating website: %s"; |
| 66 | |
| 67 | // Request headers |
| 68 | $headers = array(); |
| 69 | |
| 70 | $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey); |
| 71 | |
| 72 | // Request data |
| 73 | $data = array(); |
| 74 | |
| 75 | $data['website_url'] = $website->getURL(); |
| 76 | $data['website_name'] = $website->getName(); |
| 77 | |
| 78 | // Do the request |
| 79 | $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "POST", $data, false, true); |
| 80 | |
| 81 | // Read the response body |
| 82 | if (null === $responseBody = @json_decode($httpResponse->getBody())) { |
| 83 | $errorMessage = "No response body!"; |
| 84 | |
| 85 | throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR); |
| 86 | } |
| 87 | |
| 88 | // React according to the status code |
| 89 | $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 90 | |
| 91 | switch ($statusCode) { |
| 92 | case ResponseStatus::OK: |
| 93 | // All is well, return a result |
| 94 | return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey); |
| 95 | default: |
| 96 | // An error has occurred, throw an exception with the status code |
| 97 | throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public function update(Website $website) { |
| 102 | // Prepare keys |
| 103 | $keys = $this->keysInstance(); |
| 104 | |
| 105 | // Provide target site_id |
| 106 | $additional_params = array( |
| 107 | 'target_site_id' => $website->getAPIKey() |
| 108 | ); |
| 109 | |
| 110 | // Prepare the request URL |
| 111 | $url = new IntegrationUrl("website_update", $this->siteId, $this->siteSecret, null, $additional_params); |
| 112 | |
| 113 | // Error template |
| 114 | $errorTemplate = "Error while updating website: %s"; |
| 115 | |
| 116 | // Request headers |
| 117 | $headers = array(); |
| 118 | |
| 119 | $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey); |
| 120 | |
| 121 | // Request data |
| 122 | $data = array(); |
| 123 | |
| 124 | $data['website_url'] = $website->getURL(); |
| 125 | $data['website_name'] = $website->getName(); |
| 126 | |
| 127 | // Do the request |
| 128 | $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "POST", $data, false, true); |
| 129 | |
| 130 | // Read the response body |
| 131 | if (null === $responseBody = @json_decode($httpResponse->getBody())) { |
| 132 | $errorMessage = "No response body!"; |
| 133 | |
| 134 | throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR); |
| 135 | } |
| 136 | |
| 137 | // React according to the status code |
| 138 | $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 139 | |
| 140 | switch ($statusCode) { |
| 141 | case ResponseStatus::OK: |
| 142 | // All is well, return a result |
| 143 | return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey); |
| 144 | default: |
| 145 | // An error has occurred, throw an exception with the status code |
| 146 | throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public function remove($apikey) { |
| 151 | // Provide target site_id |
| 152 | $additional_params = array( |
| 153 | 'target_site_id' => $apikey |
| 154 | ); |
| 155 | |
| 156 | // Prepare the request URL |
| 157 | $url = new IntegrationUrl("website_remove", $this->siteId, $this->siteSecret, null, $additional_params); |
| 158 | |
| 159 | // Error template |
| 160 | $errorTemplate = "Error while removing website: %s"; |
| 161 | |
| 162 | // Do the request |
| 163 | $httpResponse = $this->makeRequest($url->getPath(), array(), array(), "DELETE", array(), false, true); |
| 164 | |
| 165 | // Read the response body |
| 166 | if (null === $responseBody = @json_decode($httpResponse->getBody())) { |
| 167 | $errorMessage = "No response body!"; |
| 168 | |
| 169 | throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR); |
| 170 | } |
| 171 | |
| 172 | // React according to the status code |
| 173 | $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 174 | |
| 175 | switch ($statusCode) { |
| 176 | case ResponseStatus::OK: |
| 177 | // All is well, return a result |
| 178 | return true; |
| 179 | default: |
| 180 | // An error has occurred, throw an exception with the status code |
| 181 | throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public function readByAPIKey($apikey) { |
| 186 | // Prepare keys |
| 187 | $keys = $this->keysInstance(); |
| 188 | |
| 189 | // Provide target site_id |
| 190 | $additional_params = array( |
| 191 | 'target_site_id' => $apikey |
| 192 | ); |
| 193 | |
| 194 | // Prepare the request URL |
| 195 | $url = new IntegrationUrl("website_read", $this->siteId, $this->siteSecret, null, $additional_params); |
| 196 | |
| 197 | // Error template |
| 198 | $errorTemplate = "Error while reading website: %s"; |
| 199 | |
| 200 | // Request headers |
| 201 | $headers = array(); |
| 202 | |
| 203 | $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey); |
| 204 | |
| 205 | // Do the request |
| 206 | $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "GET", array(), false, true); |
| 207 | |
| 208 | // Read the response body |
| 209 | if (null === $responseBody = @json_decode($httpResponse->getBody())) { |
| 210 | $errorMessage = "No response body!"; |
| 211 | |
| 212 | throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR); |
| 213 | } |
| 214 | |
| 215 | // React according to the status code |
| 216 | $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 217 | |
| 218 | switch ($statusCode) { |
| 219 | case ResponseStatus::OK: |
| 220 | // All is well, return a result |
| 221 | return $this->websiteFromStruct($responseBody->data->site, $errorTemplate, $keys->privateKey); |
| 222 | default: |
| 223 | // An error has occurred, throw an exception with the status code |
| 224 | throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | public function readPaginated($page, $limit = 250) { |
| 229 | // Prepare keys |
| 230 | $keys = $this->keysInstance(); |
| 231 | |
| 232 | // Provide target site_id |
| 233 | $additional_params = array( |
| 234 | 'page' => $page, |
| 235 | 'limit' => $limit |
| 236 | ); |
| 237 | |
| 238 | // Prepare the request URL |
| 239 | $url = new IntegrationUrl("website_read", $this->siteId, $this->siteSecret, null, $additional_params); |
| 240 | |
| 241 | // Error template |
| 242 | $errorTemplate = "Error while reading website: %s"; |
| 243 | |
| 244 | // Request headers |
| 245 | $headers = array(); |
| 246 | |
| 247 | $headers['X-Nitro-Public-Key'] = base64_encode($keys->publicKey); |
| 248 | |
| 249 | // Do the request |
| 250 | $httpResponse = $this->makeRequest($url->getPath(), $headers, array(), "GET", array(), false, true); |
| 251 | |
| 252 | // Read the response body |
| 253 | if (null === $responseBody = @json_decode($httpResponse->getBody())) { |
| 254 | $errorMessage = "No response body!"; |
| 255 | |
| 256 | throw new \RuntimeException(sprintf($errorTemplate, $errorMessage), ResponseStatus::RUNTIME_ERROR); |
| 257 | } |
| 258 | |
| 259 | // React according to the status code |
| 260 | $statusCode = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 261 | |
| 262 | switch ($statusCode) { |
| 263 | case ResponseStatus::OK: |
| 264 | // All is well, return a result |
| 265 | $result = new \stdClass; |
| 266 | |
| 267 | $result->websites = array(); |
| 268 | |
| 269 | foreach ($responseBody->data->sites as $site) { |
| 270 | $result->websites[] = $this->websiteFromStruct($site, $errorTemplate, $keys->privateKey); |
| 271 | } |
| 272 | |
| 273 | $result->pagination = $responseBody->data->pagination; |
| 274 | |
| 275 | return $result; |
| 276 | default: |
| 277 | // An error has occurred, throw an exception with the status code |
| 278 | throw new \RuntimeException(sprintf($errorTemplate, $responseBody->message), $statusCode); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 |