Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joowani committed Aug 22, 2016
1 parent adf8ba8 commit 26ad6a8
Show file tree
Hide file tree
Showing 36 changed files with 1,220 additions and 371 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
*build/
*dist/
*.coverage
.coveragerc
.cache/
tests/__pycache__/
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Here is simple usage example:
# Initialize the client for ArangoDB
client = ArangoClient(
protocol='http',
host="localhost",
host='localhost',
port=8529,
username='root',
password='',
Expand Down
12 changes: 6 additions & 6 deletions arango/aql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
AQLQueryExecuteError,
AQLFunctionCreateError,
AQLFunctionDeleteError,
AQLFunctionsListError,
AQLFunctionListError,
AQLCacheClearError,
AQLCacheConfigureError,
AQLCacheGetPropertiesError
AQLCachePropertiesError
)
from arango.request import Request

Expand Down Expand Up @@ -168,14 +168,14 @@ def functions(self):
:returns: a mapping of AQL function names to its javascript code
:rtype: dict
:raises arango.exceptions.AQLFunctionsListError: if the AQL functions
:raises arango.exceptions.AQLFunctionListError: if the AQL functions
cannot be retrieved
"""
request = Request(method='get', endpoint='/_api/aqlfunction')

def handler(res):
if res.status_code not in HTTP_OK:
raise AQLFunctionsListError(res)
raise AQLFunctionListError(res)
body = res.body or {}
return {func['name']: func['code'] for func in map(dict, body)}

Expand Down Expand Up @@ -259,7 +259,7 @@ def properties(self):
:returns: the cache properties
:rtype: dict
:raises arango.exceptions.AQLCacheGetPropertiesError: if the cache
:raises arango.exceptions.AQLCachePropertiesError: if the cache
properties cannot be retrieved
"""
request = Request(
Expand All @@ -269,7 +269,7 @@ def properties(self):

def handler(res):
if res.status_code not in HTTP_OK:
raise AQLCacheGetPropertiesError(res)
raise AQLCachePropertiesError(res)
return {'mode': res.body['mode'], 'limit': res.body['maxResults']}

return request, handler
Expand Down
19 changes: 11 additions & 8 deletions arango/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
AsyncJobNotDoneError,
AsyncJobNotFoundError,
AsyncJobCancelError,
AsyncJobGetStatusError,
AsyncJobGetResultError,
AsyncJobStatusError,
AsyncJobResultError,
AsyncJobClearError
)
from arango.graph import Graph
Expand Down Expand Up @@ -39,14 +39,15 @@ def __init__(self, connection, return_result=True):
username=connection.username,
password=connection.password,
http_client=connection.http_client,
database=connection.database
database=connection.database,
enable_logging=connection.has_logging
)
self._return_result = return_result
self._aql = AQL(self)
self._type = 'async'

def __repr__(self):
return '<ArangoDB asynchronous request>'
return '<ArangoDB asynchronous execution>'

def handle_request(self, request, handler):
"""Handle the incoming request and response handler.
Expand All @@ -57,6 +58,8 @@ def handle_request(self, request, handler):
:type handler: callable
:returns: the async job or None
:rtype: arango.async.AsyncJob
:raises arango.exceptions.AsyncExecuteError: if the async request
cannot be executed
"""
if self._return_result:
request.headers['x-arango-async'] = 'store'
Expand Down Expand Up @@ -152,7 +155,7 @@ def status(self):
not valid
:raises arango.exceptions.AsyncJobNotFoundError: if the async job
cannot be found in the server
:raises arango.exceptions.AsyncJobGetStatusError: if the status of the
:raises arango.exceptions.AsyncJobStatusError: if the status of the
async job cannot be retrieved from the server
"""
res = self._conn.get('/_api/job/{}'.format(self._id))
Expand All @@ -165,7 +168,7 @@ def status(self):
elif res.status_code == 404:
raise AsyncJobNotFoundError(res)
else:
raise AsyncJobGetStatusError(res)
raise AsyncJobStatusError(res)

def result(self):
"""Return the result of the async job if available.
Expand All @@ -178,7 +181,7 @@ def result(self):
cannot be found in the server
:raises arango.exceptions.AsyncJobNotDoneError: if the async job is
still pending in the queue
:raises arango.exceptions.AsyncJobGetResultError: if the result of the
:raises arango.exceptions.AsyncJobResultError: if the result of the
async job cannot be retrieved from the server
.. note::
Expand All @@ -204,7 +207,7 @@ def result(self):
raise AsyncJobNotDoneError(res, 'Job {} pending'.format(_id))
elif res.status_code == 400:
raise AsyncJobInvalidError(res, 'Job {} invalid'.format(_id))
raise AsyncJobGetResultError(res, 'Failed to query job {}'.format(_id))
raise AsyncJobResultError(res, 'Failed to query job {}'.format(_id))

