diff --git a/lib/InfluxPHP/BaseHTTP.php b/lib/InfluxPHP/BaseHTTP.php index bad9721..aeb5f04 100644 --- a/lib/InfluxPHP/BaseHTTP.php +++ b/lib/InfluxPHP/BaseHTTP.php @@ -53,10 +53,11 @@ class BaseHTTP $this->host = $c->host; } - protected function getCurl($url) + protected function getCurl($url, Array $args = []) { + $args = array_merge($args, ['u' => $this->user, 'p' => $this->pass]); $url = "http://{$this->host}:{$this->port}/{$this->base}{$url}"; - $url .= "?" . http_build_query(['u' => $this->user, 'p' => $this->pass]); + $url .= "?" . http_build_query($args); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); return $ch; @@ -84,9 +85,9 @@ class BaseHTTP return $this->execCurl($ch); } - protected function get($url) + protected function get($url, Array $args) { - $ch = $this->getCurl($url); + $ch = $this->getCurl($url, $args); return $this->execCurl($ch, true); } diff --git a/lib/InfluxPHP/Client.php b/lib/InfluxPHP/Client.php index 7ca8f2f..49cebec 100644 --- a/lib/InfluxPHP/Client.php +++ b/lib/InfluxPHP/Client.php @@ -52,54 +52,6 @@ class Client extends BaseHTTP $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"); diff --git a/lib/InfluxPHP/DB.php b/lib/InfluxPHP/DB.php index 622de05..9d63485 100644 --- a/lib/InfluxPHP/DB.php +++ b/lib/InfluxPHP/DB.php @@ -61,6 +61,11 @@ class DB extends BaseHTTP return $this->post('series', [$body]); } + public function query($sql) + { + return $this->get('series', ['q' => $sql]); + } + public function createUser($username, $password) { return $this->post('users', compact('username', 'password'));