11__all__ = ["Graph" ]
22
33
4- from typing import Generic , TypeVar
4+ from typing import Generic , Optional , Sequence , TypeVar
55
66from arangoasync .collection import EdgeCollection , VertexCollection
7- from arangoasync .exceptions import GraphListError
7+ from arangoasync .exceptions import (
8+ EdgeDefinitionCreateError ,
9+ GraphListError ,
10+ VertexCollectionCreateError ,
11+ )
812from arangoasync .executor import ApiExecutor
913from arangoasync .request import Method , Request
1014from arangoasync .response import Response
1115from arangoasync .result import Result
1216from arangoasync .serialization import Deserializer , Serializer
13- from arangoasync .typings import GraphProperties , Json , Jsons
17+ from arangoasync .typings import (
18+ EdgeDefinitionOptions ,
19+ GraphProperties ,
20+ Json ,
21+ Jsons ,
22+ VertexCollectionOptions ,
23+ )
1424
1525T = TypeVar ("T" ) # Serializer type
1626U = TypeVar ("U" ) # Deserializer loads
@@ -67,7 +77,7 @@ async def properties(self) -> Result[GraphProperties]:
6777 GraphListError: If the operation fails.
6878
6979 References:
70- - `get-a-graph <https://docs.arangodb.com/3.12 /develop/http-api/graphs/named-graphs/#get-a-graph>`__
80+ - `get-a-graph <https://docs.arangodb.com/stable /develop/http-api/graphs/named-graphs/#get-a-graph>`__
7181 """ # noqa: E501
7282 request = Request (method = Method .GET , endpoint = f"/_api/gharial/{ self ._name } " )
7383
@@ -96,6 +106,48 @@ def vertex_collection(self, name: str) -> VertexCollection[T, U, V]:
96106 doc_deserializer = self ._doc_deserializer ,
97107 )
98108
109+ async def create_vertex_collection (
110+ self ,
111+ name : str ,
112+ options : Optional [VertexCollectionOptions | Json ] = None ,
113+ ) -> Result [VertexCollection [T , U , V ]]:
114+ """Create a vertex collection in the graph.
115+
116+ Args:
117+ name (str): Vertex collection name.
118+ options (dict | VertexCollectionOptions | None): Extra options for
119+ creating vertex collections.
120+
121+ Returns:
122+ VertexCollection: Vertex collection API wrapper.
123+
124+ Raises:
125+ VertexCollectionCreateError: If the operation fails.
126+
127+ References:
128+ - `add-a-vertex-collection <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#add-a-vertex-collection>`__
129+ """ # noqa: E501
130+ data : Json = {"collection" : name }
131+
132+ if options is not None :
133+ if isinstance (options , VertexCollectionOptions ):
134+ data ["options" ] = options .to_dict ()
135+ else :
136+ data ["options" ] = options
137+
138+ request = Request (
139+ method = Method .POST ,
140+ endpoint = f"/_api/gharial/{ self ._name } /vertex" ,
141+ data = self .serializer .dumps (data ),
142+ )
143+
144+ def response_handler (resp : Response ) -> VertexCollection [T , U , V ]:
145+ if not resp .is_success :
146+ raise VertexCollectionCreateError (resp , request )
147+ return self .vertex_collection (name )
148+
149+ return await self ._executor .execute (request , response_handler )
150+
99151 def edge_collection (self , name : str ) -> EdgeCollection [T , U , V ]:
100152 """Returns the edge collection API wrapper.
101153
@@ -112,3 +164,66 @@ def edge_collection(self, name: str) -> EdgeCollection[T, U, V]:
112164 doc_serializer = self ._doc_serializer ,
113165 doc_deserializer = self ._doc_deserializer ,
114166 )
167+
168+ async def create_edge_definition (
169+ self ,
170+ edge_collection : str ,
171+ from_vertex_collections : Sequence [str ],
172+ to_vertex_collections : Sequence [str ],
173+ options : Optional [EdgeDefinitionOptions | Json ] = None ,
174+ ) -> Result [EdgeCollection [T , U , V ]]:
175+ """Create an edge definition in the graph.
176+
177+ This edge definition has to contain a collection and an array of each from
178+ and to vertex collections.
179+
180+ .. code-block:: python
181+
182+ {
183+ "edge_collection": "edge_collection_name",
184+ "from_vertex_collections": ["from_vertex_collection_name"],
185+ "to_vertex_collections": ["to_vertex_collection_name"]
186+ }
187+
188+ Args:
189+ edge_collection (str): Edge collection name.
190+ from_vertex_collections (list): List of vertex collections
191+ that can be used as the "from" vertex in edges.
192+ to_vertex_collections (list): List of vertex collections
193+ that can be used as the "to" vertex in edges.
194+ options (dict | EdgeDefinitionOptions | None): Extra options for
195+ creating edge definitions.
196+
197+ Returns:
198+ EdgeCollection: Edge collection API wrapper.
199+
200+ Raises:
201+ EdgeDefinitionCreateError: If the operation fails.
202+
203+ References:
204+ - `add-an-edge-definition <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#add-an-edge-definition>`__
205+ """ # noqa: E501
206+ data : Json = {
207+ "collection" : edge_collection ,
208+ "from" : from_vertex_collections ,
209+ "to" : to_vertex_collections ,
210+ }
211+
212+ if options is not None :
213+ if isinstance (options , VertexCollectionOptions ):
214+ data ["options" ] = options .to_dict ()
215+ else :
216+ data ["options" ] = options
217+
218+ request = Request (
219+ method = Method .POST ,
220+ endpoint = f"/_api/gharial/{ self ._name } /edge" ,
221+ data = self .serializer .dumps (data ),
222+ )
223+
224+ def response_handler (resp : Response ) -> EdgeCollection [T , U , V ]:
225+ if not resp .is_success :
226+ raise EdgeDefinitionCreateError (resp , request )
227+ return self .edge_collection (edge_collection )
228+
229+ return await self ._executor .execute (request , response_handler )
0 commit comments