4
4
]
5
5
6
6
7
- from typing import Optional , Sequence , TypeVar , cast
7
+ from typing import List , Optional , Sequence , TypeVar , cast
8
8
9
- from arangoasync .collection import CollectionType , StandardCollection
9
+ from arangoasync .collection import StandardCollection
10
10
from arangoasync .connection import Connection
11
11
from arangoasync .errno import HTTP_FORBIDDEN , HTTP_NOT_FOUND
12
12
from arangoasync .exceptions import (
22
22
from arangoasync .request import Method , Request
23
23
from arangoasync .response import Response
24
24
from arangoasync .serialization import Deserializer , Serializer
25
- from arangoasync .typings import Json , Jsons , Params , Result
26
- from arangoasync .wrapper import KeyOptions , ServerStatusInformation , User
25
+ from arangoasync .typings import (
26
+ CollectionInfo ,
27
+ CollectionType ,
28
+ Json ,
29
+ Jsons ,
30
+ KeyOptions ,
31
+ Params ,
32
+ Result ,
33
+ ServerStatusInformation ,
34
+ UserInfo ,
35
+ )
27
36
28
37
T = TypeVar ("T" )
29
38
U = TypeVar ("U" )
@@ -69,7 +78,10 @@ async def status(self) -> Result[ServerStatusInformation]:
69
78
70
79
Raises:
71
80
ServerSatusError: If retrieval fails.
72
- """
81
+
82
+ References:
83
+ - `get-server-status-information <https://docs.arangodb.com/stable/develop/http-api/administration/#get-server-status-information>`__
84
+ """ # noqa: E501
73
85
request = Request (method = Method .GET , endpoint = "/_admin/status" )
74
86
75
87
def response_handler (resp : Response ) -> ServerStatusInformation :
@@ -79,43 +91,80 @@ def response_handler(resp: Response) -> ServerStatusInformation:
79
91
80
92
return await self ._executor .execute (request , response_handler )
81
93
94
+ async def databases (self ) -> Result [List [str ]]:
95
+ """Return the names of all databases.
96
+
97
+ Note:
98
+ This method can only be executed in the **_system** database.
99
+
100
+ Returns:
101
+ list: Database names.
102
+
103
+ Raises:
104
+ DatabaseListError: If retrieval fails.
105
+
106
+ References:
107
+ - `list-all-databases <https://docs.arangodb.com/stable/develop/http-api/databases/#list-all-databases>`__
108
+ """ # noqa: E501
109
+ request = Request (method = Method .GET , endpoint = "/_api/database" )
110
+
111
+ def response_handler (resp : Response ) -> List [str ]:
112
+ if resp .is_success :
113
+ body = self .deserializer .loads (resp .raw_body )
114
+ return cast (List [str ], body ["result" ])
115
+ msg : Optional [str ] = None
116
+ if resp .status_code == HTTP_FORBIDDEN :
117
+ msg = "This request can only be executed in the _system database."
118
+ raise DatabaseListError (resp , request , msg )
119
+
120
+ return await self ._executor .execute (request , response_handler )
121
+
82
122
async def has_database (self , name : str ) -> Result [bool ]:
83
123
"""Check if a database exists.
84
124
125
+ Note:
126
+ This method can only be executed from within the **_system** database.
127
+
85
128
Args:
86
129
name (str): Database name.
87
130
88
131
Returns:
89
132
bool: `True` if the database exists, `False` otherwise.
90
133
91
134
Raises:
92
- DatabaseListError: If failed to retrieve the list of databases .
135
+ DatabaseListError: If retrieval fails .
93
136
"""
94
137
request = Request (method = Method .GET , endpoint = "/_api/database" )
95
138
96
139
def response_handler (resp : Response ) -> bool :
97
- if not resp .is_success :
98
- raise DatabaseListError (resp , request )
99
- body = self .deserializer .loads (resp .raw_body )
100
- return name in body ["result" ]
140
+ if resp .is_success :
141
+ body = self .deserializer .loads (resp .raw_body )
142
+ return name in body ["result" ]
143
+ msg : Optional [str ] = None
144
+ if resp .status_code == HTTP_FORBIDDEN :
145
+ msg = "This request can only be executed in the _system database."
146
+ raise DatabaseListError (resp , request , msg )
101
147
102
148
return await self ._executor .execute (request , response_handler )
103
149
104
150
async def create_database (
105
151
self ,
106
152
name : str ,
107
- users : Optional [Sequence [Json | User ]] = None ,
153
+ users : Optional [Sequence [Json | UserInfo ]] = None ,
108
154
replication_factor : Optional [int | str ] = None ,
109
155
write_concern : Optional [int ] = None ,
110
156
sharding : Optional [bool ] = None ,
111
157
) -> Result [bool ]:
112
158
"""Create a new database.
113
159
160
+ Note:
161
+ This method can only be executed from within the **_system** database.
162
+
114
163
Args:
115
164
name (str): Database name.
116
165
users (list | None): Optional list of users with access to the new
117
166
database, where each user is of :class:`User
118
- <arangoasync.wrapper.User >` type, or a dictionary with fields
167
+ <arangoasync.wrapper.UserInfo >` type, or a dictionary with fields
119
168
"username", "password" and "active". If not set, the default user
120
169
**root** will be used to ensure that the new database will be
121
170
accessible after it is created.
@@ -125,12 +174,12 @@ async def create_database(
125
174
(Enterprise Edition only), and 1, which disables replication. Used
126
175
for clusters only.
127
176
write_concern (int | None): Default write concern for collections created
128
- in this database. Determines how many copies of each shard are required
129
- to be in sync on different DB-Servers. If there are less than these many
130
- copies in the cluster a shard will refuse to write. Writes to shards with
131
- enough up-to-date copies will succeed at the same time, however. Value of
132
- this parameter can not be larger than the value of **replication_factor**.
133
- Used for clusters only.
177
+ in this database. Determines how many copies of each shard are required
178
+ to be in sync on different DB-Servers. If there are less than these many
179
+ copies in the cluster a shard will refuse to write. Writes to shards with
180
+ enough up-to-date copies will succeed at the same time, however. Value of
181
+ this parameter can not be larger than the value of **replication_factor**.
182
+ Used for clusters only.
134
183
sharding (str | None): Sharding method used for new collections in this
135
184
database. Allowed values are: "", "flexible" and "single". The first
136
185
two are equivalent. Used for clusters only.
@@ -140,7 +189,10 @@ async def create_database(
140
189
141
190
Raises:
142
191
DatabaseCreateError: If creation fails.
143
- """
192
+
193
+ References:
194
+ - `create-a-database <https://docs.arangodb.com/stable/develop/http-api/databases/#create-a-database>`__
195
+ """ # noqa: E501
144
196
data : Json = {"name" : name }
145
197
146
198
options : Json = {}
@@ -173,7 +225,10 @@ async def create_database(
173
225
def response_handler (resp : Response ) -> bool :
174
226
if resp .is_success :
175
227
return True
176
- raise DatabaseCreateError (resp , request )
228
+ msg : Optional [str ] = None
229
+ if resp .status_code == HTTP_FORBIDDEN :
230
+ msg = "This request can only be executed in the _system database."
231
+ raise DatabaseCreateError (resp , request , msg )
177
232
178
233
return await self ._executor .execute (request , response_handler )
179
234
@@ -182,6 +237,9 @@ async def delete_database(
182
237
) -> Result [bool ]:
183
238
"""Delete a database.
184
239
240
+ Note:
241
+ This method can only be executed from within the **_system** database.
242
+
185
243
Args:
186
244
name (str): Database name.
187
245
ignore_missing (bool): Do not raise an exception on missing database.
@@ -192,21 +250,21 @@ async def delete_database(
192
250
193
251
Raises:
194
252
DatabaseDeleteError: If deletion fails.
195
- """
253
+
254
+ References:
255
+ - `drop-a-database <https://docs.arangodb.com/stable/develop/http-api/databases/#drop-a-database>`__
256
+ """ # noqa: E501
196
257
request = Request (method = Method .DELETE , endpoint = f"/_api/database/{ name } " )
197
258
198
259
def response_handler (resp : Response ) -> bool :
199
260
if resp .is_success :
200
261
return True
201
262
if resp .status_code == HTTP_NOT_FOUND and ignore_missing :
202
263
return False
264
+ msg : Optional [str ] = None
203
265
if resp .status_code == HTTP_FORBIDDEN :
204
- raise DatabaseDeleteError (
205
- resp ,
206
- request ,
207
- "This request can only be executed in the _system database." ,
208
- )
209
- raise DatabaseDeleteError (resp , request )
266
+ msg = "This request can only be executed in the _system database."
267
+ raise DatabaseDeleteError (resp , request , msg )
210
268
211
269
return await self ._executor .execute (request , response_handler )
212
270
@@ -241,6 +299,40 @@ def collection(
241
299
self ._executor , name , serializer , deserializer
242
300
)
243
301
302
+ async def collections (
303
+ self ,
304
+ exclude_system : Optional [bool ] = None ,
305
+ ) -> Result [List [CollectionInfo ]]:
306
+ """Returns basic information for all collections in the current database,
307
+ optionally excluding system collections.
308
+
309
+ Returns:
310
+ list: Collection names.
311
+
312
+ Raises:
313
+ CollectionListError: If retrieval fails.
314
+
315
+ References:
316
+ - `list-all-collections <https://docs.arangodb.com/stable/develop/http-api/collections/#list-all-collections>`__
317
+ """ # noqa: E501
318
+ params : Params = {}
319
+ if exclude_system is not None :
320
+ params ["excludeSystem" ] = exclude_system
321
+
322
+ request = Request (
323
+ method = Method .GET ,
324
+ endpoint = "/_api/collection" ,
325
+ params = params ,
326
+ )
327
+
328
+ def response_handler (resp : Response ) -> List [CollectionInfo ]:
329
+ if not resp .is_success :
330
+ raise CollectionListError (resp , request )
331
+ body = self .deserializer .loads (resp .raw_body )
332
+ return [CollectionInfo (c ) for c in body ["result" ]]
333
+
334
+ return await self ._executor .execute (request , response_handler )
335
+
244
336
async def has_collection (self , name : str ) -> Result [bool ]:
245
337
"""Check if a collection exists in the database.
246
338
@@ -249,14 +341,18 @@ async def has_collection(self, name: str) -> Result[bool]:
249
341
250
342
Returns:
251
343
bool: True if the collection exists, False otherwise.
344
+
345
+ Raises:
346
+ CollectionListError: If retrieval fails.
252
347
"""
253
- request = Request (method = Method .GET , endpoint = "/_api/collection" )
348
+ request = Request (method = Method .GET , endpoint = f "/_api/collection/ { name } " )
254
349
255
350
def response_handler (resp : Response ) -> bool :
256
- if not resp .is_success :
257
- raise CollectionListError (resp , request )
258
- body = self .deserializer .loads (resp .raw_body )
259
- return any (c ["name" ] == name for c in body ["result" ])
351
+ if resp .is_success :
352
+ return True
353
+ if resp .status_code == HTTP_NOT_FOUND :
354
+ return False
355
+ raise CollectionListError (resp , request )
260
356
261
357
return await self ._executor .execute (request , response_handler )
262
358
@@ -343,7 +439,10 @@ async def create_collection(
343
439
Raises:
344
440
ValueError: If parameters are invalid.
345
441
CollectionCreateError: If the operation fails.
346
- """
442
+
443
+ References:
444
+ - `create-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#create-a-collection>`__
445
+ """ # noqa: E501
347
446
data : Json = {"name" : name }
348
447
if col_type is not None :
349
448
data ["type" ] = col_type .value
@@ -430,7 +529,10 @@ async def delete_collection(
430
529
431
530
Raises:
432
531
CollectionDeleteError: If the operation fails.
433
- """
532
+
533
+ References:
534
+ - `drop-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#drop-a-collection>`__
535
+ """ # noqa: E501
434
536
params : Params = {}
435
537
if is_system is not None :
436
538
params ["isSystem" ] = is_system
0 commit comments