corley/influxdb-http-handlers

此软件包最新版本(dev-master)没有提供许可信息。

dev-master 2015-10-18 15:41 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:42:04 UTC


README

Build Status

感谢GuzzleHTTP中间件基础设施,我们可以将InfluxDB的HTTP响应转换为更简单的数据结构,同样我们也可以管理查询错误。

转换数据结构

实际上,InfluxDB-PHP-SDK(由Corley开发)不管理从InfluxDB接收到的JSON消息。这意味着你必须处理这类数据

array(1) {
  'results' =>
  array(1) {
    [0] =>
    array(1) {
      'series' =>
      array(1) {
        ...
      }
    }
  }
}

如果你更喜欢更简单的数据结构,你可以在GuzzleHTTP客户端中添加响应处理器,以便在运行时直接转换InfluxDB响应,并获得更简单、更易读的内容

array(1) {
  'cpu' => array(2) {
    [0] => array(4) {
      'time' => string(30) "2015-09-09T20:42:07.927267636Z"
      'value1' => int(1)
      'value2' => int(2)
      'valueS' => string(6) "string"
    }
    [1] => array(4) {
      'time' => string(30) "2015-09-09T20:42:51.332853369Z"
      'value1' => int(2)
      'value2' => int(4)
      'valueS' => string(11) "another-one"
    }
  }
}

这比InfluxDB默认响应更容易使用

添加响应处理器

在你的GuzzleHTTP客户端设置期间,只需推送message_handler处理器

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client as HttpClient;
use InfluxDB\Client;
use InfluxDB\Options;
use InfluxDB\Adapter\GuzzleAdapter;

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(\InfluxDB\Handler\message_handler()); // Push the response handler

$http = new HttpClient(['handler' => $stack]);

$options = new Options();
$adapter = new GuzzleAdapter($http, $options);

$client = new Client($adapter);

错误/异常管理

使用相同的方法,你可以使用exception_handler将InfluxDB错误响应转换为PHP异常。这样,当查询失败并带有错误消息时,处理器会将失败转换为有效的PHP异常UnexceptionValueException

只需在GuzzleHTTP客户端中推送exception_handler

$stack->push(\InfluxDB\Handler\exception_handler()); // Push the response handler

层顺序

修改InfluxDB响应的处理程序应该按顺序排列

  • exception_handler应作为第一个
  • message_handler

你可以添加任意数量的处理程序

$stack->push(\InfluxDB\Handler\exception_handler());
$stack->push(\InfluxDB\Handler\message_handler());

这样你将获得新的响应格式和错误消息