22 "Database" ,
33 "StandardDatabase" ,
44 "TransactionDatabase" ,
5+ "AsyncDatabase" ,
56]
67
78
1314from arangoasync .connection import Connection
1415from arangoasync .errno import HTTP_FORBIDDEN , HTTP_NOT_FOUND
1516from arangoasync .exceptions import (
17+ AsyncJobClearError ,
18+ AsyncJobListError ,
1619 CollectionCreateError ,
1720 CollectionDeleteError ,
1821 CollectionListError ,
4144 UserReplaceError ,
4245 UserUpdateError ,
4346)
44- from arangoasync .executor import ApiExecutor , DefaultApiExecutor , TransactionApiExecutor
47+ from arangoasync .executor import (
48+ ApiExecutor ,
49+ AsyncApiExecutor ,
50+ DefaultApiExecutor ,
51+ TransactionApiExecutor ,
52+ )
4553from arangoasync .request import Method , Request
4654from arangoasync .response import Response
55+ from arangoasync .result import Result
4756from arangoasync .serialization import Deserializer , Serializer
4857from arangoasync .typings import (
4958 CollectionInfo ,
5362 Jsons ,
5463 KeyOptions ,
5564 Params ,
56- Result ,
5765 ServerStatusInformation ,
5866 UserInfo ,
5967)
@@ -1314,7 +1322,7 @@ def response_handler(resp: Response) -> str:
13141322 return cast (str , result ["id" ])
13151323
13161324 transaction_id = await self ._executor .execute (request , response_handler )
1317- return TransactionDatabase (self .connection , transaction_id )
1325+ return TransactionDatabase (self .connection , cast ( str , transaction_id ) )
13181326
13191327 def fetch_transaction (self , transaction_id : str ) -> "TransactionDatabase" :
13201328 """Fetch an existing transaction.
@@ -1328,6 +1336,86 @@ def fetch_transaction(self, transaction_id: str) -> "TransactionDatabase":
13281336 """
13291337 return TransactionDatabase (self .connection , transaction_id )
13301338
1339+ def begin_async_execution (self , return_result : bool = True ) -> "AsyncDatabase" :
1340+ """Begin async execution.
1341+
1342+ Args:
1343+ return_result (bool): If set to `True`, API executions return instances of
1344+ `arangoasync.job.AsyncJob`, which you can be used to retrieve
1345+ results from server once available. Otherwise, API executions
1346+ return `None` and no results are stored on server.
1347+
1348+ Returns:
1349+ AsyncDatabase: Database API wrapper tailored for async execution.
1350+ """
1351+ return AsyncDatabase (self .connection , return_result )
1352+
1353+ async def async_jobs (
1354+ self , status : str , count : Optional [int ] = None
1355+ ) -> Result [List [str ]]:
1356+ """Return IDs of async jobs with given status.
1357+
1358+ Args:
1359+ status (str): Job status (e.g. "pending", "done").
1360+ count (int | None): Max number of job IDs to return.
1361+
1362+ Returns:
1363+ list: List of job IDs.
1364+
1365+ Raises:
1366+ AsyncJobListError: If retrieval fails.
1367+
1368+ References:
1369+ - `list-async-jobs-by-status-or-get-the-status-of-specific-job <https://docs.arangodb.com/stable/develop/http-api/jobs/#list-async-jobs-by-status-or-get-the-status-of-specific-job>`__
1370+ """ # noqa: E501
1371+ params : Params = {}
1372+ if count is not None :
1373+ params ["count" ] = count
1374+
1375+ request = Request (
1376+ method = Method .GET , endpoint = f"/_api/job/{ status } " , params = params
1377+ )
1378+
1379+ def response_handler (resp : Response ) -> List [str ]:
1380+ if resp .is_success :
1381+ return cast (List [str ], self .deserializer .loads (resp .raw_body ))
1382+ raise AsyncJobListError (resp , request )
1383+
1384+ return await self ._executor .execute (request , response_handler )
1385+
1386+ async def clear_async_jobs (self , threshold : Optional [float ] = None ) -> None :
1387+ """Clear async job results from the server.
1388+
1389+ Async jobs that are still queued or running are not stopped.
1390+ Clients can use this method to perform an eventual garbage
1391+ collection of job results.
1392+
1393+ Args:
1394+ threshold (float | None): If specified, only the job results created
1395+ prior to the threshold (a Unix timestamp) are deleted. Otherwise,
1396+ all job results are deleted.
1397+
1398+ Raises:
1399+ AsyncJobClearError: If the operation fails.
1400+
1401+ References:
1402+ - `delete-async-job-results <https://docs.arangodb.com/stable/develop/http-api/jobs/#delete-async-job-results>`__
1403+ """ # noqa: E501
1404+ if threshold is None :
1405+ request = Request (method = Method .DELETE , endpoint = "/_api/job/all" )
1406+ else :
1407+ request = Request (
1408+ method = Method .DELETE ,
1409+ endpoint = "/_api/job/expired" ,
1410+ params = {"stamp" : threshold },
1411+ )
1412+
1413+ def response_handler (resp : Response ) -> None :
1414+ if not resp .is_success :
1415+ raise AsyncJobClearError (resp , request )
1416+
1417+ await self ._executor .execute (request , response_handler )
1418+
13311419
13321420class TransactionDatabase (Database ):
13331421 """Database API tailored specifically for
@@ -1420,3 +1508,26 @@ def response_handler(resp: Response) -> None:
14201508 raise TransactionAbortError (resp , request )
14211509
14221510 await self ._standard_executor .execute (request , response_handler )
1511+
1512+
1513+ class AsyncDatabase (Database ):
1514+ """Database API wrapper tailored specifically for async execution.
1515+
1516+ See :func:`arangoasync.database.StandardDatabase.begin_async_execution`.
1517+
1518+ Args:
1519+ connection (Connection): HTTP connection.
1520+ return_result (bool): If set to `True`, API executions return instances of
1521+ :class:`arangoasync.job.AsyncJob`, which you can use to retrieve results
1522+ from server once available. If set to `False`, API executions return `None`
1523+ and no results are stored on server.
1524+
1525+ References:
1526+ - `jobs <https://docs.arangodb.com/stable/develop/http-api/jobs/>`__
1527+ """ # noqa: E501
1528+
1529+ def __init__ (self , connection : Connection , return_result : bool ) -> None :
1530+ super ().__init__ (executor = AsyncApiExecutor (connection , return_result ))
1531+
1532+ def __repr__ (self ) -> str :
1533+ return f"<AsyncDatabase { self .name } >"
0 commit comments