def cancel(self, ignore_missing=False):
"""Cancel the async job if it is still pending.
Expand Down
5 changes: 3 additions & 2 deletions arango/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def __init__(self, connection, return_result=True, commit_on_error=False):
username=connection.username,
password=connection.password,
http_client=connection.http_client,
database=connection.database
database=connection.database,
enable_logging=connection.has_logging
)
self._id = uuid4()
self._return_result = return_result
Expand All @@ -50,7 +51,7 @@ def __init__(self, connection, return_result=True, commit_on_error=False):
self._type = 'batch'

def __repr__(self):
return '<ArangoDB batch request {}>'.format(self._id)
return '<ArangoDB batch execution {}>'.format(self._id)

def __enter__(self):
return self
Expand Down
64 changes: 37 additions & 27 deletions arango/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def __init__(self,
host='localhost',
port=8529,
username='root',
password='',
verify=True,
password='test',
verify=False,
http_client=None,
enable_logging=True):

Expand All @@ -61,15 +61,25 @@ def __init__(self,
)
self._wal = WriteAheadLog(self._conn)

# Verify the server connection
if verify:
res = self._conn.head('/_api/version')
if res.status_code not in HTTP_OK:
raise ServerConnectionError(res)
self.verify()

def __repr__(self):
return '<ArangoDB client for "{}">'.format(self._host)

def verify(self):
"""Verify the connection to ArangoDB server.
:returns: ``True`` if the connection is successful
:rtype: bool
:raises arango.exceptions.ServerConnectionError: if the connection to
the ArangoDB server fails
"""
res = self._conn.head('/_api/version')
if res.status_code not in HTTP_OK:
raise ServerConnectionError(res)
return True

@property
def protocol(self):
"""Return the internet transfer protocol.
Expand Down Expand Up @@ -147,60 +157,60 @@ def version(self):
:returns: the server version
:rtype: str
:raises arango.exceptions.ServerGetVersionError: if the server version
:raises arango.exceptions.ServerVersionError: if the server version
cannot be retrieved
"""
res = self._conn.get(
endpoint='/_api/version',
params={'details': False}
)
if res.status_code not in HTTP_OK:
raise ServerGetVersionError(res)
raise ServerVersionError(res)
return res.body['version']

def details(self):
"""Return the component details on the ArangoDB server.
:returns: the server details
:rtype: dict
:raises arango.exceptions.ServerGetDetailsError: if the server details
:raises arango.exceptions.ServerDetailsError: if the server details
cannot be retrieved
"""
res = self._conn.get(
endpoint='/_api/version',
params={'details': True}
)
if res.status_code not in HTTP_OK:
raise ServerGetDetailsError(res)
raise ServerDetailsError(res)
return res.body['details']

def required_db_version(self):
"""Return the required version of the target database.
:returns: the required version of the target database
:rtype: str
:raises arango.exceptions.ServerGetRequiredVersionError: if the
:raises arango.exceptions.ServerRequiredDBVersionError: if the
required database version cannot be retrieved
"""
res = self._conn.get('/_admin/database/target-version')
if res.status_code not in HTTP_OK:
raise ServerGetRequiredVersionError(res)
raise ServerRequiredDBVersionError(res)
return res.body['version']

def statistics(self, description=False):
"""Return the server statistics.
:returns: the statistics information
:rtype: dict
:raises arango.exceptions.ServerGetStatisticsError: if the server
:raises arango.exceptions.ServerStatisticsError: if the server
statistics cannot be retrieved
"""
res = self._conn.get(
'/_admin/statistics-description'
if description else '/_admin/statistics'
)
if res.status_code not in HTTP_OK:
raise ServerGetStatisticsError(res)
raise ServerStatisticsError(res)
res.body.pop('code', None)
res.body.pop('error', None)
return res.body
Expand All @@ -215,25 +225,25 @@ def role(self):
in the cluster) or ``"UNDEFINED"`` (the server role is undefined,
the only possible value for a single server)
:rtype: str
:raises arango.exceptions.ServerGetRoleError: if the server role cannot
:raises arango.exceptions.ServerRoleError: if the server role cannot
be retrieved
"""
res = self._conn.get('/_admin/server/role')
if res.status_code not in HTTP_OK:
raise ServerGetRoleError(res)
raise ServerRoleError(res)
return res.body.get('role')

