sofa/http-client
最新稳定版本:v1.2.0
Composer 安装命令:
composer require sofa/http-client
包简介
Factory wrapper around Guzzle HTTP Client to simplify things for lazy dev
README 文档
README
Wrapper around Guzzle HTTP Client to simplify bootstrapping
The simplest example in a Laravel app - ready to copy-paste and run
use Sofa\HttpClient\Factory; // register factory in IoC with default app logger app()->bind(Factory::class, fn () => new Factory(app()->environment('testing'), app('log')->driver())); // then inject or resolve wherever you need $httpClient = app(Factory::class) ->withOptions(['base_uri' => 'https://api.github.com']) ->enableRetries() ->enableLogging() ->make(); $httpClient->get('users/jarektkaczyk'); // et voila! Automatic retries in case of server errors and logging out of the box: [2020-05-22 12:38:32] local.INFO: GET https://api.github.com/users/jarektkaczyk HTTP/1.1 200 (1351 application/json; charset=utf-8) {"request": , "response": { "login": "jarektkaczyk", "blog": "https://softonsofa.com", "location": "Singapore", ... } }
Raw example with more customization:
// Raw example $logger = new Logger('HttpLogger'); $logger->pushHandler( new StreamHandler('/path/to/the/log/' . date('Y-m-d') . '.log')) ); $clientFactory = new Factory($fakeRequests, $logger); $httpClient = $clientFactory ->withOptions([ 'base_uri' => 'https://api.github.com', 'headers' => [ 'User-Agent' => 'My Awesome Client', ], ]) ->enableRetries($retries = 3, $delayInSec = 1, $minStatus = 500) ->enableLogging($customLogFormat) ->withMiddleware($customGuzzleMiddleware) ->make();
Testing
Guzzle on its own offers testing facilities but here we made it even easier.
Just so you don't have to worry about setting up custom testing clients or dropping ifs here and there:
$factory = new Factory(true, $logger); $httpClient = $factory->enableLogging()->make(); $httpClient->get('https://api.github.com/users/jarektkaczyk'); $httpClient->get('https://api.github.com/users/octokit'); $factory->getHistory($httpClient); => [ [ "request" => GuzzleHttp\Psr7\Request {#7218}, "response" => GuzzleHttp\Psr7\Response {#7225}, "error" => null, "options" => [ "synchronous" => true, "handler" => GuzzleHttp\HandlerStack {#7205}, "allow_redirects" => [ "max" => 5, "protocols" => [ "http", "https", ], "strict" => false, "referer" => false, "track_redirects" => false, ], "http_errors" => true, "decode_content" => true, "verify" => true, "cookies" => false, "idn_conversion" => true, ], ], ...
统计信息
- 总下载量: 110.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-05