@ 2017-07-01T15:38:45Z <?php
declare(strict_types=1);
/*
* This file is part of Guzzle Factory.
*
* (c) Graham Campbell <graham@alt-three.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace GrahamCampbell\GuzzleFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* This is the guzzle factory class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
final class GuzzleFactory
{
/**
* The default connect timeout.
*
* @var int
*/
const CONNECT_TIMEOUT = 10;
/**
* The default connect timeout.
*
* @var int
*/
const TIMEOUT = 15;
/**
* The default backoff multiplier.
*
* @var int
*/
const BACKOFF = 1000;
/**
* The default 4xx retry codes.
*
* @var int[]
*/
const CODES = [429];
/**
* Create a new guzzle client.
*
* @param array $options
* @param int|null $backoff
* @param int[]|null $codes
*
* @return \GuzzleHttp\Client
*/
public static function make(array $options = [], int $backoff = null, array $codes = null)
{
return new Client(array_merge(['handler' => self::handler($backoff, $codes), 'connect_timeout' => self::CONNECT_TIMEOUT, 'timeout' => self::TIMEOUT], $options));
}
/**
* Create a new guzzle handler stack.
*
* @param int|null $backoff
* @param int[]|null $codes
*
* @return \GuzzleHttp\Client
*/
public static function handler(int $backoff = null, array $codes = null)
{
$stack = HandlerStack::create();
$stack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, TransferException $exception = null) {
return $retries < 3 && ($exception instanceof ConnectException || ($response && ($response->getStatusCode() >= 500 || in_array($codes === null ? self::CODES : $codes, $response->getStatusCode()))));
}, function ($retries) use ($backoff) {
return (int) pow(2, $retries) * ($backoff === null ? self::BACKOFF : $backoff);
}));
return $stack;
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.5 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
preferences:dark mode live preview
153.58 ms | 402 KiB | 173 Q