@@ -1465,7 +1465,7 @@ async def update(
1465
1465
version_attribute : Optional [str ] = None ,
1466
1466
if_match : Optional [str ] = None ,
1467
1467
) -> Result [bool | Json ]:
1468
- """Insert a new document.
1468
+ """Update a document.
1469
1469
1470
1470
Args:
1471
1471
document (dict): Partial or full document with the updated values.
@@ -1791,7 +1791,7 @@ async def get(
1791
1791
if_match : Optional [str ] = None ,
1792
1792
if_none_match : Optional [str ] = None ,
1793
1793
) -> Result [Optional [Json ]]:
1794
- """Return a document .
1794
+ """Return a vertex from the graph .
1795
1795
1796
1796
Args:
1797
1797
vertex (str | dict): Document ID, key or body.
@@ -1914,7 +1914,7 @@ async def update(
1914
1914
return_old : Optional [bool ] = None ,
1915
1915
if_match : Optional [str ] = None ,
1916
1916
) -> Result [Json ]:
1917
- """Insert a new document .
1917
+ """Update a vertex in the graph .
1918
1918
1919
1919
Args:
1920
1920
vertex (dict): Partial or full document with the updated values.
@@ -1989,7 +1989,7 @@ async def replace(
1989
1989
return_old : Optional [bool ] = None ,
1990
1990
if_match : Optional [str ] = None ,
1991
1991
) -> Result [Json ]:
1992
- """Replace a document .
1992
+ """Replace a vertex in the graph .
1993
1993
1994
1994
Args:
1995
1995
vertex (dict): New document. It must contain the "_key" or "_id" field.
@@ -2063,7 +2063,7 @@ async def delete(
2063
2063
return_old : Optional [bool ] = None ,
2064
2064
if_match : Optional [str ] = None ,
2065
2065
) -> Result [bool | Json ]:
2066
- """Delete a document .
2066
+ """Delete a vertex from the graph .
2067
2067
2068
2068
Args:
2069
2069
vertex (dict): Document ID, key or body. The body must contain the
@@ -2077,9 +2077,9 @@ async def delete(
2077
2077
2078
2078
Returns:
2079
2079
bool | dict: `True` if vertex was deleted successfully, `False` if vertex
2080
- was not found and **ignore_missing** was set to `True` (does not apply in
2081
- transactions). Old document is returned if **return_old** is set to
2082
- `True`.
2080
+ was not found and **ignore_missing** was set to `True` (does not apply
2081
+ in transactions). Old document is returned if **return_old** is set
2082
+ to `True`.
2083
2083
2084
2084
Raises:
2085
2085
DocumentRevisionError: If precondition was violated.
@@ -2153,6 +2153,27 @@ def __init__(
2153
2153
def __repr__ (self ) -> str :
2154
2154
return f"<EdgeCollection { self .name } >"
2155
2155
2156
+ @staticmethod
2157
+ def _parse_result (data : Json ) -> Json :
2158
+ """Parse the result from the response.
2159
+
2160
+ Args:
2161
+ data (dict): Response data.
2162
+
2163
+ Returns:
2164
+ dict: Parsed result.
2165
+ """
2166
+ result : Json = {}
2167
+ if "new" in data or "old" in data :
2168
+ result ["edge" ] = data ["edge" ]
2169
+ if "new" in data :
2170
+ result ["new" ] = data ["new" ]
2171
+ if "old" in data :
2172
+ result ["old" ] = data ["old" ]
2173
+ else :
2174
+ result = data ["edge" ]
2175
+ return result
2176
+
2156
2177
@property
2157
2178
def graph (self ) -> str :
2158
2179
"""Return the graph name.
@@ -2161,3 +2182,128 @@ def graph(self) -> str:
2161
2182
str: Graph name.
2162
2183
"""
2163
2184
return self ._graph
2185
+
2186
+ async def get (
2187
+ self ,
2188
+ edge : str | Json ,
2189
+ rev : Optional [str ] = None ,
2190
+ if_match : Optional [str ] = None ,
2191
+ if_none_match : Optional [str ] = None ,
2192
+ ) -> Result [Optional [Json ]]:
2193
+ """Return an edge from the graph.
2194
+
2195
+ Args:
2196
+ edge (str | dict): Document ID, key or body.
2197
+ Document body must contain the "_id" or "_key" field.
2198
+ rev (str | None): If this is set a document is only returned if it
2199
+ has exactly this revision.
2200
+ if_match (str | None): The document is returned, if it has the same
2201
+ revision as the given ETag.
2202
+ if_none_match (str | None): The document is returned, if it has a
2203
+ different revision than the given ETag.
2204
F438
+
2205
+ Returns:
2206
+ dict | None: Document or `None` if not found.
2207
+
2208
+ Raises:
2209
+ DocumentRevisionError: If the revision is incorrect.
2210
+ DocumentGetError: If retrieval fails.
2211
+ DocumentParseError: If the document is malformed.
2212
+
2213
+ References:
2214
+ - `get-an-edge <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#get-an-edge>`__
2215
+ """ # noqa: E501
2216
+ handle = self ._prep_from_doc (edge )
2217
+
2218
+ headers : RequestHeaders = {}
2219
+ if if_match is not None :
2220
+ headers ["If-Match" ] = if_match
2221
+ if if_none_match is not None :
2222
+ headers ["If-None-Match" ] = if_none_match
2223
+
2224
+ params : Params = {}
2225
+ if rev is not None :
2226
+ params ["rev" ] = rev
2227
+
2228
+ request = Request (
2229
+ method = Method .GET ,
2230
+ endpoint = f"/_api/gharial/{ self ._graph } /edge/{ handle } " ,
2231
+ headers = headers ,
2232
+ params = params ,
2233
+ )
2234
+
2235
+ def response_handler (resp : Response ) -> Optional [Json ]:
2236
+ if resp .is_success :
2237
+ return self ._parse_result (self .deserializer .loads (resp .raw_body ))
2238
+ elif resp .status_code == HTTP_NOT_FOUND :
2239
+ if resp .error_code == DOCUMENT_NOT_FOUND :
2240
+ return None
2241
+ else :
2242
+ raise DocumentGetError (resp , request )
2243
+ elif resp .status_code == HTTP_PRECONDITION_FAILED :
2244
+ raise DocumentRevisionError (resp , request )
2245
+ else :
2246
+ raise DocumentGetError (resp , request )
2247
+
2248
+ return await self ._executor .execute (request , response_handler )
2249
+
2250
+ async def insert (
2251
+ self ,
2252
+ edge : T ,
2253
+ wait_for_sync : Optional [bool ] = None ,
2254
+ return_new : Optional [bool ] = None ,
2255
+ ) -> Result [Json ]:
2256
+ """Insert a new edge document.
2257
+
2258
+ Args:
2259
+ edge (dict): Document to insert. It must contain "_from" and
2260
+ "_to" fields. If it contains the "_key" or "_id"
2261
+ field, the value is used as the key of the new document (otherwise
2262
+ it is auto-generated). Any "_rev" field is ignored.
2263
+ wait_for_sync (bool | None): Wait until document has been synced to disk.
2264
+ return_new (bool | None): Additionally return the complete new document
2265
+ under the attribute `new` in the result.
2266
+
2267
+ Returns:
2268
+ dict: Document metadata (e.g. document id, key, revision).
2269
+ If `return_new` is specified, the result contains the document
2270
+ metadata in the "edge" field and the new document in the "new" field.
2271
+
2272
+ Raises:
2273
+ DocumentInsertError: If insertion fails.
2274
+ DocumentParseError: If the document is malformed.
2275
+
2276
+ References:
2277
+ - `create-an-edge <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#create-an-edge>`__
2278
+ """ # noqa: E501
2279
+ if isinstance (edge , dict ):
2280
+ edge = cast (T , self ._ensure_key_from_id (edge ))
2281
+
2282
+ params : Params = {}
2283
+ if wait_for_sync is not None :
2284
+ params ["waitForSync" ] = wait_for_sync
2285
+ if return_new is not None :
2286
+ params ["returnNew" ] = return_new
2287
+
2288
+ request = Request (
2289
+ method = Method .POST ,
2290
+ endpoint = f"/_api/gharial/{ self ._graph } /edge/{ self .name } " ,
2291
+ params = params ,
2292
+ data = self ._doc_serializer .dumps (edge ),
2293
+ )
2294
+
2295
+ def response_handler (resp : Response ) -> Json :
2296
+ if resp .is_success :
2297
+ return self ._parse_result (self .deserializer .loads (resp .raw_body ))
2298
+ msg : Optional [str ] = None
2299
+ if resp .status_code == HTTP_NOT_FOUND :
2300
+ msg = (
2301
+ "The graph cannot be found or the edge collection is not "
2302
+ "part of the graph. It is also possible that the vertex "
2303
+ "collection referenced in the _from or _to attribute is not part "
2304
+ "of the graph or the vertex collection is part of the graph, but "
2305
+ "does not exist. Finally check that _from or _to vertex do exist."
2306
+ )
2307
+ raise DocumentInsertError (resp , request , msg )
2308
+
2309
+ return await self ._executor .execute (request , response_handler )
0 commit comments