|
1 | 1 | __all__ = ["Graph"]
|
2 | 2 |
|
3 | 3 |
|
4 |
| -from typing import Generic, List, Optional, Sequence, TypeVar |
| 4 | +from typing import Generic, List, Optional, Sequence, TypeVar, cast |
5 | 5 |
|
6 | 6 | from arangoasync.collection import EdgeCollection, VertexCollection
|
7 | 7 | from arangoasync.exceptions import (
|
| 8 | + EdgeCollectionListError, |
8 | 9 | EdgeDefinitionCreateError,
|
| 10 | + EdgeDefinitionDeleteError, |
| 11 | + EdgeDefinitionListError, |
| 12 | + EdgeDefinitionReplaceError, |
9 | 13 | GraphPropertiesError,
|
10 | 14 | VertexCollectionCreateError,
|
11 | 15 | VertexCollectionDeleteError,
|
|
21 | 25 | GraphProperties,
|
22 | 26 | Json,
|
23 | 27 | Jsons,
|
| 28 | + Params, |
24 | 29 | VertexCollectionOptions,
|
25 | 30 | )
|
26 | 31 |
|
@@ -129,7 +134,7 @@ def response_handler(resp: Response) -> List[str]:
|
129 | 134 | if not resp.is_success:
|
130 | 135 | raise VertexCollectionListError(resp, request)
|
131 | 136 | body = self.deserializer.loads(resp.raw_body)
|
132 |
| - return list(sorted(set(body["collections"]))) |
| 137 | + return list(sorted(body["collections"])) |
133 | 138 |
|
134 | 139 | return await self._executor.execute(request, response_handler)
|
135 | 140 |
|
@@ -245,6 +250,76 @@ def edge_collection(self, name: str) -> EdgeCollection[T, U, V]:
|
245 | 250 | doc_deserializer=self._doc_deserializer,
|
246 | 251 | )
|
247 | 252 |
|
| 253 | + async def edge_definitions(self) -> Result[Jsons]: |
| 254 | + """Return the edge definitions from the graph. |
| 255 | +
|
| 256 | + Returns: |
| 257 | + list: List of edge definitions. |
| 258 | +
|
| 259 | + Raises: |
| 260 | + EdgeDefinitionListError: If the operation fails. |
| 261 | + """ |
| 262 | + request = Request(method=Method.GET, endpoint=f"/_api/gharial/{self._name}") |
| 263 | + |
| 264 | + def response_handler(resp: Response) -> Jsons: |
| 265 | + if not resp.is_success: |
| 266 | + raise EdgeDefinitionListError(resp, request) |
| 267 | + body = self.deserializer.loads(resp.raw_body) |
| 268 | + properties = GraphProperties(body["graph"]) |
| 269 | + edge_definitions = properties.format( |
| 270 | + GraphProperties.compatibility_formatter |
| 271 | + )["edge_definitions"] |
| 272 | + return cast(Jsons, edge_definitions) |
| 273 | + |
| 274 | + return await self._executor.execute(request, response_handler) |
| 275 | + |
| 276 | + async def has_edge_definition(self, name: str) -> Result[bool]: |
| 277 | + """Check if the graph has the given edge definition. |
| 278 | +
|
| 279 | + Returns: |
| 280 | + bool: `True` if the graph has the edge definitions, `False` otherwise. |
| 281 | +
|
| 282 | + Raises: |
| 283 | + EdgeDefinitionListError: If the operation fails. |
| 284 | + """ |
| 285 | + request = Request(method=Method.GET, endpoint=f"/_api/gharial/{self._name}") |
| 286 | + |
| 287 | + def response_handler(resp: Response) -> bool: |
| 288 | + if not resp.is_success: |
| 289 | + raise EdgeDefinitionListError(resp, request) |
| 290 | + body = self.deserializer.loads(resp.raw_body) |
| 291 | + return any( |
| 292 | + edge_definition["collection"] == name |
| 293 | + for edge_definition in body["graph"]["edgeDefinitions"] |
| 294 | + ) |
| 295 | + |
| 296 | + return await self._executor.execute(request, response_handler) |
| 297 | + |
| 298 | + async def edge_collections(self) -> Result[List[str]]: |
| 299 | + """Get the names of all edge collections in the graph. |
| 300 | +
|
| 301 | + Returns: |
| 302 | + list: List of edge collection names. |
| 303 | +
|
| 304 | + Raises: |
| 305 | + EdgeCollectionListError: If the operation fails. |
| 306 | +
|
| 307 | + References: |
| 308 | + - `list-edge-collections <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#list-edge-collections>`__ |
| 309 | + """ # noqa: E501 |
| 310 | + request = Request( |
| 311 | + method=Method.GET, |
| 312 | + endpoint=f"/_api/gharial/{self._name}/edge", |
| 313 | + ) |
| 314 | + |
| 315 | + def response_handler(resp: Response) -> List[str]: |
| 316 | + if not resp.is_success: |
| 317 | + raise EdgeCollectionListError(resp, request) |
| 318 | + body = self.deserializer.loads(resp.raw_body) |
| 319 | + return list(sorted(body["collections"])) |
| 320 | + |
| 321 | + return await self._executor.execute(request, response_handler) |
| 322 | + |
248 | 323 | async def create_edge_definition(
|
249 | 324 | self,
|
250 | 325 | edge_collection: str,
|
@@ -307,3 +382,104 @@ def response_handler(resp: Response) -> EdgeCollection[T, U, V]:
|
307 | 382 | return self.edge_collection(edge_collection)
|
308 | 383 |
|
309 | 384 | return await self._executor.execute(request, response_handler)
|
| 385 | + |
| 386 | + async def replace_edge_definition( |
| 387 | + self, |
| 388 | + edge_collection: str, |
| 389 | + from_vertex_collections: Sequence[str], |
| 390 | + to_vertex_collections: Sequence[str], |
| 391 | + options: Optional[EdgeDefinitionOptions | Json] = None, |
| 392 | + wait_for_sync: Optional[bool] = None, |
| 393 | + drop_collections: Optional[bool] = None, |
| 394 | + ) -> Result[EdgeCollection[T, U, V]]: |
| 395 | + """Replace an edge definition. |
| 396 | +
|
| 397 | + Args: |
| 398 | + edge_collection (str): Edge collection name. |
| 399 | + from_vertex_collections (list): Names of "from" vertex collections. |
| 400 | + to_vertex_collections (list): Names of "to" vertex collections. |
| 401 | + options (dict | EdgeDefinitionOptions | None): Extra options for |
| 402 | + modifying collections withing this edge definition. |
| 403 | + wait_for_sync (bool | None): If set to `True`, the operation waits for |
| 404 | + data to be synced to disk before returning. |
| 405 | + drop_collections (bool | None): Drop the edge collection in addition to |
| 406 | + removing it from the graph. The collection is only dropped if it is |
| 407 | + not used in other graphs. |
| 408 | +
|
| 409 | + Returns: |
| 410 | + EdgeCollection: API wrapper. |
| 411 | +
|
| 412 | + Raises: |
| 413 | + EdgeDefinitionReplaceError: If the operation fails. |
| 414 | +
|
| 415 | + References: |
| 416 | + - `replace-an-edge-definition <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#replace-an-edge-definition>`__ |
| 417 | + """ # noqa: E501 |
| 418 | + data: Json = { |
| 419 | + "collection": edge_collection, |
| 420 | + "from": from_vertex_collections, |
| 421 | + "to": to_vertex_collections, |
| 422 | + } |
| 423 | + if options is not None: |
| 424 | + if isinstance(options, VertexCollectionOptions): |
| 425 | + data["options"] = options.to_dict() |
| 426 | + else: |
| 427 | + data["options"] = options |
| 428 | + |
| 429 | + params: Params = {} |
| 430 | + if wait_for_sync is not None: |
| 431 | + params["waitForSync"] = wait_for_sync |
| 432 | + if drop_collections is not None: |
| 433 | + params["dropCollections"] = drop_collections |
| 434 | + |
| 435 | + request = Request( |
| 436 | + method=Method.PUT, |
| 437 | + endpoint=f"/_api/gharial/{self._name}/edge/{edge_collection}", |
| 438 | + data=self.serializer.dumps(data), |
| 439 | + params=params, |
| 440 | + ) |
| 441 | + |
| 442 | + def response_handler(resp: Response) -> EdgeCollection[T, U, V]: |
| 443 | + if resp.is_success: |
| 444 | + return self.edge_collection(edge_collection) |
| 445 | + raise EdgeDefinitionReplaceError(resp, request) |
| 446 | + |
| 447 | + return await self._executor.execute(request, response_handler) |
| 448 | + |
| 449 | + async def delete_edge_definition( |
| 450 | + self, |
| 451 | + name: str, |
| 452 | + purge: bool = False, |
| 453 | + wait_for_sync: Optional[bool] = None, |
| 454 | + ) -> None: |
| 455 | + """Delete an edge definition from the graph. |
| 456 | +
|
| 457 | + Args: |
| 458 | + name (str): Edge collection name. |
| 459 | + purge (bool): If set to `True`, the edge definition is not just removed |
| 460 | + from the graph but the edge collection is also deleted completely |
| 461 | + from the database. |
| 462 | + wait_for_sync (bool | None): If set to `True`, the operation waits for |
| 463 | + changes to be synced to disk before returning. |
| 464 | +
|
| 465 | + Raises: |
| 466 | + EdgeDefinitionDeleteError: If the operation fails. |
| 467 | +
|
| 468 | + References: |
| 469 | + - `remove-an-edge-definition <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#remove-an-edge-definition>`__ |
| 470 | + """ # noqa: E501 |
| 471 | + params: Params = {"dropCollections": purge} |
| 472 | + if wait_for_sync is not None: |
| 473 | + params["waitForSync"] = wait_for_sync |
| 474 | + |
| 475 | + request = Request( |
| 476 | + method=Method.DELETE, |
| 477 | + <
1241
span class=pl-s1>endpoint=f"/_api/gharial/{self._name}/edge/{name}", |
| 478 | + params=params, |
| 479 | + ) |
| 480 | + |
| 481 | + def response_handler(resp: Response) -> None: |
| 482 | + if not resp.is_success: |
| 483 | + raise EdgeDefinitionDeleteError(resp, request) |
| 484 | + |
| 485 | + await self._executor.execute(request, response_handler) |
0 commit comments