Configuration.php
398 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Unirest; |
| 6 | |
| 7 | use CoreInterfaces\Http\HttpConfigurations; |
| 8 | |
| 9 | class Configuration |
| 10 | { |
| 11 | /** |
| 12 | * @var string|null |
| 13 | */ |
| 14 | private $cookie; |
| 15 | /** |
| 16 | * @var string|null |
| 17 | */ |
| 18 | private $cookieFile; |
| 19 | private $curlOpts = []; |
| 20 | private $jsonOpts = []; |
| 21 | private $socketTimeout = 0; |
| 22 | private $enableRetries = false; // should we enable retries feature |
| 23 | private $maxNumberOfRetries = 3; // total number of allowed retries |
| 24 | private $retryOnTimeout = false; // Should we retry on timeout? |
| 25 | private $retryInterval = 1.0; // Initial retry interval in seconds, to be increased by backoffFactor |
| 26 | private $maximumRetryWaitTime = 120; // maximum retry wait time (commutative) |
| 27 | private $backoffFactor = 2.0; // backoff factor to be used to increase retry interval |
| 28 | private $httpStatusCodesToRetry = [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]; |
| 29 | private $httpMethodsToRetry = ["GET", "PUT"]; |
| 30 | private $verifyPeer = true; |
| 31 | private $verifyHost = true; |
| 32 | private $defaultHeaders = []; |
| 33 | |
| 34 | private $auth = [ |
| 35 | 'user' => '', |
| 36 | 'pass' => '', |
| 37 | 'method' => CURLAUTH_BASIC |
| 38 | ]; |
| 39 | |
| 40 | private $proxy = [ |
| 41 | 'port' => false, |
| 42 | 'tunnel' => false, |
| 43 | 'address' => false, |
| 44 | 'type' => CURLPROXY_HTTP, |
| 45 | 'auth' => [ |
| 46 | 'user' => '', |
| 47 | 'pass' => '', |
| 48 | 'method' => CURLAUTH_BASIC |
| 49 | ] |
| 50 | ]; |
| 51 | |
| 52 | public static function init(?HttpConfigurations $httpConfigurations = null): self |
| 53 | { |
| 54 | return new self($httpConfigurations); |
| 55 | } |
| 56 | |
| 57 | private function __construct(?HttpConfigurations $httpConfigurations) |
| 58 | { |
| 59 | if (is_null($httpConfigurations)) { |
| 60 | return; |
| 61 | } |
| 62 | $this->timeout($httpConfigurations->getTimeout()) |
| 63 | ->enableRetries($httpConfigurations->shouldEnableRetries()) |
| 64 | ->maxNumberOfRetries($httpConfigurations->getNumberOfRetries()) |
| 65 | ->retryOnTimeout($httpConfigurations->shouldRetryOnTimeout()) |
| 66 | ->retryInterval($httpConfigurations->getRetryInterval()) |
| 67 | ->maximumRetryWaitTime($httpConfigurations->getMaximumRetryWaitTime()) |
| 68 | ->backoffFactor($httpConfigurations->getBackOffFactor()) |
| 69 | ->httpStatusCodesToRetry($httpConfigurations->getHttpStatusCodesToRetry()) |
| 70 | ->httpMethodsToRetry($httpConfigurations->getHttpMethodsToRetry()); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param int $socketTimeout Timeout for API calls in seconds. |
| 75 | */ |
| 76 | public function timeout(int $socketTimeout): self |
| 77 | { |
| 78 | $this->socketTimeout = $socketTimeout; |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param bool $enableRetries Whether to enable retries and backoff feature. |
| 84 | */ |
| 85 | public function enableRetries(bool $enableRetries): self |
| 86 | { |
| 87 | $this->enableRetries = $enableRetries; |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param int $maxNumberOfRetries The number of retries to make. |
| 93 | */ |
| 94 | public function maxNumberOfRetries(int $maxNumberOfRetries): self |
| 95 | { |
| 96 | $this->maxNumberOfRetries = $maxNumberOfRetries; |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param bool $retryOnTimeout Whether to retry on timeout |
| 102 | */ |
| 103 | public function retryOnTimeout(bool $retryOnTimeout): self |
| 104 | { |
| 105 | $this->retryOnTimeout = $retryOnTimeout; |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param float $retryInterval The retry time interval between the endpoint calls. |
| 111 | */ |
| 112 | public function retryInterval(float $retryInterval): self |
| 113 | { |
| 114 | $this->retryInterval = $retryInterval; |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param int $maximumRetryWaitTime The maximum wait time in seconds for overall retrying requests. |
| 120 | */ |
| 121 | public function maximumRetryWaitTime(int $maximumRetryWaitTime): self |
| 122 | { |
| 123 | $this->maximumRetryWaitTime = $maximumRetryWaitTime; |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param float $backoffFactor Exponential backoff factor to increase interval between retries. |
| 129 | */ |
| 130 | public function backoffFactor(float $backoffFactor): self |
| 131 | { |
| 132 | $this->backoffFactor = $backoffFactor; |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param int[] $httpStatusCodesToRetry Http status codes to retry against. |
| 138 | */ |
| 139 | public function httpStatusCodesToRetry(array $httpStatusCodesToRetry): self |
| 140 | { |
| 141 | $this->httpStatusCodesToRetry = $httpStatusCodesToRetry; |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param string[] $httpMethodsToRetry Http methods to retry against. |
| 147 | */ |
| 148 | public function httpMethodsToRetry(array $httpMethodsToRetry): self |
| 149 | { |
| 150 | $this->httpMethodsToRetry = $httpMethodsToRetry; |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set JSON decode mode |
| 156 | * |
| 157 | * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. |
| 158 | * @param int $depth User specified recursion depth. |
| 159 | * @param int $options Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported |
| 160 | * (default is to cast large integers as floats) |
| 161 | */ |
| 162 | public function jsonOpts(bool $assoc = false, int $depth = 512, int $options = 0): self |
| 163 | { |
| 164 | $this->jsonOpts = [$assoc, $depth, $options]; |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Verify SSL peer |
| 170 | * |
| 171 | * @param bool $enabled enable SSL verification, by default is true |
| 172 | */ |
| 173 | public function verifyPeer(bool $enabled): self |
| 174 | { |
| 175 | $this->verifyPeer = $enabled; |
| 176 | return $this; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Verify SSL host |
| 181 | * |
| 182 | * @param bool $enabled enable SSL host verification, by default is true |
| 183 | */ |
| 184 | public function verifyHost(bool $enabled): self |
| 185 | { |
| 186 | $this->verifyHost = $enabled; |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Set default headers to send on every request |
| 192 | * |
| 193 | * @param array $headers headers array |
| 194 | */ |
| 195 | public function defaultHeaders(array $headers): self |
| 196 | { |
| 197 | $this->defaultHeaders = array_merge($this->defaultHeaders, $headers); |
| 198 | return $this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Set a new default header to send on every request |
| 203 | * |
| 204 | * @param string $name header name |
| 205 | * @param string $value header value |
| 206 | */ |
| 207 | public function defaultHeader(string $name, string $value): self |
| 208 | { |
| 209 | $this->defaultHeaders[$name] = $value; |
| 210 | return $this; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Set curl options to send on every request |
| 215 | * |
| 216 | * @param array $options options array |
| 217 | */ |
| 218 | public function curlOpts(array $options): self |
| 219 | { |
| 220 | $this->curlOpts = array_merge($this->curlOpts, $options); |
| 221 | return $this; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Set a new default header to send on every request |
| 226 | * |
| 227 | * @param string|int $name header name |
| 228 | * @param string $value header value |
| 229 | */ |
| 230 | public function curlOpt($name, string $value): self |
| 231 | { |
| 232 | $this->curlOpts[$name] = $value; |
| 233 | return $this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Set a cookie string for enabling cookie handling |
| 238 | * |
| 239 | * @param string $cookie |
| 240 | */ |
| 241 | public function cookie(string $cookie): self |
| 242 | { |
| 243 | $this->cookie = $cookie; |
| 244 | return $this; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set a cookie file path for enabling cookie handling |
| 249 | * |
| 250 | * $cookieFile must be a correct path with write permission |
| 251 | * |
| 252 | * @param string $cookieFile - path to file for saving cookie |
| 253 | */ |
| 254 | public function cookieFile(string $cookieFile): self |
| 255 | { |
| 256 | $this->cookieFile = $cookieFile; |
| 257 | return $this; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Set authentication method to use |
| 262 | * |
| 263 | * @param string $username authentication username |
| 264 | * @param string $password authentication password |
| 265 | * @param integer $method authentication method |
| 266 | */ |
| 267 | public function auth(string $username = '', string $password = '', int $method = CURLAUTH_BASIC): self |
| 268 | { |
| 269 | $this->auth['user'] = $username; |
| 270 | $this->auth['pass'] = $password; |
| 271 | $this->auth['method'] = $method; |
| 272 | return $this; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Set proxy to use |
| 277 | * |
| 278 | * @param string $address proxy address |
| 279 | * @param integer $port proxy port |
| 280 | * @param integer $type (Available options for this are CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, |
| 281 | * CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A and CURLPROXY_SOCKS5_HOSTNAME) |
| 282 | * @param bool $tunnel enable/disable tunneling |
| 283 | */ |
| 284 | public function proxy(string $address, int $port = 1080, int $type = CURLPROXY_HTTP, bool $tunnel = false): self |
| 285 | { |
| 286 | $this->proxy['type'] = $type; |
| 287 | $this->proxy['port'] = $port; |
| 288 | $this->proxy['tunnel'] = $tunnel; |
| 289 | $this->proxy['address'] = $address; |
| 290 | return $this; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Set proxy authentication method to use |
| 295 | * |
| 296 | * @param string $username authentication username |
| 297 | * @param string $password authentication password |
| 298 | * @param integer $method authentication method |
| 299 | */ |
| 300 | public function proxyAuth(string $username = '', string $password = '', int $method = CURLAUTH_BASIC): self |
| 301 | { |
| 302 | $this->proxy['auth']['user'] = $username; |
| 303 | $this->proxy['auth']['pass'] = $password; |
| 304 | $this->proxy['auth']['method'] = $method; |
| 305 | return $this; |
| 306 | } |
| 307 | |
| 308 | public function getTimeout(): int |
| 309 | { |
| 310 | return $this->socketTimeout; |
| 311 | } |
| 312 | |
| 313 | public function shouldEnableRetries(): bool |
| 314 | { |
| 315 | return $this->enableRetries; |
| 316 | } |
| 317 | |
| 318 | public function getNumberOfRetries(): int |
| 319 | { |
| 320 | return $this->maxNumberOfRetries; |
| 321 | } |
| 322 | |
| 323 | public function getRetryInterval(): float |
| 324 | { |
| 325 | return $this->retryInterval; |
| 326 | } |
| 327 | |
| 328 | public function getBackOffFactor(): float |
| 329 | { |
| 330 | return $this->backoffFactor; |
| 331 | } |
| 332 | |
| 333 | public function getMaximumRetryWaitTime(): int |
| 334 | { |
| 335 | return $this->maximumRetryWaitTime; |
| 336 | } |
| 337 | |
| 338 | public function shouldRetryOnTimeout(): bool |
| 339 | { |
| 340 | return $this->retryOnTimeout; |
| 341 | } |
| 342 | |
| 343 | public function getHttpStatusCodesToRetry(): array |
| 344 | { |
| 345 | return $this->httpStatusCodesToRetry; |
| 346 | } |
| 347 | |
| 348 | public function getHttpMethodsToRetry(): array |
| 349 | { |
| 350 | return $this->httpMethodsToRetry; |
| 351 | } |
| 352 | |
| 353 | public function getCookie(): ?string |
| 354 | { |
| 355 | return $this->cookie; |
| 356 | } |
| 357 | |
| 358 | public function getCookieFile(): ?string |
| 359 | { |
| 360 | return $this->cookieFile; |
| 361 | } |
| 362 | |
| 363 | public function getCurlOpts(): array |
| 364 | { |
| 365 | return $this->curlOpts; |
| 366 | } |
| 367 | |
| 368 | public function getJsonOpts(): array |
| 369 | { |
| 370 | return $this->jsonOpts; |
| 371 | } |
| 372 | |
| 373 | public function shouldVerifyPeer(): bool |
| 374 | { |
| 375 | return $this->verifyPeer; |
| 376 | } |
| 377 | |
| 378 | public function shouldVerifyHost(): bool |
| 379 | { |
| 380 | return $this->verifyHost; |
| 381 | } |
| 382 | |
| 383 | public function getDefaultHeaders(): array |
| 384 | { |
| 385 | return $this->defaultHeaders; |
| 386 | } |
| 387 | |
| 388 | public function getAuth(): array |
| 389 | { |
| 390 | return $this->auth; |
| 391 | } |
| 392 | |
| 393 | public function getProxy(): array |
| 394 | { |
| 395 | return $this->proxy; |
| 396 | } |
| 397 | } |
| 398 |