first commit

This commit is contained in:
César D. Rodas 2013-11-12 06:34:56 -03:00
commit 98108e7666
4 changed files with 318 additions and 0 deletions

15
composer.json Normal file
View File

@ -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/"]
}
}

104
lib/InfluxPHP/BaseHTTP.php Normal file
View File

@ -0,0 +1,104 @@
<?php
/*
+---------------------------------------------------------------------------------+
| Copyright (c) 2013 César Rodas |
+---------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
| 1. Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| 3. All advertising materials mentioning features or use of this software |
| must display the following acknowledgement: |
| This product includes software developed by César D. Rodas. |
| |
| 4. Neither the name of the César D. Rodas nor the |
| names of its contributors may be used to endorse or promote products |
| derived from this software without specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
+---------------------------------------------------------------------------------+
| Authors: César Rodas <crodas@php.net> |
+---------------------------------------------------------------------------------+
*/
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);
}
}

131
lib/InfluxPHP/Client.php Normal file
View File

@ -0,0 +1,131 @@
<?php
/*
+---------------------------------------------------------------------------------+
| Copyright (c) 2013 César Rodas |
+---------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
| 1. Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| 3. All advertising materials mentioning features or use of this software |
| must display the following acknowledgement: |
| This product includes software developed by César D. Rodas. |
| |
| 4. Neither the name of the César D. Rodas nor the |
| names of its contributors may be used to endorse or promote products |
| derived from this software without specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
+---------------------------------------------------------------------------------+
| Authors: César Rodas <crodas@php.net> |
+---------------------------------------------------------------------------------+
*/
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);
}
}

68
lib/InfluxPHP/DB.php Normal file
View File

@ -0,0 +1,68 @@
<?php
/*
+---------------------------------------------------------------------------------+
| Copyright (c) 2013 César Rodas |
+---------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
| 1. Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| 3. All advertising materials mentioning features or use of this software |
| must display the following acknowledgement: |
| This product includes software developed by César D. Rodas. |
| |
| 4. Neither the name of the César D. Rodas nor the |
| names of its contributors may be used to endorse or promote products |
| derived from this software without specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
+---------------------------------------------------------------------------------+
| Authors: César Rodas <crodas@php.net> |
+---------------------------------------------------------------------------------+
*/
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'));
}
}