Skip to content

Adding more REST methods #29

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

Merged
merged 3 commits into from
Oct 29, 2024
Merged
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
45 changes: 41 additions & 4 deletions arangoasync/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
HTTP_PRECONDITION_FAILED,
)
from arangoasync.exceptions import (
CollectionPropertiesError,
DocumentGetError,
DocumentInsertError,
DocumentParseError,
Expand All @@ -18,7 +19,7 @@
from arangoasync.request import Method, Request
from arangoasync.response import Response
from arangoasync.serialization import Deserializer, Serializer
from arangoasync.typings import Json, Params, Result
from arangoasync.typings import CollectionProperties, Json, Params, Result

T = TypeVar("T")
U = TypeVar("U")
Expand Down Expand Up @@ -48,9 +49,6 @@ def __init__(
self._doc_deserializer = doc_deserializer
self._id_prefix = f"{self._name}/"

def __repr__(self) -> str:
return f"<StandardCollection {self.name}>"

def _validate_id(self, doc_id: str) -> str:
"""Check the collection name in the document ID.

Expand Down Expand Up @@ -148,6 +146,15 @@ def name(self) -> str:
"""
return self._name

@property
def db_name(self) -> str:
"""Return the name of the current database.

Returns:
str: Database name.
"""
return self._executor.db_name


class StandardCollection(Collection[T, U, V]):
"""Standard collection API wrapper.
Expand All @@ -168,6 +175,33 @@ def __init__(
) -> None:
super().__init__(executor, name, doc_serializer, doc_deserializer)

def __repr__(self) -> str:
return f"<StandardCollection {self.name}>"

async def properties(self) -> Result[CollectionProperties]:
"""Return the full properties of the current collection.

Returns:
CollectionProperties: Properties.

Raises:
CollectionPropertiesError: If retrieval fails.

References:
- `get-the-properties-of-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#get-the-properties-of-a-collection>`__
""" # noqa: E501
request = Request(
method=Method.GET,
endpoint=f"/_api/collection/{self.name}/properties",
)

def response_handler(resp: Response) -> CollectionProperties:
if not resp.is_success:
raise CollectionPropertiesError(resp, request)
return CollectionProperties(self._executor.deserialize(resp.raw_body))

return await self._executor.execute(request, response_handler)

async def get(
self,
document: str | Json,
Expand Down Expand Up @@ -269,6 +303,9 @@ async def insert(
bool | dict: Document metadata (e.g. document id, key, revision) or `True`
if **silent** is set to `True`.

Raises:
DocumentInsertError: If insertion fails.

References:
- `create-a-document <https://docs.arangodb.com/stable/develop/http-api/documents/#create-a-document>`__
""" # noqa: E501
Expand Down
Loading
Loading