-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
310 lines (260 loc) · 11.1 KB
/
client.py
File metadata and controls
310 lines (260 loc) · 11.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
from collections.abc import AsyncIterator
from contextlib import AbstractAsyncContextManager, asynccontextmanager
from types import TracebackType
from typing import Any, Literal, Self, TypeVar, overload
from deepset_mcp.api.custom_components.resource import CustomComponentsResource
from deepset_mcp.api.haystack_service.resource import HaystackServiceResource
from deepset_mcp.api.indexes.resource import IndexResource
from deepset_mcp.api.integrations.resource import IntegrationResource
from deepset_mcp.api.pipeline.resource import PipelineResource
from deepset_mcp.api.pipeline_template.resource import PipelineTemplateResource
from deepset_mcp.api.protocols import AsyncClientProtocol
from deepset_mcp.api.search_history.resource import SearchHistoryResource
from deepset_mcp.api.secrets.resource import SecretResource
from deepset_mcp.api.transport import (
AsyncTransport,
StreamingResponse,
TransportProtocol,
TransportResponse,
)
from deepset_mcp.api.user.resource import UserResource
from deepset_mcp.api.workspace.resource import WorkspaceResource
T = TypeVar("T")
class AsyncDeepsetClient(AsyncClientProtocol):
"""Async Client for interacting with the deepset API.
This client provides asynchronous access to most deepset API endpoints
and resources through a convenient interface.
**Example usage:**
```python
from deepset_mcp.api import AsyncDeepsetClient
async with AsyncDeepsetClient() as client:
pipes = await client.pipelines(workspace="example-workspace")
all_pipelines = await pipes.list()
example_pipeline = await pipes.get(pipeline_name="example-pipeline")
await pipes.create(pipeline_name="example-pipeline-v2", yaml_config="...")
indexes = await client.indexes(workspace="example-workspace")
all_indexes = await indexes.list()
example_index = await indexes.get(index_name="example-index")
```
"""
def __init__(
self,
api_key: str | None = None,
base_url: str = "https://api.cloud.deepset.ai/api",
# base_url: str = "https://api.dev.cloud.dpst.dev/api/",
transport: TransportProtocol | None = None,
transport_config: dict[str, Any] | None = None,
) -> None:
"""Initialize an instance of the AsyncDeepsetClient.
:param api_key: API key or token. Falls back to DEEPSET_API_KEY env var
:param base_url: Base URL for the deepset API
:param transport: Custom transport implementation
:param transport_config: Configuration for default transport (e.g. timeout). All configuration parameters are
forwarded to httpx.AsyncClient.
"""
self.api_key = api_key or os.environ.get("DEEPSET_API_KEY")
if not self.api_key:
raise ValueError("API key not provided and DEEPSET_API_KEY environment variable not set")
self.base_url = base_url
if transport is not None:
self._transport = transport
else:
self._transport = AsyncTransport(
base_url=self.base_url,
api_key=self.api_key,
config=transport_config,
)
def pipelines(self, workspace: str) -> PipelineResource:
"""Resource to interact with pipelines in the specified workspace.
:param workspace: Workspace identifier
:returns: Pipeline resource instance
"""
return PipelineResource(client=self, workspace=workspace)
def indexes(self, workspace: str) -> IndexResource:
"""Resource to interact with indexes in the specified workspace.
:param workspace: Workspace identifier
:returns: Index resource instance
"""
return IndexResource(client=self, workspace=workspace)
def pipeline_templates(self, workspace: str) -> PipelineTemplateResource:
"""Resource to interact with pipeline templates in the specified workspace.
:param workspace: Workspace identifier
:returns: Pipeline template resource instance
"""
return PipelineTemplateResource(client=self, workspace=workspace)
def search_history(self, workspace: str) -> SearchHistoryResource:
"""Resource to interact with search history in the specified workspace.
:param workspace: Workspace identifier
:returns: Search history resource instance
"""
return SearchHistoryResource(client=self, workspace=workspace)
def haystack_service(self) -> HaystackServiceResource:
"""Resource to interact with the Haystack service API.
:returns: Haystack service resource instance
"""
return HaystackServiceResource(client=self)
def integrations(self) -> IntegrationResource:
"""Resource to interact with integrations.
:returns: Integration resource instance
"""
return IntegrationResource(client=self)
def custom_components(self, workspace: str) -> CustomComponentsResource:
"""Resource to interact with custom components in the specified workspace.
:param workspace: Workspace identifier
:returns: Custom components resource instance
"""
return CustomComponentsResource(client=self)
def secrets(self) -> SecretResource:
"""Resource to interact with secrets.
:returns: Secret resource instance
"""
return SecretResource(client=self)
def workspaces(self) -> WorkspaceResource:
"""Resource to interact with workspaces.
:returns: Workspace resource instance
"""
return WorkspaceResource(client=self)
def users(self) -> UserResource:
"""Resource to interact with users.
:returns: User resource instance
"""
return UserResource(client=self)
@overload
async def request(
self,
endpoint: str,
*,
response_type: type[T],
method: str = "GET",
data: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
timeout: float | None | Literal["config"] = "config",
**kwargs: Any,
) -> TransportResponse[T]: ...
@overload
async def request(
self,
endpoint: str,
*,
response_type: None = None,
method: str = "GET",
data: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
timeout: float | None | Literal["config"] = "config",
**kwargs: Any,
) -> TransportResponse[Any]: ...
async def request(
self,
endpoint: str,
*,
method: str = "GET",
data: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
response_type: type[T] | None = None,
timeout: float | None | Literal["config"] = "config",
**kwargs: Any,
) -> TransportResponse[Any]:
"""Make a regular (non-streaming) request to the deepset API.
:param endpoint: API endpoint path
:param method: HTTP method
:param data: JSON data to send in request body
:param headers: Additional headers to include
:param response_type: Expected response type for type checking
:param timeout: Request timeout in seconds. If "config", uses transport config timeout.
If None, disables timeout. If float, uses specific timeout
:param kwargs: Additional arguments to pass to transport
:returns: Response with parsed JSON if available
"""
if not endpoint.startswith("/"):
endpoint = f"/{endpoint}"
url = self.base_url + endpoint
# Default headers
request_headers: dict[str, str] = {
"Authorization": f"Bearer {self.api_key}",
"Accept": "application/json,text/plain,*/*",
}
if data is not None:
request_headers["Content-Type"] = "application/json"
# Merge custom headers
if headers:
headers.setdefault("Authorization", request_headers["Authorization"])
request_headers.update(headers)
return await self._transport.request(
method,
url,
json=data,
headers=request_headers,
response_type=response_type,
timeout=timeout,
**kwargs,
)
def stream_request(
self,
endpoint: str,
*,
method: str = "POST",
data: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
**kwargs: Any,
) -> AbstractAsyncContextManager[StreamingResponse]:
"""Make a streaming request to the deepset API.
Must be used as an async context manager to ensure proper cleanup.
Example::
async with client.stream_request("/pipelines/search-stream", data={"query": "AI"}) as response:
if response.success:
async for line in response.iter_lines():
# Process each line of the stream
data = json.loads(line)
print(data)
else:
# Handle error
error_body = await response.read_body()
print(f"Error {response.status_code}: {error_body}")
:param endpoint: API endpoint path
:param method: HTTP method (usually POST for streaming)
:param data: JSON data to send in request body
:param headers: Additional headers to include
:param kwargs: Additional arguments to pass to transport
:returns: Response object with streaming capabilities
"""
@asynccontextmanager
async def _stream() -> AsyncIterator[StreamingResponse]:
if not endpoint.startswith("/"):
full_endpoint = f"/{endpoint}"
url = self.base_url + full_endpoint
# Default headers for streaming
request_headers: dict[str, str] = {
"Authorization": f"Bearer {self.api_key}",
"Accept": "text/event-stream,application/json,text/plain,*/*",
}
if data is not None:
request_headers["Content-Type"] = "application/json"
# Merge custom headers
if headers:
headers.setdefault("Authorization", request_headers["Authorization"])
request_headers.update(headers)
async with self._transport.stream(
method,
url,
json=data,
headers=request_headers,
**kwargs,
) as response:
yield response
return _stream()
async def close(self) -> None:
"""Close underlying transport resources."""
await self._transport.close()
async def __aenter__(self) -> Self:
"""Enter the AsyncContextManager."""
return self
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> bool:
"""Exit the AsyncContextmanager and clean up resources."""
await self.close()
return False