2017 © Pedro Peláez
 

library apis

A library for defining and using APIs

image

openclerk/apis

A library for defining and using APIs

  • Monday, September 11, 2017
  • by soundasleep
  • Repository
  • 1 Watchers
  • 0 Stars
  • 251 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

openclerk/apis Build Status

A library for defining APIs in Openclerk, live on CryptFolio., (*1)

Installing

Include openclerk/apis as a requirement in your project composer.json, and run composer update to install it into your project:, (*2)

{
  "require": {
    "openclerk/apis": "dev-master"
  }
}

Using

Define subclasses of \Apis\Api to define endpoints and content:, (*3)

/**
 * API to get a single currency properties.
 */
class Currency extends \Apis\Api {

  function getJSON($arguments) {
    $cur = \DiscoveredComponents\Currencies::getInstance($arguments['currency']);
    $result = array(
      'code' => $cur->getCode(),
      'title' => $cur->getName(),
    );

    return $result;
  }

  function getEndpoint() {
    return "/api/v1/currency/:currency";
  }

}

You can then call $api->render() for the specific API., (*4)

Using with magic

Your APIs can be discovered with component-discovery by defining apis.json:, (*5)

{
  "api/v1/currencies": "\\Core\\Api\\Currencies",
  "api/v1/currency/:currency": "\\Core\\Api\\Currency"
}

You can then load these into openclerk/routing at runtime:, (*6)

// load up API routes
foreach (DiscoveredComponents\Apis::getAllInstances() as $uri => $handler) {
  \Openclerk\Router::addRoutes(array(
    $uri => $handler,
  ));
}

Caching

Using openclerk/cache you can also cache API calls:, (*7)

/**
 * API to get a single currency properties.
 */
class Currency extends \Apis\CachedApi {

  // ...

  function getHash($arguments) {
    return substr($arguments['currency'], 0, 32);
  }

  function getAge() {
    return 60; /* cache age in seconds */
  }
}

TODO

  1. Documentation on Apis\Fetch methods
  2. A way to define APIs lazily without instantiating all Apis at every request time
  3. Tests

The Versions

11/09 2017

dev-master

9999999-dev

A library for defining and using APIs

  Sources   Download

The Requires

 

The Development Requires

11/09 2017

0.1.0

0.1.0.0

A library for defining and using APIs

  Sources   Download

The Requires

 

The Development Requires