Skip to content

Commit b570c41

Browse files
committed
Refactor error handling in GraphClient and improve graph retrieval logic
- Replaced generic Exception raises with more specific error types (ValueError, RuntimeError, TimeoutError) for better clarity and debugging. - Updated the get_graph_info method to utilize the get_graphs endpoint and added logic to handle unexpected response formats. - Enhanced graph data extraction to ensure accurate retrieval by graph ID, improving robustness against varying response structures. - Added a NotImplementedError for the delete_graph method to indicate that this feature is not yet available. No breaking changes introduced; existing functionality remains intact.
1 parent 88454e7 commit b570c41

1 file changed

Lines changed: 68 additions & 41 deletions

File tree

‎robosystems_client/extensions/graph_client.py‎

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def create_graph_and_wait(
8585
from ..models.graph_metadata import GraphMetadata as APIGraphMetadata
8686

8787
if not self.token:
88-
raise Exception("No API key provided. Set X-API-Key in headers.")
88+
raise ValueError("No API key provided. Set X-API-Key in headers.")
8989

9090
client = AuthenticatedClient(
9191
base_url=self.base_url,
@@ -130,7 +130,7 @@ def create_graph_and_wait(
130130
response = create_graph(client=client, body=graph_create)
131131

132132
if not response.parsed:
133-
raise Exception(f"Failed to create graph: {response.status_code}")
133+
raise RuntimeError(f"Failed to create graph: {response.status_code}")
134134

135135
# Extract graph_id or operation_id
136136
if isinstance(response.parsed, dict):
@@ -148,7 +148,7 @@ def create_graph_and_wait(
148148

149149
# Otherwise, poll operation until complete
150150
if not operation_id:
151-
raise Exception("No graph_id or operation_id in response")
151+
raise RuntimeError("No graph_id or operation_id in response")
152152

153153
if on_progress:
154154
on_progress(f"Graph creation queued (operation: {operation_id})")
@@ -195,7 +195,7 @@ def create_graph_and_wait(
195195
on_progress(f"Graph created: {graph_id}")
196196
return graph_id
197197
else:
198-
raise Exception("Operation completed but no graph_id in result")
198+
raise RuntimeError("Operation completed but no graph_id in result")
199199

200200
elif status == "failed":
201201
# Extract error message
@@ -208,9 +208,9 @@ def create_graph_and_wait(
208208
error = props.get("error") or props.get("message") or "Unknown error"
209209
else:
210210
error = getattr(status_data, "message", "Unknown error")
211-
raise Exception(f"Graph creation failed: {error}")
211+
raise RuntimeError(f"Graph creation failed: {error}")
212212

213-
raise Exception(f"Graph creation timed out after {timeout}s")
213+
raise TimeoutError(f"Graph creation timed out after {timeout}s")
214214

215215
def get_graph_info(self, graph_id: str) -> GraphInfo:
216216
"""
@@ -221,12 +221,15 @@ def get_graph_info(self, graph_id: str) -> GraphInfo:
221221
222222
Returns:
223223
GraphInfo with graph details
224+
225+
Raises:
226+
ValueError: If graph not found
224227
"""
225228
from ..client import AuthenticatedClient
226-
from ..api.graphs.get_graph import sync_detailed as get_graph
229+
from ..api.graphs.get_graphs import sync_detailed as get_graphs
227230

228231
if not self.token:
229-
raise Exception("No API key provided. Set X-API-Key in headers.")
232+
raise ValueError("No API key provided. Set X-API-Key in headers.")
230233

231234
client = AuthenticatedClient(
232235
base_url=self.base_url,
@@ -236,59 +239,83 @@ def get_graph_info(self, graph_id: str) -> GraphInfo:
236239
headers=self.headers,
237240
)
238241

239-
response = get_graph(graph_id=graph_id, client=client)
242+
# Use get_graphs and filter for the specific graph
243+
response = get_graphs(client=client)
240244

241245
if not response.parsed:
242-
raise Exception(f"Failed to get graph info: {response.status_code}")
246+
raise RuntimeError(f"Failed to get graphs: {response.status_code}")
243247

244248
data = response.parsed
249+
graphs = None
250+
251+
# Extract graphs list from response
245252
if isinstance(data, dict):
253+
graphs = data.get("graphs", [])
254+
elif hasattr(data, "additional_properties"):
255+
graphs = data.additional_properties.get("graphs", [])
256+
elif hasattr(data, "graphs"):
257+
graphs = data.graphs
258+
else:
259+
raise RuntimeError("Unexpected response format from get_graphs")
260+
261+
# Find the specific graph by ID
262+
graph_data = None
263+
for graph in graphs:
264+
if isinstance(graph, dict):
265+
if graph.get("graph_id") == graph_id or graph.get("id") == graph_id:
266+
graph_data = graph
267+
break
268+
elif hasattr(graph, "graph_id"):
269+
if graph.graph_id == graph_id or getattr(graph, "id", None) == graph_id:
270+
graph_data = graph
271+
break
272+
273+
if not graph_data:
274+
raise ValueError(f"Graph not found: {graph_id}")
275+
276+
# Build GraphInfo from the found graph
277+
if isinstance(graph_data, dict):
246278
return GraphInfo(
247-
graph_id=data.get("graph_id", graph_id),
248-
graph_name=data.get("graph_name", ""),
249-
description=data.get("description"),
250-
schema_extensions=data.get("schema_extensions"),
251-
tags=data.get("tags"),
252-
created_at=data.get("created_at"),
253-
status=data.get("status"),
279+
graph_id=graph_data.get("graph_id") or graph_data.get("id", graph_id),
280+
graph_name=graph_data.get("graph_name") or graph_data.get("name", ""),
281+
description=graph_data.get("description"),
282+
schema_extensions=graph_data.get("schema_extensions"),
283+
tags=graph_data.get("tags"),
284+
created_at=graph_data.get("created_at"),
285+
status=graph_data.get("status"),
254286
)
255287
else:
256288
return GraphInfo(
257-
graph_id=getattr(data, "graph_id", graph_id),
258-
graph_name=getattr(data, "graph_name", ""),
259-
description=getattr(data, "description", None),
260-
schema_extensions=getattr(data, "schema_extensions", None),
261-
tags=getattr(data, "tags", None),
262-
created_at=getattr(data, "created_at", None),
263-
status=getattr(data, "status", None),
289+
graph_id=getattr(graph_data, "graph_id", None)
290+
or getattr(graph_data, "id", graph_id),
291+
graph_name=getattr(graph_data, "graph_name", None)
292+
or getattr(graph_data, "name", ""),
293+
description=getattr(graph_data, "description", None),
294+
schema_extensions=getattr(graph_data, "schema_extensions", None),
295+
tags=getattr(graph_data, "tags", None),
296+
created_at=getattr(graph_data, "created_at", None),
297+
status=getattr(graph_data, "status", None),
264298
)
265299

266300
def delete_graph(self, graph_id: str) -> None:
267301
"""
268302
Delete a graph.
269303
304+
Note: This method is not yet available as the delete_graph endpoint
305+
is not included in the generated SDK. This will be implemented when
306+
the endpoint is added to the API specification.
307+
270308
Args:
271309
graph_id: The graph ID to delete
272-
"""
273-
from ..client import AuthenticatedClient
274-
from ..api.graphs.delete_graph import sync_detailed as delete_graph
275310
276-
if not self.token:
277-
raise Exception("No API key provided. Set X-API-Key in headers.")
278-
279-
client = AuthenticatedClient(
280-
base_url=self.base_url,
281-
token=self.token,
282-
prefix="",
283-
auth_header_name="X-API-Key",
284-
headers=self.headers,
311+
Raises:
312+
NotImplementedError: This feature is not yet available
313+
"""
314+
raise NotImplementedError(
315+
"Graph deletion is not yet available. "
316+
"The delete_graph endpoint needs to be added to the API specification."
285317
)
286318

287-
response = delete_graph(graph_id=graph_id, client=client)
288-
289-
if response.status_code not in [200, 204]:
290-
raise Exception(f"Failed to delete graph: {response.status_code}")
291-
292319
def close(self):
293320
"""Clean up resources (placeholder for consistency)"""
294321
pass

0 commit comments

Comments
 (0)