Skip to content

Commit

Permalink
Add exists() and dirExists() methods to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Dec 17, 2015
1 parent 9765d99 commit ff56d13
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 154 deletions.
332 changes: 178 additions & 154 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,173 +208,33 @@ public function geVersion()
}

/**
* Make a GET request
*
* @param string $url
* @param array $query_arguments
* @return array
*/
private function httpGet($url, $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

return $this->executeCurlRequest($this->getCurlHandle($url), $url);
}

/**
* Make a POST request
*
* @param string $url
* @param array $payload
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpPost($url, $payload = [], $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));

return $this->executeCurlRequest($curl, $url);
}

/**
* Make a PUT request
*
* @param string $url
* @param array $payload
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpPut($url, $payload = [], $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));

return $this->executeCurlRequest($curl, $url);
}

/**
* Make a DELETE request
*
* @param string $url
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpDelete($url, $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');

return $this->executeCurlRequest($curl, $url);
}

/**
* Initialize curl handle
*
* @param string $url
* @return resource
* {@inheritdoc}
*/
private function getCurlHandle($url)
public function exists($key)
{
if ($curl = curl_init($url)) {
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

if ($this->is_https && $this->verify_ssl_peer) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

if ($this->custom_ca_file) {
curl_setopt($curl, CURLOPT_CAINFO, $this->custom_ca_file);
}
} else {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
try {
$response = $this->httpGet($this->getKeyUrl($key));

return $curl;
} else {
throw new \RuntimeException("Can't create curl handle");
return !empty($response['node']) && array_key_exists('value', $response['node']);
} catch (KeyNotFoundException $e) {
return false;
}
}

/**
* @param resource $curl
* @param string $url
* @param bool|true $decode_etcd_json
* @return array|mixed
* @throws EtcdException
* {@inheritdoc}
*/
private function executeCurlRequest($curl, $url, $decode_etcd_json = true)
public function dirExists($key)
{
$response = curl_exec($curl);

if ($error_code = curl_errno($curl)) {
$error = curl_error($curl);

curl_close($curl);
throw new \RuntimeException("$url request failed. Reason: $error", $error_code);
} else {
curl_close($curl);

if ($decode_etcd_json) {
$response = json_decode($response, true);

if (isset($response['errorCode']) && $response['errorCode']) {
$message = $response['message'];

if (isset($response['cause']) && $response['cause']) {
$message .= '. Cause: ' . $response['cause'];
}

switch ($response['errorCode']) {
case 100:
throw new KeyNotFoundException($message);
case 105:
throw new KeyExistsException($message);
default:
throw new EtcdException($message);
}
}
}

return $response;
try {
return !empty($this->httpGet($this->getKeyUrl($key))['node']['dir']);
} catch (KeyNotFoundException $e) {
return false;
}
}

/**
* Set the value of a key
*
* @param string $key
* @param string $value
* @param int $ttl
* @param array $condition
* @return array
* {@inheritdoc}
*/
public function set($key, $value, $ttl = null, $condition = [])
{
Expand Down Expand Up @@ -629,4 +489,168 @@ public function getKeyValueMap($root = '/', $recursive = true, $key = null)

return $this->values;
}

// ---------------------------------------------------
// Make requests
// ---------------------------------------------------

/**
* Make a GET request
*
* @param string $url
* @param array $query_arguments
* @return array
*/
private function httpGet($url, $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

return $this->executeCurlRequest($this->getCurlHandle($url), $url);
}

/**
* Make a POST request
*
* @param string $url
* @param array $payload
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpPost($url, $payload = [], $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));

return $this->executeCurlRequest($curl, $url);
}

/**
* Make a PUT request
*
* @param string $url
* @param array $payload
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpPut($url, $payload = [], $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));

return $this->executeCurlRequest($curl, $url);
}

/**
* Make a DELETE request
*
* @param string $url
* @param array $query_arguments
* @return array|mixed
* @throws EtcdException
*/
private function httpDelete($url, $query_arguments = [])
{
if (!empty($query_arguments)) {
$url .= '?' . http_build_query($query_arguments);
}

$curl = $this->getCurlHandle($url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');

return $this->executeCurlRequest($curl, $url);
}

/**
* Initialize curl handle
*
* @param string $url
* @return resource
*/
private function getCurlHandle($url)
{
if ($curl = curl_init($url)) {
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

if ($this->is_https && $this->verify_ssl_peer) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

if ($this->custom_ca_file) {
curl_setopt($curl, CURLOPT_CAINFO, $this->custom_ca_file);
}
} else {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}

return $curl;
} else {
throw new \RuntimeException("Can't create curl handle");
}
}

/**
* @param resource $curl
* @param string $url
* @param bool|true $decode_etcd_json
* @return array|mixed
* @throws EtcdException
*/
private function executeCurlRequest($curl, $url, $decode_etcd_json = true)
{
$response = curl_exec($curl);

if ($error_code = curl_errno($curl)) {
$error = curl_error($curl);

curl_close($curl);
throw new \RuntimeException("$url request failed. Reason: $error", $error_code);
} else {
curl_close($curl);

if ($decode_etcd_json) {
$response = json_decode($response, true);

if (isset($response['errorCode']) && $response['errorCode']) {
$message = $response['message'];

if (isset($response['cause']) && $response['cause']) {
$message .= '. Cause: ' . $response['cause'];
}

switch ($response['errorCode']) {
case 100:
throw new KeyNotFoundException($message);
case 105:
throw new KeyExistsException($message);
default:
throw new EtcdException($message);
}
}
}

return $response;
}
}
}
16 changes: 16 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public function getKeyUrl($key);
*/
public function geVersion();

/**
* Return true if key exists
*
* @param string $key
* @return boolean
*/
public function exists($key);

/**
* Return true if directory exists
*
* @param string $key
* @return boolean
*/
public function dirExists($key);

/**
* Set the value of a key
*
Expand Down
Loading

0 comments on commit ff56d13

Please sign in to comment.