def time(self):
"""Return the current server system time.
:returns: the server system time
:rtype: datetime.datetime
:raises arango.exceptions.ServerGetTimeError: if the server time
:raises arango.exceptions.ServerTimeError: if the server time
cannot be retrieved
"""
res = self._conn.get('/_admin/time')
if res.status_code not in HTTP_OK:
raise ServerGetTimeError(res)
raise ServerTimeError(res)
return datetime.fromtimestamp(res.body['time'])

def endpoints(self):
Expand All @@ -246,12 +256,12 @@ def endpoints(self):
:returns: the list of endpoints
:rtype: list
:raises arango.exceptions.ServerGetEndpointsError: if the endpoints
:raises arango.exceptions.ServerEndpointsError: if the endpoints
cannot be retrieved from the server
"""
res = self._conn.get('/_api/endpoint')
if res.status_code not in HTTP_OK:
raise ServerGetEndpointsError(res)
raise ServerEndpointsError(res)
return res.body

def echo(self):
Expand Down Expand Up @@ -285,7 +295,7 @@ def sleep(self, seconds):
raise ServerSleepError(res)
return res.body['duration']

def shutdown(self):
def shutdown(self): # pragma: no cover
"""Initiate the server shutdown sequence.
:returns: whether the server was shutdown successfully
Expand All @@ -301,7 +311,7 @@ def shutdown(self):
raise ServerShutdownError(res)
return True

def run_tests(self, tests):
def run_tests(self, tests): # pragma: no cover
"""Run the available unittests on the server.
:param tests: list of files containing the test suites
Expand Down Expand Up @@ -384,7 +394,7 @@ def read_log(self,
params['sort'] = sort
res = self._conn.get('/_admin/log')
if res.status_code not in HTTP_OK:
ServerReadLogError(res)
raise ServerReadLogError(res)
if 'totalAmount' in res.body:
res.body['total_amount'] = res.body.pop('totalAmount')
return res.body
Expand Down Expand Up @@ -413,7 +423,7 @@ def databases(self, user_only=False):
:type user_only: bool
:returns: the database names
:rtype: list
:raises arango.exceptions.DatabasesListError: if the database names
:raises arango.exceptions.DatabaseListError: if the database names
cannot be retrieved from the server
"""
# Get the current user's databases
Expand All @@ -422,7 +432,7 @@ def databases(self, user_only=False):
if user_only else '/_api/database'
)
if res.status_code not in HTTP_OK:
raise DatabasesListError(res)
raise DatabaseListError(res)
return res.body['result']

def db(self, name, username=None, password=None):
Expand Down Expand Up @@ -523,12 +533,12 @@ def users(self):
:returns: the mapping of usernames to user details
:rtype: list
:raises arango.exceptions.UsersListError: if the details on the users
:raises arango.exceptions.UserListError: if the details on the users
cannot be retrieved from the server
"""
res = self._conn.get('/_api/user')
if res.status_code not in HTTP_OK:
raise UsersListError(res)
raise UserListError(res)

return [{
'username': record['user'],
Expand Down

0 comments on commit 26ad6a8

Please sign in to comment.