1313from arangoasync .connection import Connection
1414from arangoasync .errno import HTTP_FORBIDDEN , HTTP_NOT_FOUND
1515from arangoasync .exceptions import (
16+ AsyncJobClearError ,
17+ AsyncJobListError ,
1618 CollectionCreateError ,
1719 CollectionDeleteError ,
1820 CollectionListError ,
4143 UserReplaceError ,
4244 UserUpdateError ,
4345)
44- from arangoasync .executor import ApiExecutor , DefaultApiExecutor , TransactionApiExecutor
46+ from arangoasync .executor import (
47+ ApiExecutor ,
48+ AsyncApiExecutor ,
49+ DefaultApiExecutor ,
50+ TransactionApiExecutor ,
51+ )
4552from arangoasync .request import Method , Request
4653from arangoasync .response import Response
54+ from arangoasync .result import Result
4755from arangoasync .serialization import Deserializer , Serializer
4856from arangoasync .typings import (
4957 CollectionInfo ,
5361 Jsons ,
5462 KeyOptions ,
5563 Params ,
56- Result ,
5764 ServerStatusInformation ,
5865 UserInfo ,
5966)
@@ -1314,7 +1321,7 @@ def response_handler(resp: Response) -> str:
13141321 return cast (str , result ["id" ])
13151322
13161323 transaction_id = await self ._executor .execute (request , response_handler )
1317- return TransactionDatabase (self .connection , transaction_id )
1324+ return TransactionDatabase (self .connection , cast ( str , transaction_id ) )
13181325
13191326 def fetch_transaction (self , transaction_id : str ) -> "TransactionDatabase" :
13201327 """Fetch an existing transaction.
@@ -1328,6 +1335,86 @@ def fetch_transaction(self, transaction_id: str) -> "TransactionDatabase":
13281335 """
13291336 return TransactionDatabase (self .connection , transaction_id )
13301337
1338+ def begin_async_execution (self , return_result : bool = True ) -> "AsyncDatabase" :
1339+ """Begin async execution.
1340+
1341+ Args:
1342+ return_result (bool): If set to `True`, API executions return instances of
1343+ `arangoasync.job.AsyncJob`, which you can be used to retrieve
1344+ results from server once available. Otherwise, API executions
1345+ return `None` and no results are stored on server.
1346+
1347+ Returns:
1348+ AsyncDatabase: Database API wrapper tailored for async execution.
1349+ """
1350+ return AsyncDatabase (self .connection , return_result )
1351+
1352+ async def async_jobs (
1353+ self , status : str , count : Optional [int ] = None
1354+ ) -> Result [List [str ]]:
1355+ """Return IDs of async jobs with given status.
1356+
1357+ Args:
1358+ status (str): Job status (e.g. "pending", "done").
1359+ count (int | None): Max number of job IDs to return.
1360+
1361+ Returns:
1362+ list: List of job IDs.
1363+
1364+ Raises:
1365+ AsyncJobListError: If retrieval fails.
1366+
1367+ References:
1368+ - `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>`__
1369+ """ # noqa: E501
1370+ params : Params = {}
1371+ if count is not None :
1372+ params ["count" ] = count
1373+
1374+ request = Request (
1375+ method = Method .GET , endpoint = f"/_api/job/{ status } " , params = params
1376+ )
1377+
1378+ def response_handler (resp : Response ) -> List [str ]:
1379+ if resp .is_success :
1380+ return cast (List [str ], self .deserializer .loads (resp .raw_body ))
1381+ raise AsyncJobListError (resp , request )
1382+
1383+ return await self ._executor .execute (request , response_handler )
1384+
1385+ async def clear_async_jobs (self , threshold : Optional [float ] = None ) -> None :
1386+ """Clear async job results from the server.
1387+
1388+ Async jobs that are still queued or running are not stopped.
1389+ Clients can use this method to perform an eventual garbage
1390+ collection of job results.
1391+
1392+ Args:
1393+ threshold (float | None): If specified, only the job results created
1394+ prior to the threshold (a Unix timestamp) are deleted. Otherwise,
1395+ all job results are deleted.
1396+
1397+ Raises:
1398+ AsyncJobClearError: If the operation fails.
1399+
1400+ References:
1401+ - `delete-async-job-results <https://docs.arangodb.com/stable/develop/http-api/jobs/#delete-async-job-results>`__
1402+ """ # noqa: E501
1403+ if threshold is None :
1404+ request = Request (method = Method .DELETE , endpoint = "/_api/job/all" )
1405+ else :
1406+ request = Request (
1407+ method = Method .DELETE ,
1408+ endpoint = "/_api/job/expired" ,
1409+ params = {"stamp" : threshold },
1410+ )
1411+
1412+ def response_handler (resp : Response ) -> None :
1413+ if not resp .is_success :
1414+ raise AsyncJobClearError (resp , request )
1415+
1416+ await self ._executor .execute (request , response_handler )
1417+
13311418
13321419class TransactionDatabase (Database ):
13331420 """Database API tailored specifically for
@@ -1420,3 +1507,26 @@ def response_handler(resp: Response) -> None:
14201507 raise TransactionAbortError (resp , request )
14211508
14221509 await self ._standard_executor .execute (request , response_handler )
1510+
1511+
1512+ class AsyncDatabase (Database ):
1513+ """Database API wrapper tailored specifically for async execution.
1514+
1515+ See :func:`arangoasync.database.StandardDatabase.begin_async_execution`.
1516+
1517+ Args:
1518+ connection (Connection): HTTP connection.
1519+ return_result (bool): If set to `True`, API executions return instances of
1520+ :class:`arangoasync.job.AsyncJob`, which you can use to retrieve results
1521+ from server once available. If set to `False`, API executions return `None`
1522+ and no results are stored on server.
1523+
1524+ References:
1525+ - `jobs <https://docs.arangodb.com/stable/develop/http-api/jobs/>`__
1526+ """ # noqa: E501
1527+
1528+ def __init__ (self , connection : Connection , return_result : bool ) -> None :
1529+ super ().__init__ (executor = AsyncApiExecutor (connection , return_result ))
1530+
1531+ def __repr__ (self ) -> str :
1532+ return f"<AsyncDatabase { self .name } >"
0 commit comments