esi/api

使用 Guzzle 的基础 API 客户端的一个简单包装/构建器。

v1.1.0 2024-05-22 02:33 UTC

This package is auto-updated.

Last update: 2024-09-23 03:44:34 UTC


README

Build Status Code Coverage Scrutinizer Code Quality Continuous Integration Latest Stable Version Downloads per Month License

关于

文档可能有些简略,单元测试也需要很多工作。但话虽如此,我创建这个库主要是为了在我的围绕 API 服务项目的项目中使用;将许多在每个 API 服务库中重复的逻辑解耦到自己的库中。

还有很长的路要走,但应该相对稳定。

功能

  • 基于 guzzle/guzzle 作为 HTTP 客户端。
  • 通过 Kevinrob/guzzle-cache-middleware 缓存请求。
  • 可以通过 Guzzle 重试中间件在连接或服务器错误上重试请求。
    • 使用 Client::enableRetryAttempts() 指示客户端尝试重试。
    • 使用 Client::disableRetryAttempts() 禁用尝试重试。
    • 使用 Client::setMaxRetryAttempts() 设置最大重试次数。
  • 可以在 Client::build() 中传递头信息,使其成为 '持久' 头,即每次请求都会发送的头信息。
  • 处理发送请求的一个函数:Client::send()

目前不支持异步请求和连接池。只是常规的、老式的标准请求。

示例

use Esi\Api\Client;

// api url, api key, cache path, does the api require key sent as a query arg, the name of the query arg
$client = new Client('https://myapiurl.com/api', 'myApiKey', '/var/tmp', true, 'api_key');

// Must first 'build' the client with (optional) $options array which can include any valid Guzzle option.
$client->build([
    'persistentHeaders' => [
        'Accept' => 'application/json',
    ],
    'allow_redirects' => true,
    // ... etc.
]);
$client->enableRetryAttempts();
$client->setMaxRetryAttempts(5);

$response = $client->send('GET', '/', ['query' => ['foo' => 'bar']]);

// Decode the json and return as array
$data = $client->toArray($response);

// or... as an object
$data = $client->toObject($response);

// or... for the raw json response, to do with as you will
$data = $client->raw(); // or $response->getBody()->getContents()

需求

  • PHP 8.2.0 或更高版本。

提交错误和功能请求

错误和功能请求在 GitHub 上跟踪

问题是最快报告错误的方式。如果您发现错误或文档错误,请首先检查以下内容

  • 没有关于该错误的已打开问题
  • 该问题尚未被解决(例如在已关闭的问题中)

贡献

CONTRIBUTING.

作者

Eric Sizemore - admin@secondversion.com - https://www.secondversion.com

许可

Esi\Api 在 MIT 许可证下授权 - 有关详细信息,请参阅 LICENSE.md 文件