-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
319 lines (279 loc) · 11.5 KB
/
Copy pathhttp.py
File metadata and controls
319 lines (279 loc) · 11.5 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
311
312
313
314
315
316
317
318
319
"""HTTP access to Hotdata via the official ``hotdata`` Python SDK (OpenAPI client)."""
from __future__ import annotations
import http
import io
import json
import time
from collections.abc import Callable, Mapping, Sequence
from typing import Any, TypeVar
import pyarrow as pa
import pyarrow_hotfix # noqa: F401
import pyarrow.ipc as pa_ipc
from hotdata import ApiClient, Configuration
from hotdata.api import (
ConnectionsApi,
InformationSchemaApi,
QueryApi,
QueryRunsApi,
ResultsApi,
UploadsApi,
)
from hotdata.api.databases_api import DatabasesApi
from hotdata.exceptions import ApiException
from hotdata.models import QueryRequest
from hotdata.models.async_query_response import AsyncQueryResponse
from hotdata.models.create_database_request import CreateDatabaseRequest
from hotdata.models.database_default_schema_decl import DatabaseDefaultSchemaDecl
from hotdata.models.database_default_table_decl import DatabaseDefaultTableDecl
from hotdata.models.load_managed_table_request import LoadManagedTableRequest
T = TypeVar("T")
# Matches Hotdata / runtimedb ``GET /v1/results/{{id}}`` Arrow responses.
APPLICATION_ARROW_STREAM = "application/vnd.apache.arrow.stream"
# Statuses that mean the query run is still in progress.
# runtimedb QueryRunStatus only emits "running", "succeeded", "failed".
_IN_FLIGHT = {"running"}
def _sleep_until(deadline: float, interval: float) -> None:
"""Sleep up to ``interval`` s but never past ``deadline`` (cleaner timeout behavior)."""
remaining = deadline - time.monotonic()
if remaining > 0:
time.sleep(min(interval, remaining))
class HotdataAPIError(Exception):
def __init__(self, message: str, *, status_code: int | None = None, body: Any = None):
super().__init__(message)
self.status_code = status_code
self.body = body
def _from_api_exception(exc: ApiException) -> HotdataAPIError:
body = exc.body
if isinstance(body, (bytes, bytearray)):
body = body.decode("utf-8", errors="replace")
msg = f"Hotdata API error: {exc.reason}"
if body:
msg = f"{msg} {body}"
return HotdataAPIError(msg.strip(), status_code=exc.status, body=exc.body)
def _ipc_stream_bytes_to_table(data: bytes) -> pa.Table:
with pa_ipc.open_stream(io.BytesIO(data)) as reader:
return reader.read_all()
def _json_utf8(obj: bytes) -> Any:
return json.loads(obj.decode("utf-8"))
class HotdataClient:
"""Thin wrapper around the SDK used by the Ibis backend."""
def __init__(
self,
*,
api_url: str,
token: str,
workspace_id: str,
session_id: str | None = None,
timeout: float = 120.0,
verify_ssl: bool | str = True,
) -> None:
host = api_url.rstrip("/")
conf = Configuration(
host=host, api_key=token, workspace_id=workspace_id, session_id=session_id
)
if verify_ssl is False:
conf.verify_ssl = False
elif isinstance(verify_ssl, str):
conf.ssl_ca_cert = verify_ssl
self._timeout = timeout
self._client = ApiClient(conf)
self._query = QueryApi(self._client)
self._query_runs = QueryRunsApi(self._client)
self._results = ResultsApi(self._client)
self._connections = ConnectionsApi(self._client)
self._databases = DatabasesApi(self._client)
self._information_schema = InformationSchemaApi(self._client)
self._uploads = UploadsApi(self._client)
def close(self) -> None:
client = self._client
close_fn = getattr(client, "close", None)
if callable(close_fn):
close_fn()
return
pool = getattr(getattr(client, "rest_client", None), "pool_manager", None)
if pool is not None:
pool.clear()
def _safe_call(self, fn: Callable[..., T], /, *args: Any, **kwargs: Any) -> T:
try:
return fn(*args, _request_timeout=self._timeout, **kwargs)
except ApiException as exc:
raise _from_api_exception(exc) from exc
def list_connections(self) -> dict[str, Any]:
"""GET ``/v1/connections``."""
out = self._safe_call(self._connections.list_connections)
return out.model_dump(by_alias=True, mode="json")
def get_information_schema(self, params: Mapping[str, Any]) -> dict[str, Any]:
"""GET ``/v1/information_schema`` — ``params`` uses REST names (``schema`` not ``var_schema``)."""
out = self._safe_call(
self._information_schema.information_schema,
connection_id=params.get("connection_id"),
var_schema=params.get("schema"),
table=params.get("table"),
include_columns=params.get("include_columns"),
limit=params.get("limit"),
cursor=params.get("cursor"),
)
return out.model_dump(by_alias=True, mode="json")
def execute_query(
self,
sql: str,
*,
database_id: str | None = None,
async_after_ms: int | None = None,
poll_interval_s: float = 0.25,
poll_timeout_s: float = 600.0,
) -> dict[str, Any]:
req = QueryRequest(sql=sql, var_async=True, async_after_ms=async_after_ms)
kwargs: dict[str, Any] = {}
if database_id is not None:
kwargs["x_database_id"] = database_id
out = self._safe_call(self._query.query, req, **kwargs)
if isinstance(out, AsyncQueryResponse):
query_run_id = out.query_run_id
deadline = time.monotonic() + poll_timeout_s
while time.monotonic() < deadline:
qr = self._safe_call(self._query_runs.get_query_run, query_run_id)
status = qr.status
if status == "failed":
raise HotdataAPIError(qr.error_message or "Query run failed")
if status == "succeeded":
result_id = qr.result_id
if result_id is None:
raise HotdataAPIError("succeeded query run missing result_id")
return self._poll_result_arrow(
result_id,
deadline=deadline,
poll_interval_s=poll_interval_s,
)
if status not in _IN_FLIGHT:
raise HotdataAPIError(f"Unexpected query run status: {status!r}")
_sleep_until(deadline, poll_interval_s)
raise HotdataAPIError("Timeout waiting for asynchronous query")
raise HotdataAPIError("Unexpected query response type")
def upload_file(self, data: bytes, *, content_type: str | None = None) -> dict[str, Any]:
kwargs: dict[str, Any] = {}
if content_type is not None:
kwargs["_content_type"] = content_type
resp = self._safe_call(self._uploads.upload_file, data, **kwargs)
return resp.model_dump(by_alias=True, mode="json")
def list_databases(self) -> dict[str, Any]:
"""GET ``/v1/databases``."""
out = self._safe_call(self._databases.list_databases)
return out.model_dump(by_alias=True, mode="json")
def get_database(self, database_id: str) -> dict[str, Any]:
"""GET ``/v1/databases/{database_id}``."""
out = self._safe_call(self._databases.get_database, database_id)
return out.model_dump(by_alias=True, mode="json")
def create_managed_database(
self,
description: str | None = None,
*,
schema: str = "public",
tables: Sequence[str] = (),
) -> dict[str, Any]:
"""POST ``/v1/databases`` — creates a managed database with an auto-provisioned default catalog."""
schemas = None
if tables:
schemas = [
DatabaseDefaultSchemaDecl(
name=schema,
tables=[DatabaseDefaultTableDecl(name=t) for t in tables],
)
]
req = CreateDatabaseRequest(description=description, schemas=schemas)
resp = self._safe_call(self._databases.create_database, req)
return resp.model_dump(by_alias=True, mode="json")
def delete_database(self, database_id: str) -> None:
"""DELETE ``/v1/databases/{database_id}``."""
self._safe_call(self._databases.delete_database, database_id)
def load_managed_table(
self,
connection_id: str,
schema: str,
table: str,
*,
upload_id: str,
) -> dict[str, Any]:
req = LoadManagedTableRequest(mode="replace", upload_id=upload_id)
resp = self._safe_call(
self._connections.load_managed_table,
connection_id,
schema,
table,
req,
)
return resp.model_dump(by_alias=True, mode="json")
def delete_managed_table(self, connection_id: str, schema: str, table: str) -> None:
self._safe_call(
self._connections.delete_managed_table,
connection_id,
schema,
table,
)
def _poll_result_arrow(
self,
result_id: str,
*,
deadline: float,
poll_interval_s: float,
) -> dict[str, Any]:
"""Poll ``GET /v1/results/{{id}}`` with ``Accept: application/vnd.apache.arrow.stream``."""
while time.monotonic() < deadline:
try:
raw = self._results.get_result_without_preload_content(
result_id,
_headers={"Accept": APPLICATION_ARROW_STREAM},
_request_timeout=self._timeout,
)
except ApiException as exc:
raise _from_api_exception(exc) from exc
body = raw.read()
status = raw.status
ctype = (raw.headers.get("Content-Type") or "").split(";")[0].strip().lower()
if status == http.HTTPStatus.OK and ctype == APPLICATION_ARROW_STREAM.lower():
table = _ipc_stream_bytes_to_table(body)
return self._arrow_payload_from_table(table, result_id=result_id)
if status == http.HTTPStatus.ACCEPTED:
_sleep_until(deadline, poll_interval_s)
continue
if status == http.HTTPStatus.CONFLICT:
d = _json_utf8(body) if body else {}
raise HotdataAPIError(
d.get("error_message") or "Result failed",
status_code=http.HTTPStatus.CONFLICT,
body=d,
)
if status == http.HTTPStatus.NOT_FOUND:
d = _json_utf8(body) if body else {}
raise HotdataAPIError(
d.get("detail") or f"Result {result_id!r} not found",
status_code=http.HTTPStatus.NOT_FOUND,
body=d,
)
raise HotdataAPIError(
f"Unexpected GET /v1/results/{result_id} status {status}",
status_code=status,
body=body,
)
raise HotdataAPIError("Timeout waiting for Arrow query result")
def _arrow_payload_from_table(
self,
table: pa.Table,
*,
result_id: str,
) -> dict[str, Any]:
sch = table.schema
columns = sch.names
nullable = [field.nullable for field in sch]
return {
"format": "arrow",
"pa_table": table,
"columns": columns,
"nullable": nullable,
"rows": [],
"result_id": result_id,
"row_count": table.num_rows,
"execution_time_ms": None,
"query_run_id": None,
"warning": None,
}