carsguide/apiwrapper

Service api wrapper

v8.0.0 2021-10-01 03:50 UTC

README

Api wrapper for micro services

Installation

Via composer

$ composer require carsguide/apiwrapper

Configuration settings

Connections are added through a connections config file. To create a connection, add the config.connections file. Each item in the array points to another array containing the host and version of the api to be used.

return [
    'connection_name' => [
        'host' => 'example.com',
        'version' => 'v1',
        'audience' => 'Api Audience'
    ],
];

Usage

Create request

$response = $this->api->setApi('vader')
        ->setRequestType('POST')
        ->setResource('/resource')
        ->setBody('body')
        ->setHeaders([
            'content-type' => 'application/json',
        ])
        ->setBearerToken($access_token)
        ->makeRequest();

$body = $this->api->decodeBody();

Request via Get method

    $response = $this->api->setApi($api)
        ->setBearerToken($access_token)
        ->get($endpoint);

    $body = json_decode($response->getBody());

Request via helpers

    $response = api_wrapper($api)->get($endpoint);

    $body = json_decode($response->getBody());