Skip to content

Commit 1d08ba4

Browse files
committed
Adding view management
1 parent 7f0ed63 commit 1d08ba4

File tree

4 files changed

+412
-0
lines changed

4 files changed

+412
-0
lines changed

arangoasync/database.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
UserListError,
4747
UserReplaceError,
4848
UserUpdateError,
49+
ViewCreateError,
50+
ViewDeleteError,
51+
ViewGetError,
52+
ViewListError,
53+
ViewRenameError,
54+
ViewReplaceError,
55+
ViewUpdateError,
4956
)
5057
from arangoasync.executor import (
5158
ApiExecutor,
@@ -1223,6 +1230,237 @@ def response_handler(resp: Response) -> bool:
12231230

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

1233+
async def view(self, name: str) -> Result[Json]:
1234+
"""Return the properties of a view.
1235+
1236+
Args:
1237+
name (str): View name.
1238+
1239+
Returns:
1240+
dict: View properties.
1241+
1242+
Raises:
1243+
ViewGetError: If the operation fails.
1244+
1245+
References:
1246+
- `read-properties-of-a-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#read-properties-of-a-view>`__
1247+
- `get-the-properties-of-a-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#get-the-properties-of-a-view>`__
1248+
""" # noqa: E501
1249+
request = Request(method=Method.GET, endpoint=f"/_api/view/{name}/properties")
1250+
1251+
def response_handler(resp: Response) -> Json:
1252+
if not resp.is_success:
1253+
raise ViewGetError(resp, request)
1254+
return self.deserializer.loads(resp.raw_body)
1255+
1256+
return await self._executor.execute(request, response_handler)
1257+
1258+
async def view_info(self, name: str) -> Result[Json]:
1259+
"""Return basic information about a specific view.
1260+
1261+
Args:
1262+
name (str): View name.
1263+
1264+
Returns:
1265+
dict: View information.
1266+
1267+
Raises:
1268+
ViewGetError: If the operation fails.
1269+
1270+
References:
1271+
- `get-information-about-a-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#get-information-about-a-view>`_
1272+
- `get-information-about-a-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#get-information-about-a-view>`__
1273+
""" # noqa: E501
1274+
request = Request(method=Method.GET, endpoint=f"/_api/view/{name}")
1275+
1276+
def response_handler(resp: Response) -> Json:
1277+
if not resp.is_success:
1278+
raise ViewGetError(resp, request)
1279+
return self.deserializer.loads(resp.raw_body)
1280+
1281+
return await self._executor.execute(request, response_handler)
1282+
1283+
async def views(self) -> Result[Jsons]:
1284+
"""List all views in the database along with their summary information.
1285+
1286+
Returns:
1287+
list: List of views with their properties.
1288+
1289+
Raises:
1290+
ViewListError: If the operation fails.
1291+
1292+
References:
1293+
- `list-all-views <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#list-all-views>`__
1294+
- `list-all-views <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#list-all-views>`__
1295+
""" # noqa: E501
1296+
request = Request(method=Method.GET, endpoint="/_api/view")
1297+
1298+
def response_handler(resp: Response) -> Jsons:
1299+
if not resp.is_success:
1300+
raise ViewListError(resp, request)
1301+
body = self.deserializer.loads(resp.raw_body)
1302+
return cast(Jsons, body["result"])
1303+
1304+
return await self._executor.execute(request, response_handler)
1305+
1306+
async def create_view(
1307+
self,
1308+
name: str,
1309+
view_type: str,
1310+
properties: Optional[Json] = None,
1311+
) -> Result[Json]:
1312+
"""Create a view.
1313+
1314+
Args:
1315+
name (str): View name.
1316+
view_type (str): Type of the view (e.g., "arangosearch", "view").
1317+
properties (dict | None): Properties of the view.
1318+
1319+
Returns:
1320+
dict: View properties.
1321+
1322+
Raises:
1323+
ViewCreateError: If the operation fails.
1324+
1325+
References:
1326+
- `create-a-search-alias-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#create-a-search-alias-view>`__
1327+
- `create-an-arangosearch-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#create-an-arangosearch-view>`__
1328+
""" # noqa: E501
1329+
data: Json = {"name": name, "type": view_type}
1330+
if properties is not None:
1331+
data.update(properties)
1332+
1333+
request = Request(
1334+
method=Method.POST,
1335+
endpoint="/_api/view",
1336+
data=self.serializer.dumps(data),
1337+
)
1338+
1339+
def response_handler(resp: Response) -> Json:
1340+
if not resp.is_success:
1341+
raise ViewCreateError(resp, request)
1342+
return self.deserializer.loads(resp.raw_body)
1343+
1344+
return await self._executor.execute(request, response_handler)
1345+
1346+
async def replace_view(self, name: str, properties: Json) -> Result[Json]:
1347+
"""Replace the properties of an existing view.
1348+
1349+
Args:
1350+
name (str): View name.
1351+
properties (dict): New properties for the view.
1352+
1353+
Returns:
1354+
dict: Updated view properties.
1355+
1356+
Raises:
1357+
ViewReplaceError: If the operation fails.
1358+
1359+
References:
1360+
- `replace-the-properties-of-a-search-alias-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#replace-the-properties-of-a-search-alias-view>`__
1361+
- `replace-the-properties-of-an-arangosearch-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#replace-the-properties-of-an-arangosearch-view>`__
1362+
""" # noqa: E501
1363+
request = Request(
1364+
method=Method.PUT,
1365+
endpoint=f"/_api/view/{name}/properties",
1366+
data=self.serializer.dumps(properties),
1367+
)
1368+
1369+
def response_handler(resp: Response) -> Json:
1370+
if resp.is_success:
1371+
return self.deserializer.loads(resp.raw_body)
1372+
raise ViewReplaceError(resp, request)
1373+
1374+
return await self._executor.execute(request, response_handler)
1375+
1376+
async def update_view(self, name: str, properties: Json) -> Result[Json]:
1377+
"""Update the properties of an existing view.
1378+
1379+
Args:
1380+
name (str): View name.
1381+
properties (dict): New properties for the view.
1382+
1383+
Returns:
1384+
dict: Updated view properties.
1385+
1386+
Raises:
1387+
ViewUpdateError: If the operation fails.
1388+
1389+
References:
1390+
- `update-the-properties-of-a-search-alias-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#update-the-properties-of-a-search-alias-view>`__
1391+
- `update-the-properties-of-an-arangosearch-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#update-the-properties-of-an-arangosearch-view>`__
1392+
""" # noqa: E501
1393+
request = Request(
1394+
method=Method.PATCH,
1395+
endpoint=f"/_api/view/{name}/properties",
1396+
data=self.serializer.dumps(properties),
1397+
)
1398+
1399+
def response_handler(resp: Response) -> Json:
1400+
if resp.is_success:
1401+
return self.deserializer.loads(resp.raw_body)
1402+
raise ViewUpdateError(resp, request)
1403+
1404+
return await self._executor.execute(request, response_handler)
1405+
1406+
async def rename_view(self, name: str, new_name: str) -> None:
1407+
"""Rename an existing view (not supported in cluster deployments).
1408+
1409+
Args:
1410+
name (str): Current view name.
1411+
new_name (str): New view name.
1412+
1413+
Raises:
1414+
ViewRenameError: If the operation fails.
1415+
1416+
References:
1417+
- `rename-a-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#rename-a-view>`__
1418+
- `rename-a-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#rename-a-view>`__
1419+
""" # noqa: E501
1420+
request = Request(
1421+
method=Method.PUT,
1422+
endpoint=f"/_api/view/{name}/rename",
1423+
data=self.serializer.dumps({"name": new_name}),
1424+
)
1425+
1426+
def response_handler(resp: Response) -> None:
1427+
if not resp.is_success:
1428+
raise ViewRenameError(resp, request)
1429+
1430+
await self._executor.execute(request, response_handler)
1431+
1432+
async def delete_view(
1433+
self, name: str, ignore_missing: bool = False
1434+
) -> Result[bool]:
1435+
"""Delete a view.
1436+
1437+
Args:
1438+
name (str): View name.
1439+
ignore_missing (bool): If `True`, do not raise an exception if the
1440+
view does not exist.
1441+
1442+
Returns:
1443+
bool: `True` if the view was deleted successfully, `False` if the
1444+
view was not found and **ignore_missing** was set to `True`.
1445+
1446+
Raises:
1447+
ViewDeleteError: If the operation fails.
1448+
1449+
References:
1450+
- `drop-a-view <https://docs.arangodb.com/stable/develop/http-api/views/search-alias-views/#drop-a-view>`__
1451+
- `drop-a-view <https://docs.arangodb.com/stable/develop/http-api/views/arangosearch-views/#drop-a-view>`__
1452+
""" # noqa: E501
1453+
request = Request(method=Method.DELETE, endpoint=f"/_api/view/{name}")
1454+
1455+
def response_handler(resp: Response) -> bool:
1456+
if resp.is_success:
1457+
return True
1458+
if resp.status_code == HTTP_NOT_FOUND and ignore_missing:
1459+
return False
1460+
raise ViewDeleteError(resp, request)
1461+
1462+
return await self._executor.execute(request, response_handler)
1463+
12261464
async def has_user(self, username: str) -> Result[bool]:
12271465
"""Check if a user exists.
12281466

arangoasync/exceptions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,31 @@ class VertexCollectionDeleteError(ArangoServerError):
429429

430430
class VertexCollectionListError(ArangoServerError):
431431
"""Failed to retrieve vertex collections."""
432+
433+
434+
class ViewCreateError(ArangoServerError):
435+
"""Failed to create view."""
436+
437+
438+
class ViewDeleteError(ArangoServerError):
439+
"""Failed to delete view."""
440+
441+
442+
class ViewGetError(ArangoServerError):
443+
"""Failed to retrieve view details."""
444+
445+
446+
class ViewListError(ArangoServerError):
447+
"""Failed to retrieve views."""
448+
449+
450+
class ViewRenameError(ArangoServerError):
451+
"""Failed to rename view."""
452+
453+
454+
class ViewReplaceError(ArangoServerError):
455+
"""Failed to replace view."""
456+
457+
458+
class ViewUpdateError(ArangoServerError):
459+
"""Failed to update view."""

tests/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ def generate_string():
4444
str: Random unique string.
4545
"""
4646
return uuid4().hex
47+
48+
49+
def generate_view_name():
50+
"""Generate and return a random view name.
51+
52+
Returns:
53+
str: Random view name.
54+
"""
55+
return f"test_view_{uuid4().hex}"

0 commit comments

Comments
 (0)