grixu / api-client
此包已被废弃且不再维护。未建议替代包。
带有 OAuth2 认证处理器的简单 API 客户端
3.5.2
2021-11-18 11:31 UTC
Requires
- php: ^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.2
- guzzlehttp/psr7: ^1.7 || ^2.0
- illuminate/http: ^8.0
- illuminate/support: ^8.0
- spatie/laravel-enum: ^2.2
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- friendsofphp/php-cs-fixer: ^3.1
- nunomaduro/larastan: ^0.7.12 || ^1.0.0
- nunomaduro/phpinsights: ^2.0
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.4
- spatie/data-transfer-object: ^3.1
- spatie/x-ray: ^1.1
- dev-main
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.1
- 3.4.0
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.0.1
- 2.0.0
- 1.0.0
- dev-dependabot/github_actions/shivammathur/setup-php-2.23.0
- dev-dependabot/github_actions/8398a7/action-slack-3.15.0
- dev-dependabot/github_actions/dependabot/fetch-metadata-1.3.5
- dev-dependabot/github_actions/actions/cache-3.0.11
- dev-dependabot/github_actions/actions/checkout-3.1.0
- dev-dependabot/composer/spatie/laravel-enum-tw-2.2or-tw-3.0
- dev-dev/3.3.x
This package is auto-updated.
Last update: 2023-01-27 00:22:50 UTC
README
带有 OAuth2 认证处理器的简单 API 客户端。
安装
您可以通过 composer 安装此包
composer require grixu/api-client
JSON API 使用
创建配置对象
use Grixu\ApiClient\Config\JsonApiConfig; use Grixu\ApiClient\Data\PaginatedData; use Grixu\ApiClient\Data\StraightKeyParser; $config = new JsonApiConfig( baseUrl: 'http://rywal.com.pl', responseDataClass: PaginatedData::class, responseParserClass: StraightKeyParser::class, authType: 'oAuth2', // or you can use enum: AuthType::OAUTH2() authUrl: 'http://rywal.com.pl', authData: ['key', 'secret'], paginationParam: 'page', filters: ['list', 'of', 'param', 'names', 'that', 'could', 'be', 'used', 'as', 'filters'], includes: ['same', 'but', 'for', 'includes'], sorts: ['same', 'this', 'time', 'for', 'sort', 'options'] );
如果您有各种过滤器名称的值,或者需要处理大量的 API,请考虑创建一个工厂来处理创建 JsonApiConfig
。或者将它们保存在单独的配置文件中。
创建抓取器
use Grixu\ApiClient\JsonApiFetcher; $fetcher = new JsonApiFetcher($config, '/api/path');
在这里,您可以通过添加过滤器、排序、包含等来使用 UrlCompose
调整您的查询
// in every example you could pass multiple values $fetcher->compose()->addFilter('filter_name', 'filter_value_1'); $fetcher->compose()->addInclude('include', 'include_relationship_1', 'include_relationship_2'); $fetcher->compose()->addSort('sort', 'sort_field'); //also you could set page in pagination $fetcher->compose()->setPage('page', 2); // or simply move to next page by hand $fetcher->compose()->nextPage();
抓取数据
$fetcher->fetch(); $parsedCollection = $parser->parse(DtoClass::class);
$parsedCollection
是填充了 DTO 的 \Illuminate\Support\Collection
配置
您可以在 .env
文件中调整 APIClient 的全局配置
API_ERROR_LOGGING=true
API_ERROR_LOG_CHANNEL="api-client"
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 mateusz.gostanski@gmail.com 反馈,而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。