2017 © Pedro Peláez
 

library tol-api

PHP client library for REST APIs

image

traderinteractive/tol-api

PHP client library for REST APIs

  • Wednesday, June 13, 2018
  • by traderinteractive
  • Repository
  • 20 Watchers
  • 7 Stars
  • 930 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 0 Open issues
  • 24 Versions
  • 94 % Grown

The README.md

tol-api-php

Build Status Scrutinizer Code Quality Coverage Status, (*1)

Latest Stable Version Latest Unstable Version License, (*2)

Total Downloads Daily Downloads Monthly Downloads, (*3)

This is a PHP client for REST APIs like the TraderOnline APIs., (*4)

Requirements

This api client requires PHP 7.3 or newer and uses composer to install further PHP dependencies. See the composer specification for more details., (*5)

When contributing, access to a working mongo database for testing is needed. See the Contribution Guidelines for more details., (*6)

Installation

tol-api-php can be installed for use in your project using composer., (*7)

The recommended way of using this library in your project is to add a composer.json file to your project. The following contents would add tol-api-php as a dependency:, (*8)

composer require traderinteractive/tol-api

Basic Usage

The basic guzzle client, without caching or automated pagination handling is mostly easy to work with., (*9)

To instantiate a client, you need the guzzle adapter, client id, client secret, and API url. This client should work with apis like the TOL APIs., (*10)

use TraderInteractive\Api;
$apiAdapter = new Api\GuzzleAdapter();
$auth = Api\Authentication::createClientCredentials(
    'clientId',
    'clientSecret'
)
$apiClient = new Api\Client(
    $apiAdapter,
    $auth,
    'https://baseApiUrl/v1'
);

Then you can make index requests like below, although it is recommended to take a look at the Collection section below so that you can take advantage of automatic pagination handling. Here's an example of how to fetch a single page of items., (*11)

<li>
index(
    'resourceName',
    array('aFilter' => '5')
);

if ($response->getStatusCode() !== 200) {
    throw new Exception('Non successful index call');
}

$body = json_decode($response->getBody(), true);
$total = $body['pagination']['total'];

// Loop over the first page of items
foreach ($body['result'] as $item) {
    echo "
  • {$item['foo']}
  • \n"; } ?> </ul>

    For getting just a single item back from the api, you can use the get method:, (*12)

    // Get item 1234
    $response = $apiClient->get('resourceName', '1234');
    
    if ($response->getStatusCode() !== 200) {
        throw new Exception('Failed to fetch item 1234');
    }
    
    $item = json_decode($response->getBody(), true);
    echo "Fetched item {$item['foo']}\n";
    

    For creating a new item, you can use the post method:, (*13)

    $response = $apiClient->post(
        'resourceName',
        array(
            'foo' => array(
                'bar' => 'boo',
                'bing' => '5',
            ),
        )
    );
    
    if ($response->getStatusCode() !== 201) {
        throw new Exception('Failed to create item foo');
    }
    
    $item = json_decode($response->getBody(), true);
    echo $item['result']['foo'];
    

    For updating an item, you can use the put method:, (*14)

    // Set item 1234's foo to bar.
    $response = $apiClient->put(
        'resourceName',
        '1234',
        array('bing' => array('foo' => 'bar'))
    );
    
    if ($response->getStatusCode() !== 200) {
        throw new Exception('Failed to update item 1234');
    }
    

    For deleting an item, you can use the delete method:, (*15)

    // Delete item 1234.
    $response = $apiClient->delete('resourceName', '1234');
    
    if ($response->getStatusCode() !== 204) {
        throw new Exception('Failed to delete item 1234');
    }
    

    For making asynchronous requests use the start*() and end() methods:, (*16)

    $handleOne = $apiClient->startGet('resourceName', '1234');
    $handleTwo = $apiClient->startGet('resourceName', '5678');
    
    $responseOne = $apiClient->end($handleOne);
    $responseTwo = $apiClient->end($handleTwo);
    
    if ($responseOne->getStatusCode() !== 200) {
        throw new Exception('Failed to fetch item 1234');
    }
    
    if ($responseTwo->getStatusCode() !== 200) {
        throw new Exception('Failed to fetch item 5678');
    }
    
    $itemOne = json_decode($responseOne->getBody(), true);
    $itemTwo = json_decode($responseTwo->getBody(), true);
    
    echo "Fetched item {$itemOne['foo']}\n";
    echo "Fetched item {$itemTwo['foo']}\n";
    

    Cache

    The library allows for a PSR-16 SimpleCache implementation., (*17)

    Collection

    This is the preferred way to make index requests so that you don't have to handle (or forget to handle!) pagination yourself. Using this iterator is simple with the API Client. As an example, here is a snippet of code that will create a dropdown list of items. WARNING Updates should not be performed to the items in the collection while interating as this may change the pagination., (*18)

    <ul>
    <?php
    $items = new \TraderInteractive\Api\Collection(
        $apiClient,
        'resourceName',
        array('aFilter' => '5')
    );
    foreach ($items as $item) {
        echo "<li>{$item['foo']}</li>\n";
    }
    ?>
    </ul>
    

    Contributing

    If you would like to contribute, please use our build process for any changes and after the build passes, send us a pull request on github!, (*19)

    There is also a docker-based fig configuration that will standup docker containers for the databases, execute the build inside a docker container, and then terminate everything. This is an easy way to build the application:, (*20)

    fig run build
    

    The Versions

    13/06 2018

    dev-master

    9999999-dev

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    13/06 2018

    v3.1.0

    3.1.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    28/03 2018

    v2.x-dev

    2.9999999.9999999.9999999-dev

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    28/03 2018

    v2.0.8

    2.0.8.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    07/03 2018

    v3.0.0

    3.0.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    15/02 2018

    v2.0.7

    2.0.7.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    25/07 2017

    v2.0.6

    2.0.6.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    18/04 2017

    v2.0.5

    2.0.5.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    11/04 2017

    v2.0.4

    2.0.4.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    07/04 2017

    v2.0.3

    2.0.3.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    23/03 2017

    v2.0.2

    2.0.2.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    17/02 2017

    v2.0.1

    2.0.1.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    15/02 2017

    v1.x-dev

    1.9999999.9999999.9999999-dev

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    15/02 2017

    v1.1.2

    1.1.2.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    10/11 2016

    v0.x-dev

    0.9999999.9999999.9999999-dev

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    10/11 2016

    v0.2.1

    0.2.1.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    12/10 2016

    v1.1.1

    1.1.1.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    19/08 2016

    v2.0.0

    2.0.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    04/08 2016

    v1.1.0

    1.1.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    09/06 2016

    v0.2.0

    0.2.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    25/05 2016

    v1.0.0

    1.0.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    20/05 2015

    v0.1.2

    0.1.2.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    02/12 2014

    v0.1.1

    0.1.1.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api

    07/07 2014

    v0.1.0

    0.1.0.0

    PHP client library for REST APIs

      Sources   Download

    MIT

    The Requires

     

    The Development Requires

    by Spencer Rinehart
    by Jonathan Gaillard

    api