grixu / api-client
Simple API Client with OAuth2 Auth handler
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
Simple API Client with OAuth2 Auth handler.
Installation
You can install the package via composer:
composer require grixu/api-client
Usage for JSON API
Create configuration object
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'] );
If you have various values of filter names, or extensive API to handle - consider creating Factory which will be
handling creating JsonApiConfig
. Or keep them in separate config file.
Create fetcher
use Grixu\ApiClient\JsonApiFetcher; $fetcher = new JsonApiFetcher($config, '/api/path');
Here, you can adjust your query using UrlCompose
by adding filters, sorts, includes:
// 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();
Fetch Data
$fetcher->fetch(); $parsedCollection = $parser->parse(DtoClass::class);
$parsedCollection
is \Illuminate\Support\Collection
filled with DTOs you
Configuration
You can adjust global configuration of APIClient in your .env
file:
API_ERROR_LOGGING=true
API_ERROR_LOG_CHANNEL="api-client"
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email mateusz.gostanski@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.