Added query

This commit is contained in:
César D. Rodas 2013-11-12 06:45:04 -03:00
parent 3bc3666e17
commit a953393d15
3 changed files with 10 additions and 52 deletions

View File

@ -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);
}

View File

@ -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");

View File

@ -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'));