grixu/api-client

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple API Client with OAuth2 Auth handler


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.