44automatic operation monitoring. Supports both SSE (Server-Sent Events)
55for real-time updates and polling fallback.
66
7- Graph lifecycle operations (create-subgraph, delete-subgraph, create-backup,
8- change-tier, materialize) all go through the operations surface at
7+ The lifecycle operations this client wraps (materialize, change-tier) go
8+ through the operations surface at
99``POST /v1/graphs/{graph_id}/operations/{op_name}`` and return an
10- ``OperationEnvelope``.
10+ ``OperationEnvelope``. Subgraph and backup operations live on the same
11+ surface but have no method here — call the generated ``api`` functions.
1112
1213Backups are a download capability: there is no customer-facing restore.
1314Graphs with an upstream rebuild from it — entity graphs re-materialize,
2324
2425import httpx
2526
27+ from ..types import Unset
2628from .operation_client import OperationClient , OperationProgress , MonitorOptions
29+ from .token_utils import apply_auth_header , current_token
2730
2831logger = logging .getLogger (__name__ )
2932
@@ -96,8 +99,8 @@ class MaterializationResult:
9699class GraphClient :
97100 """Client for graph management and lifecycle operations.
98101
99- Covers graph creation, info retrieval, and all graph lifecycle
100- operations (materialize, subgraphs, backups, tier changes) .
102+ Covers graph creation and deletion , info retrieval, materialization
103+ and tier changes.
101104 """
102105
103106 def __init__ (self , config : Dict [str , Any ]):
@@ -118,12 +121,14 @@ def _get_authenticated_client(self):
118121 """Build an AuthenticatedClient for API calls."""
119122 from .retry import retrying_authenticated_client
120123
121- if not self .token :
124+ token = current_token (self .config , self .token )
125+
126+ if not token :
122127 raise ValueError ("No API key provided. Set X-API-Key in headers." )
123128
124129 return retrying_authenticated_client (
125130 base_url = self .base_url ,
126- token = self . token ,
131+ token = token ,
127132 headers = self .headers ,
128133 config = self .config ,
129134 )
@@ -250,18 +255,18 @@ def get_graph_info(self, graph_id: str) -> GraphInfo:
250255 client = self ._get_authenticated_client ()
251256 response = get_graphs (client = client )
252257
253- if not response .parsed :
258+ if response . status_code != 200 or not response .parsed :
254259 raise RuntimeError (f"Failed to get graphs: { response .status_code } " )
255260
261+ # Every generated model carries `additional_properties` (the fields the
262+ # schema does not name), so the declared `graphs` field is read first.
256263 data = response .parsed
257- graphs = None
258-
259264 if isinstance (data , dict ):
260265 graphs = data .get ("graphs" , [])
261- elif hasattr (data , "additional_properties" ):
262- graphs = data .additional_properties .get ("graphs" , [])
263266 elif hasattr (data , "graphs" ):
264267 graphs = data .graphs
268+ elif hasattr (data , "additional_properties" ):
269+ graphs = data .additional_properties .get ("graphs" , [])
265270 else :
266271 raise RuntimeError ("Unexpected response format from get_graphs" )
267272
@@ -290,16 +295,19 @@ def get_graph_info(self, graph_id: str) -> GraphInfo:
290295 status = graph_data .get ("status" ),
291296 )
292297 else :
298+
299+ def field (name : str ) -> Any :
300+ value = getattr (graph_data , name , None )
301+ return None if isinstance (value , Unset ) else value
302+
293303 return GraphInfo (
294- graph_id = getattr (graph_data , "graph_id" , None )
295- or getattr (graph_data , "id" , graph_id ),
296- graph_name = getattr (graph_data , "graph_name" , None )
297- or getattr (graph_data , "name" , "" ),
298- description = getattr (graph_data , "description" , None ),
299- schema_extensions = getattr (graph_data , "schema_extensions" , None ),
300- tags = getattr (graph_data , "tags" , None ),
301- created_at = getattr (graph_data , "created_at" , None ),
302- status = getattr (graph_data , "status" , None ),
304+ graph_id = field ("graph_id" ) or field ("id" ) or graph_id ,
305+ graph_name = field ("graph_name" ) or field ("name" ) or "" ,
306+ description = field ("description" ),
307+ schema_extensions = field ("schema_extensions" ),
308+ tags = field ("tags" ),
309+ created_at = field ("created_at" ),
310+ status = field ("status" ),
303311 )
304312
305313 # ---------------------------------------------------------------------------
@@ -535,7 +543,10 @@ def _wait_with_sse(
535543 ) -> str :
536544 """Wait for operation completion using SSE stream."""
537545 stream_url = f"{ self .base_url } /v1/operations/{ operation_id } /stream"
538- headers = {"X-API-Key" : self .token , "Accept" : "text/event-stream" }
546+ headers = {"Accept" : "text/event-stream" }
547+ token = current_token (self .config , self .token )
548+ if token :
549+ apply_auth_header (headers , token )
539550
540551 with httpx .Client (timeout = httpx .Timeout (timeout + 5.0 )) as http_client :
541552 with http_client .stream ("GET" , stream_url , headers = headers ) as response :
0 commit comments