bokbasen/php-api-client

Generic PHP API client for Bokbasen's API

v3.0.3 2024-04-09 15:00 UTC

This package is auto-updated.

Last update: 2024-04-09 15:02:46 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

Click here if you're looking for documentation to version 1.*

Generic API client for use against all Bokbasen APIs where no spesific SDK is implemented that require authetication.

The HTTP client is simple in use and you must implement API spesific functionality yourself. But it provides a standard interface to do request against Bokbasen APIs and allow you to use the Login SDK handling any complexity related to authentication.

First create a Login object see php-sdk-auth for details

Installation

We are using HttpClientDiscovery so you'll need to require a PSR-7 compatible implementation, e.g.;

$ composer require php-http/guzzle6-adapter

Then add our package:

$ composer require bokbasen/php-api-client

Usage

use Bokbasen\Auth\Login;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

try {
    /* This example is using a file cache for the TGT, you can replace this with any PSR-6 
        compatible cache. Always using caching in production to avoid performance penalty of 
        creating and deleting tokens.
    */
    $cache = new FilesystemAdapter();
    $login = new Login('username', 'password', Login::URL_PROD, $cache);
} catch(\Throwable $e) {
    // error handling
}
use Bokbasen\ApiClient\Client;
use Bokbasen\ApiClient\HttpRequestOptions;
use Bokbasen\ApiClient\Exceptions\BokbasenApiClientException;

try {
    /* Pass the base URL of the API you are interacting with. You can also pass a logger 
        and a custom http client. Any request made through the API returns an instance 
        of \Psr\Http\Message\ResponseInterface.  All of these API calls will include the 
        necessary authentication headers.
    */
    $client = new Client($login, 'https://loan.api.boknett.no');
    
    // Execute get request, it is recommended to explicitly set accept parameter
    $headers = ['Accept' => HttpRequestOptions::CONTENT_TYPE_JSON];
    $response = $client->get('/path', $headers);
    
    // Execute POST request with json data
    $response = $client->postJson('/path', $body, $headers);
    
    // Execute POST request 
    $response = $client->post('/path', $body, $headers);
    
    // Execute PUT request
    $response = $client->put('/path', $body, $headers);
    
    // Execute PATCH request
    $response = $client->patch('/path', $body, $headers);
} catch(BokbasenApiClientException $e){
    //error handling
}

Tests

$ ./vendor/bin/phpunit