Skip to content

Commit 3ea1d35

Browse files
committed
Adding support for /shards
1 parent 01d2b96 commit 3ea1d35

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

arangoasync/collection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from arangoasync.exceptions import (
1919
CollectionPropertiesError,
2020
CollectionResponsibleShardError,
21+
CollectionShardsError,
2122
CollectionStatisticsError,
2223
CollectionTruncateError,
2324
DocumentCountError,
@@ -627,6 +628,41 @@ def response_handler(resp: Response) -> str:
627628

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

631+
async def shards(self, details: Optional[bool] = None) -> Result[Json]:
632+
"""Return collection shards and properties.
633+
634+
Available only in a cluster setup.
635+
636+
Args:
637+
details (bool | None): If set to `True`, include responsible
638+
servers for these shards.
639+
640+
Returns:
641+
dict: Collection shards and properties.
642+
643+
Raises:
644+
CollectionShardsError: If retrieval fails.
645+
646+
References:
647+
- `get-the-shard-ids-of-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#get-the-shard-ids-of-a-collection>`__
648+
""" # noqa: E501
649+
params: Params = {}
650+
if details is not None:
651+
params["details"] = details
652+
653+
request = Request(
654+
method=Method.GET,
655+
endpoint=f"/_api/collection/{self.name}/shards",
656+
params=params,
657+
)
658+
659+
def response_handler(resp: Response) -> Json:
660+
if not resp.is_success:
661+
raise CollectionShardsError(resp, request)
662+
return Response.format_body(self.deserializer.loads(resp.raw_body))
663+
664+
return await self._executor.execute(request, response_handler)
665+
630666
async def has(
631667
self,
632668
document: str | Json,

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ class CollectionResponsibleShardError(ArangoServerError):
199199
"""Failed to retrieve responsible shard."""
200200

201201

202+
class CollectionShardsError(ArangoServerError):
203+
"""Failed to retrieve collection shards."""
204+
205+
202206
class CollectionStatisticsError(ArangoServerError):
203207
"""Failed to retrieve collection statistics."""
204208

tests/test_collection.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from arangoasync.exceptions import (
77
CollectionPropertiesError,
88
CollectionResponsibleShardError,
9+
CollectionShardsError,
910
CollectionStatisticsError,
1011
CollectionTruncateError,
1112
DocumentCountError,
@@ -24,7 +25,7 @@ def test_collection_attributes(db, doc_col):
2425

2526

2627
@pytest.mark.asyncio
27-
async def test_collection_misc_methods(doc_col, bad_col, docs):
28+
async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
2829
# Properties
2930
properties = await doc_col.properties()
3031
assert properties.name == doc_col.name
@@ -41,11 +42,16 @@ async def test_collection_misc_methods(doc_col, bad_col, docs):
4142
await bad_col.statistics()
4243

4344
# Shards
44-
doc = await doc_col.insert(docs[0])
45-
shard = await doc_col.responsible_shard(doc)
46-
assert isinstance(shard, str)
47-
with pytest.raises(CollectionResponsibleShardError):
48-
await bad_col.responsible_shard(doc)
45+
if cluster:
46+
doc = await doc_col.insert(docs[0])
47+
shard = await doc_col.responsible_shard(doc)
48+
assert isinstance(shard, str)
49+
with pytest.raises(CollectionResponsibleShardError):
50+
await bad_col.responsible_shard(doc)
51+
shards = await doc_col.shards(details=True)
52+
assert isinstance(shards, dict)
53+
with pytest.raises(CollectionShardsError):
54+
await bad_col.shards()
4955

5056

5157
@pytest.mark.asyncio

0 commit comments

Comments
 (0)