-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.py
More file actions
206 lines (167 loc) · 6.7 KB
/
Copy pathextensions.py
File metadata and controls
206 lines (167 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""RoboSystems Client Extensions - Main entry point
Enhanced clients with SSE support for the RoboSystems API.
"""
from dataclasses import dataclass
from typing import Dict, Any, Optional, Callable
from .query_client import QueryClient
from .agent_client import AgentClient
from .operation_client import OperationClient
from .file_client import FileClient
from .materialization_client import MaterializationClient
from .table_client import TableClient
from .graph_client import GraphClient
from .sse_client import SSEClient
@dataclass
class RoboSystemsExtensionConfig:
"""Configuration for RoboSystems extensions"""
base_url: Optional[str] = None
headers: Optional[Dict[str, str]] = None
max_retries: int = 5
retry_delay: int = 1000
timeout: int = 30
s3_endpoint_url: Optional[str] = None # Override S3 endpoint (e.g., for LocalStack)
class RoboSystemsExtensions:
"""Main extensions class providing enhanced RoboSystems API functionality"""
def __init__(self, config: RoboSystemsExtensionConfig = None):
if config is None:
config = RoboSystemsExtensionConfig()
# Get base URL from config or use default
self.config = {
"base_url": config.base_url or "http://localhost:8000",
"headers": config.headers or {},
"max_retries": config.max_retries,
"retry_delay": config.retry_delay,
"timeout": config.timeout,
"s3_endpoint_url": config.s3_endpoint_url,
}
# Extract token from headers if it was set by auth classes
# The auth classes should set the token in a standard way
token = None
if config.headers:
# Check for Authorization Bearer token
auth_header = config.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
token = auth_header[7:]
# Check for X-API-Key
elif config.headers.get("X-API-Key"):
token = config.headers.get("X-API-Key")
# Pass token to child clients if available
if token:
self.config["token"] = token
# Initialize clients
self.query = QueryClient(self.config)
self.agent = AgentClient(self.config)
self.operations = OperationClient(self.config)
self.files = FileClient(self.config)
self.materialization = MaterializationClient(self.config)
self.tables = TableClient(self.config)
self.graphs = GraphClient(self.config)
def monitor_operation(
self, operation_id: str, on_progress: Optional[Callable] = None
) -> Any:
"""Convenience method to monitor any operation"""
from .operation_client import MonitorOptions
options = MonitorOptions(on_progress=on_progress)
return self.operations.monitor_operation(operation_id, options)
def create_sse_client(self) -> SSEClient:
"""Create custom SSE client for advanced use cases"""
from .sse_client import SSEConfig
sse_config = SSEConfig(
base_url=self.config["base_url"],
headers=self.config["headers"],
max_retries=self.config["max_retries"],
retry_delay=self.config["retry_delay"],
timeout=self.config["timeout"],
)
return SSEClient(sse_config)
def close(self):
"""Clean up all active connections"""
self.query.close()
self.agent.close()
self.operations.close_all()
if hasattr(self.files, "close"):
self.files.close()
if hasattr(self.materialization, "close"):
self.materialization.close()
if hasattr(self.tables, "close"):
self.tables.close()
self.graphs.close()
# Convenience methods that delegate to the appropriate clients
def execute_query(self, graph_id: str, query: str, parameters: Dict[str, Any] = None):
"""Execute a query using the query client"""
return self.query.query(graph_id, query, parameters)
def stream_query(
self,
graph_id: str,
query: str,
parameters: Dict[str, Any] = None,
chunk_size: int = 1000,
):
"""Stream a query using the query client"""
return self.query.stream_query(graph_id, query, parameters, chunk_size)
def get_operation_status(self, operation_id: str):
"""Get operation status using the operation client"""
return self.operations.get_operation_status(operation_id)
def cancel_operation(self, operation_id: str):
"""Cancel an operation using the operation client"""
return self.operations.cancel_operation(operation_id)
class AsyncRoboSystemsExtensions:
"""Async version of the extensions class"""
def __init__(self, config: RoboSystemsExtensionConfig = None):
if config is None:
config = RoboSystemsExtensionConfig()
self.config = {
"base_url": config.base_url or "http://localhost:8000",
"headers": config.headers or {},
"max_retries": config.max_retries,
"retry_delay": config.retry_delay,
"timeout": config.timeout,
}
# Initialize async clients
from .query_client import AsyncQueryClient
from .operation_client import AsyncOperationClient
self.query = AsyncQueryClient(self.config)
self.operations = AsyncOperationClient(self.config)
async def monitor_operation(
self, operation_id: str, on_progress: Optional[Callable] = None
) -> Any:
"""Convenience method to monitor any operation (async)"""
from .operation_client import MonitorOptions
options = MonitorOptions(on_progress=on_progress)
return await self.operations.monitor_operation(operation_id, options)
def create_sse_client(self):
"""Create custom async SSE client for advanced use cases"""
from .sse_client import AsyncSSEClient, SSEConfig
sse_config = SSEConfig(
base_url=self.config["base_url"],
headers=self.config["headers"],
max_retries=self.config["max_retries"],
retry_delay=self.config["retry_delay"],
timeout=self.config["timeout"],
)
return AsyncSSEClient(sse_config)
async def close(self):
"""Clean up all active connections (async)"""
await self.query.close()
await self.operations.close_all()
async def execute_query(
self, graph_id: str, query: str, parameters: Dict[str, Any] = None
):
"""Execute a query using the async query client"""
return await self.query.query(graph_id, query, parameters)
async def stream_query(
self,
graph_id: str,
query: str,
parameters: Dict[str, Any] = None,
chunk_size: int = 1000,
):
"""Stream a query using the async query client"""
async for item in self.query.stream_query(graph_id, query, parameters, chunk_size):
yield item
async def get_operation_status(self, operation_id: str):
"""Get operation status using the async operation client"""
return await self.operations.get_operation_status(operation_id)
async def cancel_operation(self, operation_id: str):
"""Cancel an operation using the async operation client"""
return await self.operations.cancel_operation(operation_id)