commit 98108e766608de2fdf6962d4499aefe901e62f79 Author: César D. Rodas Date: Tue Nov 12 06:34:56 2013 -0300 first commit diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..047614b --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "crodas/influx-php", + "description": "Simple client for InfluxDB", + "license": "BSD", + "authors": [ + { + "name": "César D. Rodas", + "email": "crodas@php.net" + } + ], + "minimum-stability": "dev", + "autoload": { + "classmap":["lib/"] + } +} diff --git a/lib/InfluxPHP/BaseHTTP.php b/lib/InfluxPHP/BaseHTTP.php new file mode 100644 index 0000000..bad9721 --- /dev/null +++ b/lib/InfluxPHP/BaseHTTP.php @@ -0,0 +1,104 @@ + | + +---------------------------------------------------------------------------------+ +*/ + +namespace crodas\InfluxPHP; + +class BaseHTTP +{ + protected $host; + protected $port; + protected $user; + protected $pass; + protected $base; + + protected function inherits(BaseHTTP $c) + { + $this->user = $c->user; + $this->pass = $c->pass; + $this->port = $c->port; + $this->host = $c->host; + } + + protected function getCurl($url) + { + $url = "http://{$this->host}:{$this->port}/{$this->base}{$url}"; + $url .= "?" . http_build_query(['u' => $this->user, 'p' => $this->pass]); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + return $ch; + } + + protected function execCurl($ch, $json = false) + { + $response = curl_exec ($ch); + $status = (string)curl_getinfo($ch, CURLINFO_HTTP_CODE); + //$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); + curl_close($ch); + if ($status[0] != 2) { + throw new \Exception($response); + } + return $json ? json_decode($response, true) : $response; + } + + protected function delete($url) + { + $ch = $this->getCurl($url); + curl_setopt_array($ch, [ + CURLOPT_CUSTOMREQUEST => "DELETE", + ]); + + return $this->execCurl($ch); + } + + protected function get($url) + { + $ch = $this->getCurl($url); + return $this->execCurl($ch, true); + } + + protected function post($url, Array $body) + { + $ch = $this->getCurl($url); + curl_setopt_array($ch, [ + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => json_encode($body), + ]); + + return $this->execCurl($ch); + } + +} diff --git a/lib/InfluxPHP/Client.php b/lib/InfluxPHP/Client.php new file mode 100644 index 0000000..7ca8f2f --- /dev/null +++ b/lib/InfluxPHP/Client.php @@ -0,0 +1,131 @@ + | + +---------------------------------------------------------------------------------+ +*/ + +namespace crodas\InfluxPHP; + +class Client extends BaseHTTP +{ + protected $host; + protected $port; + protected $user; + protected $pass; + + public function __construct($host = "localhost", $port = 8086, $u = 'root', $p = 'root') + { + $this->host = $host; + $this->port = $port; + $this->user = $u; + $this->pass = $p; + } + + protected function getCurl($url) + { + $url = "http://{$this->host}:{$this->port}/{$url}"; + $url .= "?" . http_build_query(['u' => $this->user, 'p' => $this->pass]); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + return $ch; + } + + protected function execCurl($ch, $json = false) + { + $response = curl_exec ($ch); + $status = (string)curl_getinfo($ch, CURLINFO_HTTP_CODE); + //$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); + curl_close($ch); + if ($status[0] != 2) { + throw new \Exception($response); + } + return $json ? json_decode($response, true) : $response; + } + + protected function delete($url) + { + $ch = $this->getCurl($url); + curl_setopt_array($ch, [ + CURLOPT_CUSTOMREQUEST => "DELETE", + ]); + + return $this->execCurl($ch); + } + + protected function get($url) + { + $ch = $this->getCurl($url); + return $this->execCurl($ch, true); + } + + protected function post($url, Array $body) + { + $ch = $this->getCurl($url); + curl_setopt_array($ch, [ + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => json_encode($body), + ]); + + return $this->execCurl($ch); + } + + public function deleteDatabase($name) + { + return $this->delete("db/$name"); + } + + public function createDatabase($name) + { + return $this->post('db', ['name' => $name]); + } + + public function getDatabases() + { + $self = $this; + return array_map(function($obj) use($self) { + return new DB($self, $obj['name']); + }, $this->get('dbs')); + } + + public function getDatabase($name) + { + return new DB($this, $name); + } + + public function __get($name) + { + return new DB($this, $name); + } +} + diff --git a/lib/InfluxPHP/DB.php b/lib/InfluxPHP/DB.php new file mode 100644 index 0000000..622de05 --- /dev/null +++ b/lib/InfluxPHP/DB.php @@ -0,0 +1,68 @@ + | + +---------------------------------------------------------------------------------+ +*/ + +namespace crodas\InfluxPHP; + +class DB extends BaseHTTP +{ + protected $client; + protected $name; + + public function __construct(Client $client, $name) + { + $this->client = $client; + $this->name = $name; + $this->inherits($client); + $this->base = "db/$name/"; + } + + public function insert($name, Array $data) + { + $points = []; + $columns = array_keys(current($data)); + foreach ($data as $value) { + $points[] = array_values($value); + } + $body = compact('name', 'columns', 'points'); + return $this->post('series', [$body]); + } + + public function createUser($username, $password) + { + return $this->post('users', compact('username', 'password')); + } +}