Simple API Client with OAuth2 Auth handler.
You can install the package via composer:
composer require grixu/api-clientuse 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.
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();$fetcher->fetch(); $parsedCollection = $parser->parse(DtoClass::class);$parsedCollection is \Illuminate\Support\Collection filled with DTOs you
You can adjust global configuration of APIClient in your .env file:
API_ERROR_LOGGING=true API_ERROR_LOG_CHANNEL="api-client"composer testPlease see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email mateusz.gostanski@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.