8000 Inserting and retrieving edges · arangodb/python-arango-async@d1fabd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1fabd4

Browse files
committed
Inserting and retrieving edges
1 parent d3b45af commit d1fabd4

File tree

3 files changed

+349
-37
lines changed

3 files changed

+349
-37
lines changed

arangoasync/collection.py

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ async def update(
14651465
version_attribute: Optional[str] = None,
14661466
if_match: Optional[str] = None,
14671467
) -> Result[bool | Json]:
1468-
"""Insert a new document.
1468+
"""Update a document.
14691469
14701470
Args:
14711471
document (dict): Partial or full document with the updated values.
@@ -1791,7 +1791,7 @@ async def get(
17911791
if_match: Optional[str] = None,
17921792
if_none_match: Optional[str] = None,
17931793
) -> Result[Optional[Json]]:
1794-
"""Return a document.
1794+
"""Return a vertex from the graph.
17951795
17961796
Args:
17971797
vertex (str | dict): Document ID, key or body.
@@ -1914,7 +1914,7 @@ async def update(
19141914
return_old: Optional[bool] = None,
19151915
if_match: Optional[str] = None,
19161916
) -> Result[Json]:
1917-
"""Insert a new document.
1917+
"""Update a vertex in the graph.
19181918
19191919
Args:
19201920
vertex (dict): Partial or full document with the updated values.
@@ -1989,7 +1989,7 @@ async def replace(
19891989
return_old: Optional[bool] = None,
19901990
if_match: Optional[str] = None,
19911991
) -> Result[Json]:
1992-
"""Replace a document.
1992+
"""Replace a vertex in the graph.
19931993
19941994
Args:
19951995
vertex (dict): New document. It must contain the "_key" or "_id" field.
@@ -2063,7 +2063,7 @@ async def delete(
20632063
return_old: Optional[bool] = None,
20642064
if_match: Optional[str] = None,
20652065
) -> Result[bool | Json]:
2066-
"""Delete a document.
2066+
"""Delete a vertex from the graph.
20672067
20682068
Args:
20692069
vertex (dict): Document ID, key or body. The body must contain the
@@ -2077,9 +2077,9 @@ async def delete(
20772077
20782078
Returns:
20792079
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`.
20832083
20842084
Raises:
20852085
DocumentRevisionError: If precondition was violated.
@@ -2153,6 +2153,27 @@ def __init__(
21532153
def __repr__(self) -> str:
21542154
return f"<EdgeCollection {self.name}>"
21552155

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+
21562177
@property
21572178
def graph(self) -> str:
21582179
"""Return the graph name.
@@ -2161,3 +2182,128 @@ def graph(self) -> str:
21612182
str: Graph name.
21622183
"""
21632184
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

Comments
 (0)
0