55]
66
77
8- from typing import List , Optional , Sequence , TypeVar , cast
8+ from typing import Any , List , Optional , Sequence , TypeVar , cast
9+ from warnings import warn
910
1011from arangoasync .collection import StandardCollection
1112from arangoasync .connection import Connection
2728 ServerStatusError ,
2829 TransactionAbortError ,
2930 TransactionCommitError ,
31+ TransactionExecuteError ,
3032 TransactionInitError ,
3133 TransactionListError ,
3234 TransactionStatusError ,
@@ -1079,6 +1081,105 @@ def response_handler(resp: Response) -> Json:
10791081
10801082 return await self ._executor .execute (request , response_handler )
10811083
1084+ async def list_transactions (self ) -> Result [Jsons ]:
1085+ """List all currently running stream transactions.
1086+
1087+ Returns:
1088+ list: List of transactions, with each transaction containing
1089+ an "id" and a "state" field.
1090+
1091+ Raises:
1092+ TransactionListError: If the operation fails on the server side.
1093+ """
1094+ request = Request (method = Method .GET , endpoint = "/_api/transaction" )
1095+
1096+ def response_handler (resp : Response ) -> Jsons :
1097+ if not resp .is_success :
1098+ raise TransactionListError (resp , request )
1099+ result : Json = self .deserializer .loads (resp .raw_body )
1100+ return cast (Jsons , result ["transactions" ])
1101+
1102+ return await self ._executor .execute (request , response_handler )
1103+
1104+ async def execute_transaction (
1105+ self ,
1106+ command : str ,
1107+ params : Optional [Json ] = None ,
1108+ read : Optional [str | Sequence [str ]] = None ,
1109+ write : Optional [str | Sequence [str ]] = None ,
1110+ exclusive : Optional [str | Sequence [str ]] = None ,
1111+ allow_implicit : Optional [bool ] = None ,
1112+ wait_for_sync : Optional [bool ] = None ,
1113+ lock_timeout : Optional [int ] = None ,
1114+ max_transaction_size : Optional [int ] = None ,
1115+ ) -> Result [Any ]:
1116+ """Execute a JavaScript Transaction.
1117+
1118+ Warning:
1119+ JavaScript Transactions are deprecated from ArangoDB v3.12.0 onward and
1120+ will be removed in a future version.
1121+
1122+ Args:
1123+ command (str): The actual transaction operations to be executed, in the
1124+ form of stringified JavaScript code.
1125+ params (dict): Optional parameters passed into the JavaScript command.
1126+ read (str | list | None): Name(s) of collections read during transaction.
1127+ write (str | list | None): Name(s) of collections written to during
1128+ transaction with shared access.
1129+ exclusive (str | list | None): Name(s) of collections written to during
1130+ transaction with exclusive access.
1131+ allow_implicit (bool | None): Allow reading from undeclared collections.
1132+ wait_for_sync (bool | None): If `True`, will force the transaction to write
1133+ all data to disk before returning.
1134+ lock_timeout (int | None): Timeout for waiting on collection locks. Setting
1135+ it to 0 will prevent ArangoDB from timing out while waiting for a lock.
1136+ max_transaction_size (int | None): Transaction size limit in bytes.
1137+
1138+ Returns:
1139+ Any: Result of the transaction.
1140+
1141+ Raises:
1142+ TransactionExecuteError: If the operation fails on the server side.
1143+
1144+ References:
1145+ - `execute-a-javascript-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/javascript-transactions/#execute-a-javascript-transaction>`__
1146+ """ # noqa: 501
1147+ m = "JavaScript Transactions are deprecated from ArangoDB v3.12.0 onward and will be removed in a future version." # noqa: E501
1148+ warn (m , DeprecationWarning , stacklevel = 2 )
1149+
1150+ collections = dict ()
1151+ if read is not None :
1152+ collections ["read" ] = read
1153+ if write is not None :
1154+ collections ["write" ] = write
1155+ if exclusive is not None :
1156+ collections ["exclusive" ] = exclusive
1157+
1158+ data : Json = dict (collections = collections , action = command )
1159+ if params is not None :
1160+ data ["params" ] = params
1161+ if wait_for_sync is not None :
1162+ data ["waitForSync" ] = wait_for_sync
1163+ if allow_implicit is not None :
1164+ data ["allowImplicit" ] = allow_implicit
1165+ if lock_timeout is not None :
1166+ data ["lockTimeout" ] = lock_timeout
1167+ if max_transaction_size is not None :
1168+ data ["maxTransactionSize" ] = max_transaction_size
1169+
1170+ request = Request (
1171+ method = Method .POST ,
1172+ endpoint = "/_api/transaction" ,
1173+ data = self .serializer .dumps (data ),
1174+ )
1175+
1176+ def response_handler (resp : Response ) -> Any :
1177+ if not resp .is_success :
1178+ raise TransactionExecuteError (resp , request )
1179+ return self .deserializer .loads (resp .raw_body )["result" ]
1180+
1181+ return await self ._executor .execute (request , response_handler )
1182+
10821183
10831184class StandardDatabase (Database ):
10841185 """Standard database API wrapper.
@@ -1119,7 +1220,7 @@ async def begin_transaction(
11191220 all data to disk before returning
11201221 allow_implicit (bool | None): Allow reading from undeclared collections.
11211222 lock_timeout (int | None): Timeout for waiting on collection locks. Setting
1122- it to 0 will make ArangoDB not time out waiting for a lock.
1223+ it to 0 will prevent ArangoDB from timing out while waiting for a lock.
11231224 max_transaction_size (int | None): Transaction size limit in bytes.
11241225 allow_dirty_read (bool | None): If `True`, allows the Coordinator to ask any
11251226 shard replica for the data, not only the shard leader. This may result
@@ -1135,7 +1236,10 @@ async def begin_transaction(
11351236
11361237 Raises:
11371238 TransactionInitError: If the operation fails on the server side.
1138- """
1239+
1240+ References:
1241+ - `begin-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#begin-a-stream-transaction>`__
1242+ """ # noqa: E501
11391243 collections = dict ()
11401244 if read is not None :
11411245 collections ["read" ] = read
@@ -1188,26 +1292,6 @@ def fetch_transaction(self, transaction_id: str) -> "TransactionDatabase":
11881292 """
11891293 return TransactionDatabase (self .connection , transaction_id )
11901294
1191- async def list_transactions (self ) -> Result [Jsons ]:
1192- """List all currently running stream transactions.
1193-
1194- Returns:
1195- list: List of transactions, with each transaction containing
1196- an "id" and a "state" field.
1197-
1198- Raises:
1199- TransactionListError: If the operation fails on the server side.
1200- """
1201- request = Request (method = Method .GET , endpoint = "/_api/transaction" )
1202-
1203- def response_handler (resp : Response ) -> Jsons :
1204- if not resp .is_success :
1205- raise TransactionListError (resp , request )
1206- result : Json = self .deserializer .loads (resp .raw_body )
1207- return cast (Jsons , result ["transactions" ])
1208-
1209- return await self ._executor .execute (request , response_handler )
1210-
12111295
12121296class TransactionDatabase (Database ):
12131297 """Database API tailored specifically for
@@ -1244,7 +1328,10 @@ async def transaction_status(self) -> str:
12441328
12451329 Raises:
12461330 TransactionStatusError: If the transaction is not found.
1247- """
1331+
1332+ References:
1333+ - `get-the-status-of-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#get-the-status-of-a-stream-transaction>`__
1334+ """ # noqa: E501
12481335 request = Request (
12491336 method = Method .GET ,
12501337 endpoint = f"/_api/transaction/{ self .transaction_id } " ,
@@ -1263,7 +1350,10 @@ async def commit_transaction(self) -> None:
12631350
12641351 Raises:
12651352 TransactionCommitError: If the operation fails on the server side.
1266- """
1353+
1354+ References:
1355+ - `commit-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#commit-a-stream-transaction>`__
1356+ """ # noqa: E501
12671357 request = Request (
12681358 method = Method .PUT ,
12691359 endpoint = f"/_api/transaction/{ self .transaction_id } " ,
@@ -1276,7 +1366,14 @@ def response_handler(resp: Response) -> None:
12761366 await self ._executor .execute (request , response_handler )
12771367
12781368 async def abort_transaction (self ) -> None :
1279- """Abort the transaction."""
1369+ """Abort the transaction.
1370+
1371+ Raises:
1372+ TransactionAbortError: If the operation fails on the server side.
1373+
1374+ References:
1375+ - `abort-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#abort-a-stream-transaction>`__
1376+ """ # noqa: E501
12801377 request = Request (
12811378 method = Method .DELETE ,
12821379 endpoint = f"/_api/transaction/{ self .transaction_id } " ,
0 commit comments