Api
2 years ago
Integrations
2 years ago
StorageDriver
4 years ago
Url
2 years ago
Utils
2 years ago
data
6 years ago
Api.php
2 years ago
Backlog.php
4 years ago
BacklogReplayTimeoutException.php
4 years ago
ChallengeProcessingException.php
4 years ago
ChallengeVerificationException.php
4 years ago
ConfigFetcherException.php
4 years ago
Crypto.php
3 years ago
Device.php
6 years ago
DeviceType.php
6 years ago
ElementRevision.php
3 years ago
EmptyConfigException.php
6 years ago
ExcludeEntry.php
2 years ago
ExcludeOperationType.php
2 years ago
FileHandle.php
5 years ago
Filesystem.php
4 years ago
HealthStatus.php
5 years ago
IntegrationUrl.php
2 years ago
NitroPack.php
1 year ago
NoConfigException.php
5 years ago
Pagecache.php
2 years ago
PurgeType.php
3 years ago
ServiceDownException.php
5 years ago
StorageException.php
6 years ago
VariationCookieException.php
5 years ago
WebhookException.php
5 years ago
Website.php
6 years ago
Api.php
427 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\SDK; |
| 4 | |
| 5 | use \NitroPack\SDK\Website as Website; |
| 6 | |
| 7 | class Api |
| 8 | { |
| 9 | private $allowedWebhooks = array('config', 'cache_clear', 'cache_ready', 'sitemap'); |
| 10 | private $children; |
| 11 | |
| 12 | public function __construct($siteId, $siteSecret) |
| 13 | { |
| 14 | $this->children = []; |
| 15 | $this->children["cache"] = new Api\Cache($siteId, $siteSecret); |
| 16 | $this->children["tagger"] = new Api\Tagger($siteId, $siteSecret); |
| 17 | $this->children["url"] = new Api\Url($siteId, $siteSecret); |
| 18 | $this->children["stats"] = new Api\Stats($siteId, $siteSecret); |
| 19 | $this->children["webhook"] = new Api\Webhook($siteId, $siteSecret); |
| 20 | $this->children["warmup"] = new Api\Warmup($siteId, $siteSecret); |
| 21 | $this->children["integration"] = new Api\Integration($siteId, $siteSecret); |
| 22 | $this->children["variation_cookie"] = new Api\VariationCookie($siteId, $siteSecret); |
| 23 | $this->children["safe_mode"] = new Api\SafeMode($siteId, $siteSecret); |
| 24 | $this->children["request_maker"] = new Api\RequestMaker($siteId); |
| 25 | $this->children["secure_request_maker"] = new Api\SecureRequestMaker($siteId, $siteSecret); |
| 26 | $this->children["varnish"] = new Api\Varnish($siteId, $siteSecret); |
| 27 | $this->children["excludedurls"] = new Api\ExcludedUrls($siteId, $siteSecret); |
| 28 | $this->children["additionaldomains"] = new Api\AdditionalDomains($siteId, $siteSecret); |
| 29 | $this->children["excludes"] = new Api\Excludes($siteId, $siteSecret); |
| 30 | } |
| 31 | |
| 32 | public function __get($prop) |
| 33 | { |
| 34 | return !empty($this->children[$prop]) ? $this->children[$prop] : NULL; |
| 35 | } |
| 36 | |
| 37 | public function setBacklog($backlog) |
| 38 | { |
| 39 | foreach ($this->children as $child) { |
| 40 | $child->setBacklog($backlog); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function setNitroPack($nitropack) |
| 45 | { |
| 46 | foreach ($this->children as $child) { |
| 47 | $child->setNitroPack($nitropack); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function getCache($url, $userAgent, $cookies, $isAjax, $layout, $referer) |
| 52 | { |
| 53 | return $this->cache->get($url, $userAgent, $cookies, $isAjax, $layout, NULL, $referer); |
| 54 | } |
| 55 | |
| 56 | public function getCacheMulti($urls, $userAgent, $cookies, $isAjax, $layout, $referer) |
| 57 | { |
| 58 | return $this->cache->getMulti($urls, $userAgent, $cookies, $isAjax, $layout, NULL, $referer); |
| 59 | } |
| 60 | |
| 61 | public function getLastCachePurge() |
| 62 | { |
| 63 | return $this->cache->getLastPurge(); |
| 64 | } |
| 65 | |
| 66 | public function purgeCache($url = NULL, $pagecacheOnly = false, $reason = NULL, $lightPurge = false) |
| 67 | { |
| 68 | return $this->cache->purge($url, $pagecacheOnly, $reason, $lightPurge); |
| 69 | } |
| 70 | |
| 71 | public function purgeCacheByTag($tag, $reason = NULL, $lightPurge = false) |
| 72 | { |
| 73 | return $this->cache->purgeByTag($tag, $reason, $lightPurge); |
| 74 | } |
| 75 | |
| 76 | public function getUrls($page = 1, $limit = 250, $search = NULL, $deviceType = NULL, $status = NULL) |
| 77 | { |
| 78 | return $this->url->get($page, $limit, $search, $deviceType, $status); |
| 79 | } |
| 80 | |
| 81 | public function getUrlsCount($search = NULL, $deviceType = NULL, $status = NULL) |
| 82 | { |
| 83 | $resp = $this->url->count($search, $deviceType, $status); |
| 84 | return (int)$resp["count"]; |
| 85 | } |
| 86 | |
| 87 | public function getPendingUrls($page = 1, $limit = 250, $priority = NULL) |
| 88 | { |
| 89 | return $this->url->getpending($page, $limit, $priority); |
| 90 | } |
| 91 | |
| 92 | public function getPendingUrlsCount($priority = NULL) |
| 93 | { |
| 94 | $resp = $this->url->pendingCount($priority); |
| 95 | return (int)$resp["count"]; |
| 96 | } |
| 97 | |
| 98 | public function tagUrl($url, $tag) |
| 99 | { |
| 100 | $resp = @json_decode($this->tagger->tag($url, $tag)->getBody()); |
| 101 | if ($resp && !$resp->success) { |
| 102 | $msg = $resp->error ? $resp->error : "Unable to tag URL"; |
| 103 | throw new \RuntimeException($msg); |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | public function untagUrl($url, $tag) |
| 110 | { |
| 111 | $resp = json_decode($this->tagger->remove($url, $tag)->getBody(), true); |
| 112 | return $resp['removed']; |
| 113 | } |
| 114 | |
| 115 | public function getTaggedUrls($tag, $page = 1, $limit = 250) |
| 116 | { |
| 117 | return $this->tagger->getUrls($tag, $page, $limit); |
| 118 | } |
| 119 | |
| 120 | public function getTaggedUrlsCount($tag) |
| 121 | { |
| 122 | $resp = $this->tagger->getUrlsCount($tag); |
| 123 | return (int)$resp["count"]; |
| 124 | } |
| 125 | |
| 126 | public function getTags($url = NULL, $page = 1, $limit = 250) |
| 127 | { |
| 128 | return $this->tagger->getTags($url, $page, $limit); |
| 129 | } |
| 130 | |
| 131 | public function getSavings() |
| 132 | { |
| 133 | return $this->stats->getSavings(); |
| 134 | } |
| 135 | |
| 136 | public function getDiskUsage() |
| 137 | { |
| 138 | return $this->stats->getDiskUsage(); |
| 139 | } |
| 140 | |
| 141 | public function getRequestUsage() |
| 142 | { |
| 143 | return $this->stats->getRequestUsage(); |
| 144 | } |
| 145 | |
| 146 | public function resetSavingsStats() |
| 147 | { |
| 148 | return $this->stats->resetSavings(); |
| 149 | } |
| 150 | |
| 151 | public function enableWarmup() |
| 152 | { |
| 153 | return $this->warmup->enable(); |
| 154 | } |
| 155 | |
| 156 | public function disableWarmup() |
| 157 | { |
| 158 | return $this->warmup->disable(); |
| 159 | } |
| 160 | |
| 161 | public function resetWarmup() |
| 162 | { |
| 163 | return $this->warmup->reset(); |
| 164 | } |
| 165 | |
| 166 | public function setWarmupSitemap($url) |
| 167 | { |
| 168 | return $this->warmup->setSitemap($url); |
| 169 | } |
| 170 | |
| 171 | public function unsetWarmupSitemap() |
| 172 | { |
| 173 | return $this->warmup->setSitemap(NULL); |
| 174 | } |
| 175 | |
| 176 | public function setWarmupHomepage($url) |
| 177 | { |
| 178 | return $this->warmup->setHomepage($url); |
| 179 | } |
| 180 | |
| 181 | public function unsetWarmupHomepage() |
| 182 | { |
| 183 | return $this->warmup->setHomepage(NULL); |
| 184 | } |
| 185 | |
| 186 | public function estimateWarmup($id = NULL, $urls = NULL) |
| 187 | { |
| 188 | $resp = $this->warmup->estimate($id, $urls); |
| 189 | if ($id) { |
| 190 | return (int)$resp["count"]; |
| 191 | } else { |
| 192 | return $resp["id"]; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | public function runWarmup($urls = NULL, $force = false) |
| 197 | { |
| 198 | return $this->warmup->run($urls, $force); |
| 199 | } |
| 200 | |
| 201 | public function getWarmupStats() |
| 202 | { |
| 203 | return $this->warmup->stats(); |
| 204 | } |
| 205 | |
| 206 | public function unsetWebhook($type) |
| 207 | { |
| 208 | if (!in_array($type, $this->allowedWebhooks)) { |
| 209 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 210 | } |
| 211 | |
| 212 | try { |
| 213 | $this->webhook->set($type, null); |
| 214 | } catch (\RuntimeException $e) { |
| 215 | throw new WebhookException($e->getMessage()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public function setWebhook($type, \NitroPack\Url\Url $url) |
| 220 | { |
| 221 | if (!in_array($type, $this->allowedWebhooks)) { |
| 222 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 223 | } |
| 224 | |
| 225 | if (!$url->isValid()) { |
| 226 | throw new WebhookException(sprintf("The webhook URL '%s' is invalid!", $url->getUrl())); |
| 227 | } |
| 228 | |
| 229 | try { |
| 230 | $this->webhook->set($type, $url->getUrl()); |
| 231 | } catch (\RuntimeException $e) { |
| 232 | throw new WebhookException($e->getMessage()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | public function getWebhook($type) |
| 237 | { |
| 238 | if (!in_array($type, $this->allowedWebhooks)) { |
| 239 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 240 | } |
| 241 | |
| 242 | try { |
| 243 | $resp = $this->webhook->get($type); |
| 244 | return $resp["url"]; |
| 245 | } catch (\RuntimeException $e) { |
| 246 | throw new WebhookException($e->getMessage()); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public function setVariationCookie($name, $values = array(), $group = null) |
| 251 | { |
| 252 | if (!is_array($values) && !is_string($values)) { |
| 253 | throw new VariationCookieException("The provided values is not an array or a string."); |
| 254 | } else if (is_array($values)) { |
| 255 | foreach ($values as $value) { |
| 256 | if (!is_string($value)) { |
| 257 | throw new VariationCookieException("A non-string cookie value has been detected."); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (!is_string($name) || trim($name) == "") { |
| 263 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 264 | } |
| 265 | |
| 266 | if (null !== $group && !filter_var($group, FILTER_VALIDATE_INT)) { |
| 267 | throw new VariationCookieException("The provided group is not null, and it is not a positive integer."); |
| 268 | } |
| 269 | |
| 270 | try { |
| 271 | return $this->variation_cookie->set($name, $values, $group); |
| 272 | } catch (\RuntimeException $e) { |
| 273 | throw new VariationCookieException($e->getMessage()); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public function unsetVariationCookie($name) |
| 278 | { |
| 279 | if (!is_string($name) || trim($name) == "") { |
| 280 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 281 | } |
| 282 | |
| 283 | try { |
| 284 | return $this->variation_cookie->delete($name); |
| 285 | } catch (\RuntimeException $e) { |
| 286 | throw new VariationCookieException($e->getMessage()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public function getVariationCookies() |
| 291 | { |
| 292 | try { |
| 293 | return $this->variation_cookie->get(); |
| 294 | } catch (\RuntimeException $e) { |
| 295 | throw new VariationCookieException($e->getMessage()); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | public function createWebsite(Website $website) |
| 300 | { |
| 301 | return $this->integration->create($website); |
| 302 | } |
| 303 | |
| 304 | public function updateWebsite(Website $website) |
| 305 | { |
| 306 | return $this->integration->update($website); |
| 307 | } |
| 308 | |
| 309 | public function removeWebsite($apikey) |
| 310 | { |
| 311 | return $this->integration->remove($apikey); |
| 312 | } |
| 313 | |
| 314 | // Get a single website via API key |
| 315 | public function getWebsiteByAPIKey($apikey) |
| 316 | { |
| 317 | return $this->integration->readByAPIKey($apikey); |
| 318 | } |
| 319 | |
| 320 | // Get all websites, along with a corresponding pagination |
| 321 | public function getWebsitesPaginated($page, $limit = 250) |
| 322 | { |
| 323 | return $this->integration->readPaginated($page, $limit); |
| 324 | } |
| 325 | |
| 326 | public function isSafeModeEnabled() |
| 327 | { |
| 328 | $status = $this->safe_mode->status(); |
| 329 | return !empty($status->isEnabled); |
| 330 | } |
| 331 | |
| 332 | public function enableCartCache() |
| 333 | { |
| 334 | $this->cache->enableCartCache(); |
| 335 | } |
| 336 | |
| 337 | public function disableCartCache() |
| 338 | { |
| 339 | $this->cache->disableCartCache(); |
| 340 | } |
| 341 | |
| 342 | public function enableVarnishIntegration() |
| 343 | { |
| 344 | $this->varnish->enable(); |
| 345 | } |
| 346 | |
| 347 | public function disableVarnishIntegration() |
| 348 | { |
| 349 | $this->varnish->disable(); |
| 350 | } |
| 351 | |
| 352 | public function configureVarnishIntegration($settings) |
| 353 | { |
| 354 | $this->varnish->configure($settings); |
| 355 | } |
| 356 | |
| 357 | public function getVarnishIntegrationConfig() |
| 358 | { |
| 359 | return $this->varnish->get(); |
| 360 | } |
| 361 | |
| 362 | public function setVarnishIntegrationConfig($settings) |
| 363 | { |
| 364 | $this->varnish->set($settings); |
| 365 | } |
| 366 | |
| 367 | public function enableExcludedUrls() |
| 368 | { |
| 369 | $this->excludedurls->enable(); |
| 370 | } |
| 371 | |
| 372 | public function disableExcludedUrls() |
| 373 | { |
| 374 | $this->excludedurls->disable(); |
| 375 | } |
| 376 | |
| 377 | public function addExcludedUrl($urlPattern) |
| 378 | { |
| 379 | $this->excludedurls->add($urlPattern); |
| 380 | } |
| 381 | |
| 382 | public function removeExcludedUrl($urlPattern) |
| 383 | { |
| 384 | $this->excludedurls->remove($urlPattern); |
| 385 | } |
| 386 | |
| 387 | public function enableAdditionalDomains() |
| 388 | { |
| 389 | $this->additionaldomains->enable(); |
| 390 | } |
| 391 | |
| 392 | public function disableAdditionalDomains() |
| 393 | { |
| 394 | $this->additionaldomains->disable(); |
| 395 | } |
| 396 | |
| 397 | public function addAdditionalDomain($domain) |
| 398 | { |
| 399 | $this->additionaldomains->add($domain); |
| 400 | } |
| 401 | |
| 402 | public function removeAdditionalDomain($domain) |
| 403 | { |
| 404 | $this->additionaldomains->remove($domain); |
| 405 | } |
| 406 | |
| 407 | public function enableExcludes() |
| 408 | { |
| 409 | return $this->excludes->enable(); |
| 410 | } |
| 411 | |
| 412 | public function disableExcludes() |
| 413 | { |
| 414 | return $this->excludes->disable(); |
| 415 | } |
| 416 | |
| 417 | public function getExcludes() |
| 418 | { |
| 419 | return $this->excludes->get(); |
| 420 | } |
| 421 | |
| 422 | public function setExcludes($excludes) |
| 423 | { |
| 424 | return $this->excludes->set($excludes); |
| 425 | } |
| 426 | } |
| 427 |