Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created adapter for Apache Couchdb #276

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,210 changes: 1,210 additions & 0 deletions src/Database/Adapter/CouchDB.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/Database/Adapter/CouchDB/Exception/DocumentNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Utopia\Database\Adapter\CouchDB\Exception;

class DocumentNotFound extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Database/Adapter/CouchDB/Exception/InvalidCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Utopia\Database\Adapter\CouchDB\Exception;

class InvalidCase extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Database/Adapter/CouchDB/Exception/MethodNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Utopia\Database\Adapter\CouchDB\Exception;

class MethodNotFound extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Database/Adapter/CouchDB/Exception/NotSupported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Utopia\Database\Adapter\CouchDB\Exception;

class Notsupported extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Database/Adapter/CouchDB/Exception/NullException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Utopia\Database\Adapter\CouchDB\Exception;

class NullException extends \Exception
{

}
152 changes: 152 additions & 0 deletions src/Database/Adapter/CouchDB/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Utopia\Database\Adapter\CouchDB;

use Exception;
use Utopia\Database\Adapter\CouchDB\Exception\MethodNotFound;

class Request
{
/**
* @var string
*/
private string $host;

/**
* @var int
*/
private int $port;

/**
* @var string
*/
private string $username;

/**
* @var string
*/
private string $password;

/**
* @var bool
*/
private bool $tls;

/**
* @var int
*/
private int $timeout = 60;

/**
* @var array
*/
private array $methods = ['HEAD', 'GET', 'POST', 'PUT', 'DELETE'];

public function __construct(string $host, int $port, string $username, string $password, ?int $timeout = null, bool $tls = false)
{
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->tls = $tls;

if (!is_null($timeout))
$this->timeout = $timeout;
}


/**
* @param string $method
* @param array $data
*
* @return array
* @throws MethodNotFound
*/
private function buildContext(string $method, array|null $data): array
{
if (!in_array(strtoupper($method), $this->methods, true))
throw new MethodNotFound('CouchDB does not support '. strtoupper($method). ' method');

if (!is_null($data)) {
return array(
'http' => array(
'header' => 'Content-type: application/json',
'method' => strtoupper($method),
'content' => json_encode($data),
'timeout' => $this->timeout,
'ignore_errors' => true
)
);
} else {
return array(
'http' => array(
'header' => 'Content-type: application/json',
'method' => strtoupper($method),
'timeout' => $this->timeout,
'ignore_errors' => true
)
);
}

}

public function __call($name, $arguments): array
{
try {
$context = stream_context_create($this->buildContext($name, $arguments[0]['data']));
$url = $this->generateUrl($arguments[0]['uri']);
$response = file_get_contents($url, false, $context);
return [
'code' => $this->parseHeaders($http_response_header)['response_code'],
'body' => json_decode($response, true)
];
} catch (MethodNotFound $e) {
throw $e;
} catch (Exception $e) {
throw $e;
}
}

/**
* Generate Url
*
* @param string $uri
*
* @return string
*/
private function generateUrl(string $uri): string
{
$url = '';
if ($this->tls)
$url .= 'https://';
else
$url .= 'http://';

$url .= "$this->username:$this->password@$this->host:$this->port";

return $url . $uri;
}

/**
* Parse response headers
*
* @param array $headers
*
* @return array
*/
private function parseHeaders(array $headers): array
{
$head = array();
foreach ($headers as $key => $value) {
$t = explode(':', $value, 2);
if (isset($t[1]))
$head[trim($t[0])] = trim($t[1]);
else {
$head[] = $value;
if(preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $value, $out))
$head['response_code'] = intval($out[1]);
}
}
return $head;
}
}
17 changes: 17 additions & 0 deletions src/Database/Adapter/CouchDB/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Utopia\Database\Adapter\CouchDB;

class Response
{
public const OK = 200;
public const CREATED = 201;
public const ACCEPTED = 202;
public const NOT_MODIFIED = 304;
public const BAD_REQUEST = 400;
public const UNAUTHORIZED = 401;
public const FORBIDDEN = 403;
public const NOT_FOUND = 404;
public const CONFLICT = 409;
public const PRECONDITION_FAILED = 412;
}