2
2
"Database" ,
3
3
"StandardDatabase" ,
4
4
"TransactionDatabase" ,
5
+ "AsyncDatabase" ,
5
6
]
6
7
7
8
13
14
from arangoasync .connection import Connection
14
15
from arangoasync .errno import HTTP_FORBIDDEN , HTTP_NOT_FOUND
15
16
from arangoasync .exceptions import (
17
+ AsyncJobClearError ,
18
+ AsyncJobListError ,
16
19
CollectionCreateError ,
17
20
CollectionDeleteError ,
18
21
CollectionListError ,
41
44
UserReplaceError ,
42
45
UserUpdateError ,
43
46
)
44
- from arangoasync .executor import ApiExecutor , DefaultApiExecutor , TransactionApiExecutor
47
+ from arangoasync .executor import (
48
+ ApiExecutor ,
49
+ AsyncApiExecutor ,
50
+ DefaultApiExecutor ,
51
+ TransactionApiExecutor ,
52
+ )
45
53
from arangoasync .request import Method , Request
46
54
from arangoasync .response import Response
55
+ from arangoasync .result import Result
47
56
from arangoasync .serialization import Deserializer , Serializer
48
57
from arangoasync .typings import (
49
58
CollectionInfo ,
53
62
Jsons ,
54
63
KeyOptions ,
55
64
Params ,
56
- Result ,
57
65
ServerStatusInformation ,
58
66
UserInfo ,
59
67
)
@@ -1314,7 +1322,7 @@ def response_handler(resp: Response) -> str:
1314
1322
return cast (str , result ["id" ])
1315
1323
1316
1324
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 ) )
1318
1326
1319
1327
def fetch_transaction (self , transaction_id : str ) -> "TransactionDatabase" :
1320
1328
"""Fetch an existing transaction.
@@ -1328,6 +1336,86 @@ def fetch_transaction(self, transaction_id: str) -> "TransactionDatabase":
1328
1336
"""
1329
1337
return TransactionDatabase (self .connection , transaction_id )
1330
1338
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
+
1331
1419
1332
1420
class TransactionDatabase (Database ):
1333
1421
"""Database API tailored specifically for
@@ -1420,3 +1508,26 @@ def response_handler(resp: Response) -> None:
1420
1508
raise TransactionAbortError (resp , request )
1421
1509
1422
1510
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