该软件包最新版本(0.1.0)没有提供许可信息。

一个用于定义和使用API的库

0.1.0 2017-09-11 07:28 UTC

This package is auto-updated.

Last update: 2024-09-14 09:03:11 UTC


README

一个在Openclerk中定义API的库,实时运行在CryptFolio上。

安装

在项目 composer.json 中将 openclerk/apis 包含为依赖项,然后运行 composer update 以将其安装到项目中

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

使用

通过定义 \Apis\Api 的子类来定义端点和内容

/**
 * 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";
  }

}

然后您可以调用特定API的 $api->render()

使用魔法

通过定义 apis.json,您可以使用 component-discovery 发现API

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

然后您可以在运行时将这些加载到 openclerk/routing

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

缓存

使用 openclerk/cache,您还可以缓存API调用

/**
 * 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 */
  }
}

待办事项

  1. 关于 Apis\Fetch 方法的文档
  2. 一种在不请求时实例化所有Api的情况下延迟定义API的方法
  3. 测试