Api
5 years ago
Integrations
6 years ago
StorageDriver
5 years ago
Url
6 years ago
data
6 years ago
Api.php
5 years ago
Backlog.php
5 years ago
Crypto.php
6 years ago
Device.php
6 years ago
DeviceType.php
6 years ago
EmptyConfigException.php
6 years ago
FileHandle.php
5 years ago
Filesystem.php
5 years ago
HealthStatus.php
5 years ago
IntegrationUrl.php
6 years ago
NitroPack.php
5 years ago
Pagecache.php
5 years ago
PurgeType.php
6 years ago
ServiceDownException.php
5 years ago
StorageException.php
6 years ago
Website.php
6 years ago
Api.php
278 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK; |
| 3 | |
| 4 | use \NitroPack\SDK\Website as Website; |
| 5 | |
| 6 | class Api { |
| 7 | private $cache; |
| 8 | private $tagger; |
| 9 | private $url; |
| 10 | private $allowedWebhooks = array('config', 'cache_clear', 'cache_ready', 'sitemap'); |
| 11 | private $children; |
| 12 | |
| 13 | public function __construct($siteId, $siteSecret) { |
| 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["request_maker"] = new Api\RequestMaker($siteId); |
| 24 | $this->children["secure_request_maker"] = new Api\SecureRequestMaker($siteId, $siteSecret); |
| 25 | |
| 26 | foreach ($this->children as $name=>$child) { |
| 27 | $this->{$name} = $child; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public function setBacklog($backlog) { |
| 32 | foreach ($this->children as $child) { |
| 33 | $child->setBacklog($backlog); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function setNitroPack($nitropack) { |
| 38 | foreach ($this->children as $child) { |
| 39 | $child->setNitroPack($nitropack); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function getCache($url, $userAgent, $cookies, $isAjax, $layout) { |
| 44 | return $this->cache->get($url, $userAgent, $cookies, $isAjax, $layout); |
| 45 | } |
| 46 | |
| 47 | public function getLastCachePurge() { |
| 48 | return $this->cache->getLastPurge(); |
| 49 | } |
| 50 | |
| 51 | public function purgeCache($url = NULL, $pagecacheOnly = false, $reason = NULL) { |
| 52 | return $this->cache->purge($url, $pagecacheOnly, $reason); |
| 53 | } |
| 54 | |
| 55 | public function purgeCacheByTag($tag, $reason = NULL) { |
| 56 | return $this->cache->purgeByTag($tag, $reason); |
| 57 | } |
| 58 | |
| 59 | public function getUrls($page = 1, $limit = 250, $search = NULL, $deviceType = NULL, $status = NULL) { |
| 60 | return $this->url->get($page, $limit, $search, $deviceType, $status); |
| 61 | } |
| 62 | |
| 63 | public function getUrlsCount($search = NULL, $deviceType = NULL, $status = NULL) { |
| 64 | $resp = $this->url->count($search, $deviceType, $status); |
| 65 | return (int)$resp["count"]; |
| 66 | } |
| 67 | |
| 68 | public function getPendingUrls($page = 1, $limit = 250, $priority = NULL) { |
| 69 | return $this->url->getpending($page, $limit, $priority); |
| 70 | } |
| 71 | |
| 72 | public function getPendingUrlsCount($priority = NULL) { |
| 73 | $resp = $this->url->pendingCount($priority); |
| 74 | return (int)$resp["count"]; |
| 75 | } |
| 76 | |
| 77 | public function tagUrl($url, $tag) { |
| 78 | $resp = @json_decode($this->tagger->tag($url, $tag)->getBody()); |
| 79 | if ($resp && !$resp->success) { |
| 80 | $msg = $resp->error ? $resp->error : "Unable to tag URL"; |
| 81 | throw new \RuntimeException($msg); |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | public function untagUrl($url, $tag) { |
| 88 | $resp = json_decode($this->tagger->remove($url, $tag)->getBody(), true); |
| 89 | return $resp['removed']; |
| 90 | } |
| 91 | |
| 92 | public function getTaggedUrls($tag, $page = 1, $limit = 250) { |
| 93 | return $this->tagger->getUrls($tag, $page, $limit); |
| 94 | } |
| 95 | |
| 96 | public function getTaggedUrlsCount($tag) { |
| 97 | $resp = $this->tagger->getUrlsCount($tag); |
| 98 | return (int)$resp["count"]; |
| 99 | } |
| 100 | |
| 101 | public function getTags($url = NULL, $page = 1, $limit = 250) { |
| 102 | return $this->tagger->getTags($url, $page, $limit); |
| 103 | } |
| 104 | |
| 105 | public function getSavings() { |
| 106 | return $this->stats->getSavings(); |
| 107 | } |
| 108 | |
| 109 | public function getDiskUsage() { |
| 110 | return $this->stats->getDiskUsage(); |
| 111 | } |
| 112 | |
| 113 | public function getRequestUsage() { |
| 114 | return $this->stats->getRequestUsage(); |
| 115 | } |
| 116 | |
| 117 | public function resetSavingsStats() { |
| 118 | return $this->stats->resetSavings(); |
| 119 | } |
| 120 | |
| 121 | public function enableWarmup() { |
| 122 | return $this->warmup->enable(); |
| 123 | } |
| 124 | |
| 125 | public function disableWarmup() { |
| 126 | return $this->warmup->disable(); |
| 127 | } |
| 128 | |
| 129 | public function resetWarmup() { |
| 130 | return $this->warmup->reset(); |
| 131 | } |
| 132 | |
| 133 | public function setWarmupSitemap($url) { |
| 134 | return $this->warmup->setSitemap($url); |
| 135 | } |
| 136 | |
| 137 | public function unsetWarmupSitemap() { |
| 138 | return $this->warmup->setSitemap(NULL); |
| 139 | } |
| 140 | |
| 141 | public function setWarmupHomepage($url) { |
| 142 | return $this->warmup->setHomepage($url); |
| 143 | } |
| 144 | |
| 145 | public function unsetWarmupHomepage() { |
| 146 | return $this->warmup->setHomepage(NULL); |
| 147 | } |
| 148 | |
| 149 | public function estimateWarmup($id = NULL, $urls = NULL) { |
| 150 | $resp = $this->warmup->estimate($id, $urls); |
| 151 | if ($id) { |
| 152 | return (int)$resp["count"]; |
| 153 | } else { |
| 154 | return $resp["id"]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public function runWarmup($urls = NULL, $force = false) { |
| 159 | return $this->warmup->run($urls, $force); |
| 160 | } |
| 161 | |
| 162 | public function getWarmupStats() { |
| 163 | return $this->warmup->stats(); |
| 164 | } |
| 165 | |
| 166 | public function unsetWebhook($type) { |
| 167 | if (!in_array($type, $this->allowedWebhooks)) { |
| 168 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 169 | } |
| 170 | |
| 171 | try { |
| 172 | $this->webhook->set($type, null); |
| 173 | } catch (\RuntimeException $e) { |
| 174 | throw new WebhookException($e->getMessage()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public function setWebhook($type, \NitroPack\Url $url) { |
| 179 | if (!in_array($type, $this->allowedWebhooks)) { |
| 180 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 181 | } |
| 182 | |
| 183 | if (!filter_var($url->getUrl(), FILTER_VALIDATE_URL)) { |
| 184 | throw new WebhookException(sprintf("The webhook URL '%s' is invalid!", $url->getUrl())); |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | $this->webhook->set($type, $url->getUrl()); |
| 189 | } catch (\RuntimeException $e) { |
| 190 | throw new WebhookException($e->getMessage()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | public function getWebhook($type) { |
| 195 | if (!in_array($type, $this->allowedWebhooks)) { |
| 196 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 197 | } |
| 198 | |
| 199 | try { |
| 200 | $resp = $this->webhook->get($type); |
| 201 | return $resp["url"]; |
| 202 | } catch (\RuntimeException $e) { |
| 203 | throw new WebhookException($e->getMessage()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public function setVariationCookie($name, $values = array(), $group = null) { |
| 208 | if (!is_array($values) && !is_string($values)) { |
| 209 | throw new VariationCookieException("The provided values is not an array or a string."); |
| 210 | } else if (is_array($values)) { |
| 211 | foreach ($values as $value) { |
| 212 | if (!is_string($value)) { |
| 213 | throw new VariationCookieException("A non-string cookie value has been detected."); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (!is_string($name) || trim($name) == "") { |
| 219 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 220 | } |
| 221 | |
| 222 | if (null !== $group && !filter_var($group, FILTER_VALIDATE_INT)) { |
| 223 | throw new VariationCookieException("The provided group is not null, and it is not a positive integer."); |
| 224 | } |
| 225 | |
| 226 | try { |
| 227 | return $this->variation_cookie->set($name, $values, $group); |
| 228 | } catch (\RuntimeException $e) { |
| 229 | throw new VariationCookieException($e->getMessage()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public function unsetVariationCookie($name) { |
| 234 | if (!is_string($name) || trim($name) == "") { |
| 235 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 236 | } |
| 237 | |
| 238 | try { |
| 239 | return $this->variation_cookie->delete($name); |
| 240 | } catch (\RuntimeException $e) { |
| 241 | throw new VariationCookieException($e->getMessage()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | public function getVariationCookies() { |
| 246 | try { |
| 247 | return $this->variation_cookie->get(); |
| 248 | } catch (\RuntimeException $e) { |
| 249 | throw new VariationCookieException($e->getMessage()); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public function createWebsite(Website $website) { |
| 254 | return $this->integration->create($website); |
| 255 | } |
| 256 | |
| 257 | public function updateWebsite(Website $website) { |
| 258 | return $this->integration->update($website); |
| 259 | } |
| 260 | |
| 261 | public function removeWebsite($apikey) { |
| 262 | return $this->integration->remove($apikey); |
| 263 | } |
| 264 | |
| 265 | // Get a single website via API key |
| 266 | public function getWebsiteByAPIKey($apikey) { |
| 267 | return $this->integration->readByAPIKey($apikey); |
| 268 | } |
| 269 | |
| 270 | // Get all websites, along with a corresponding pagination |
| 271 | public function getWebsitesPaginated($page, $limit = 250) { |
| 272 | return $this->integration->readPaginated($page, $limit); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | class WebhookException extends \Exception {} |
| 277 | class VariationCookieException extends \Exception {} |
| 278 |