|
1 | 1 | __all__ = [
|
2 | 2 | "Database",
|
| 3 | + "StandardDatabase", |
3 | 4 | ]
|
4 | 5 |
|
5 |
| -from arangoasync.connection import BaseConnection |
| 6 | +import json |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from arangoasync.connection import Connection |
| 10 | +from arangoasync.exceptions import ServerStatusError |
| 11 | +from arangoasync.executor import ApiExecutor, DefaultApiExecutor |
| 12 | +from arangoasync.request import Method, Request |
| 13 | +from arangoasync.response import Response |
6 | 14 |
|
7 | 15 |
|
8 | 16 | class Database:
|
9 | 17 | """Database API."""
|
10 | 18 |
|
11 |
| - def __init__(self, connection: BaseConnection) -> None: |
12 |
| - self._conn = connection |
| 19 | + def __init__(self, executor: ApiExecutor) -> None: |
| 20 | + self._executor = executor |
13 | 21 |
|
14 | 22 | @property
|
15 |
| - def conn(self) -> BaseConnection: |
| 23 | + def connection(self) -> Connection: |
16 | 24 | """Return the HTTP connection."""
|
17 |
| - return self._conn |
| 25 | + return self._executor.connection |
| 26 | + |
| 27 | + @property |
| 28 | + def name(self) -> str: |
| 29 | + """Return the name of the current database.""" |
| 30 | + return self.connection.db_name |
| 31 | + |
| 32 | + # TODO - user real return type |
| 33 | + async def status(self) -> Any: |
| 34 | + """Query the server status. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + Json: Server status. |
| 38 | +
|
| 39 | + Raises: |
| 40 | + ServerSatusError: If retrieval fails. |
| 41 | + """ |
| 42 | + request = Request(method=Method.GET, endpoint="/_admin/status") |
| 43 | + |
| 44 | + # TODO |
| 45 | + # - introduce specific return type for response_handler |
| 46 | + # - introduce specific serializer and deserializer |
| 47 | + def response_handler(resp: Response) -> Any: |
| 48 | + if not resp.is_success: |
| 49 | + raise ServerStatusError(resp, request) |
| 50 | + return json.loads(resp.raw_body) |
| 51 | + |
| 52 | + return await self._executor.execute(request, response_handler) |
| 53 | + |
| 54 | + |
| 55 | +class StandardDatabase(Database): |
| 56 | + """Standard database API wrapper.""" |
| 57 | + |
| 58 | + def __init__(self, connection: Connection) -> None: |
| 59 | + super().__init__(DefaultApiExecutor(connection)) |
0 commit comments