-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdeclarative.py
More file actions
343 lines (284 loc) · 12 KB
/
declarative.py
File metadata and controls
343 lines (284 loc) · 12 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
"""Support for declarative yaml source testing."""
from __future__ import annotations
import contextlib
import hashlib
import warnings
from pathlib import Path
from typing import IO, TYPE_CHECKING, Any, cast
import pydantic
import yaml
from airbyte_cdk.entrypoint import AirbyteEntrypoint
from airbyte_cdk.sources.declarative.concurrent_declarative_source import (
ConcurrentDeclarativeSource,
)
from airbyte_cdk.sources.declarative.retrievers.simple_retriever import SimpleRetriever
from airbyte_cdk.sources.types import StreamSlice
from airbyte import exceptions as exc
from airbyte._executors.base import Executor
if TYPE_CHECKING:
from argparse import Namespace
from collections.abc import Iterator
from airbyte._message_iterators import AirbyteMessageIterator
def _suppress_cdk_pydantic_deprecation_warnings() -> None:
"""Suppress deprecation warnings from Pydantic in the CDK.
CDK has deprecated uses of `json()` and `parse_obj()`, and we don't want users
to see these warnings.
"""
warnings.filterwarnings(
"ignore",
category=pydantic.warnings.PydanticDeprecatedSince20,
)
def _unwrap_to_declarative_stream(stream: object) -> object:
"""Unwrap a concurrent stream wrapper to access the underlying declarative stream.
This function uses duck-typing to navigate through various wrapper layers that may
exist around declarative streams, depending on the CDK version. It tries common
wrapper attribute names and returns the first object that has a 'retriever' attribute.
Args:
stream: A stream object that may be wrapped (e.g., AbstractStream wrapper).
Returns:
The underlying declarative stream object with a retriever attribute.
Raises:
NotImplementedError: If unable to locate a declarative stream with a retriever.
"""
if hasattr(stream, "retriever"):
return stream
wrapper_attrs = [
"declarative_stream",
"wrapped_stream",
"stream",
"_stream",
"underlying_stream",
"inner",
]
for attr_name in wrapper_attrs:
if hasattr(stream, attr_name):
unwrapped = getattr(stream, attr_name)
if unwrapped is not None and hasattr(unwrapped, "retriever"):
return unwrapped
for branch_attr in ["full_refresh_stream", "incremental_stream"]:
if hasattr(stream, branch_attr):
branch_stream = getattr(stream, branch_attr)
if branch_stream is not None and hasattr(branch_stream, "retriever"):
return branch_stream
stream_type = type(stream).__name__
raise NotImplementedError(
f"Unable to locate declarative stream with retriever from {stream_type}. "
f"fetch_record() requires access to the stream's retriever component."
)
class DeclarativeExecutor(Executor):
"""An executor for declarative sources."""
def __init__(
self,
name: str,
manifest: dict | Path,
components_py: str | Path | None = None,
components_py_checksum: str | None = None,
) -> None:
"""Initialize a declarative executor.
- If `manifest` is a path, it will be read as a json file.
- If `manifest` is a string, it will be parsed as an HTTP path.
- If `manifest` is a dict, it will be used as is.
- If `components_py` is provided, components will be injected into the source.
- If `components_py_checksum` is not provided, it will be calculated automatically.
"""
_suppress_cdk_pydantic_deprecation_warnings()
self.name = name
self._manifest_dict: dict
if isinstance(manifest, Path):
self._manifest_dict = cast("dict", yaml.safe_load(manifest.read_text()))
elif isinstance(manifest, dict):
self._manifest_dict = manifest
config_dict: dict[str, Any] = {}
if components_py:
if isinstance(components_py, Path):
components_py = components_py.read_text()
if components_py_checksum is None:
components_py_checksum = hashlib.md5(components_py.encode()).hexdigest()
config_dict["__injected_components_py"] = components_py
config_dict["__injected_components_py_checksums"] = {
"md5": components_py_checksum,
}
self.reported_version: str | None = self._manifest_dict.get("version", None)
self._config_dict = config_dict
@property
def declarative_source(self) -> ConcurrentDeclarativeSource:
"""Get the declarative source object.
Notes:
1. Since Sep 2025, the declarative source class used is `ConcurrentDeclarativeSource`.
2. The `ConcurrentDeclarativeSource` object sometimes doesn't want to be read from twice,
likely due to threads being already shut down after a successful read.
3. Rather than cache the source object, we recreate it each time we need it, to
avoid any issues with re-using the same object.
"""
return ConcurrentDeclarativeSource(
config=self._config_dict,
source_config=self._manifest_dict,
)
def get_installed_version(
self,
*,
raise_on_error: bool = False,
recheck: bool = False,
) -> str | None:
"""Detect the version of the connector installed."""
_ = raise_on_error, recheck # Not used
return self.reported_version
@property
def _cli(self) -> list[str]:
"""Not applicable."""
return [] # N/A
def execute(
self,
args: list[str],
*,
stdin: IO[str] | AirbyteMessageIterator | None = None,
suppress_stderr: bool = False,
) -> Iterator[str]:
"""Execute the declarative source."""
_ = stdin, suppress_stderr # Not used
source_entrypoint = AirbyteEntrypoint(self.declarative_source)
mapped_args: list[str] = self.map_cli_args(args)
parsed_args: Namespace = source_entrypoint.parse_args(mapped_args)
yield from source_entrypoint.run(parsed_args)
def ensure_installation(self, *, auto_fix: bool = True) -> None:
"""No-op. The declarative source is included with PyAirbyte."""
_ = auto_fix
pass
def install(self) -> None:
"""No-op. The declarative source is included with PyAirbyte."""
pass
def uninstall(self) -> None:
"""No-op. The declarative source is included with PyAirbyte."""
pass
def fetch_record( # noqa: PLR0914, PLR0912, PLR0915
self,
stream_name: str,
primary_key_value: str,
) -> dict[str, Any]:
"""Fetch a single record by primary key from a declarative stream.
This method uses the already-instantiated streams from the declarative source
to access the stream's retriever and make an HTTP GET request by appending
the primary key value to the stream's base path (e.g., /users/123).
Args:
stream_name: The name of the stream to fetch from.
primary_key_value: The primary key value as a string.
Returns:
The fetched record as a dictionary.
Raises:
exc.AirbyteStreamNotFoundError: If the stream is not found.
exc.AirbyteRecordNotFoundError: If the record is not found (empty response).
NotImplementedError: If the stream does not use SimpleRetriever.
"""
streams = self.declarative_source.streams(self._config_dict)
target_stream = None
for stream in streams:
stream_name_attr = getattr(stream, "name", None)
if stream_name_attr == stream_name:
target_stream = stream
break
try:
unwrapped = _unwrap_to_declarative_stream(stream)
if getattr(unwrapped, "name", None) == stream_name:
target_stream = stream
break
except NotImplementedError:
continue
if target_stream is None:
available_streams = []
for s in streams:
name = getattr(s, "name", None)
if name:
available_streams.append(name)
raise exc.AirbyteStreamNotFoundError(
stream_name=stream_name,
connector_name=self.name,
available_streams=available_streams,
message=f"Stream '{stream_name}' not found in source.",
)
declarative_stream = _unwrap_to_declarative_stream(target_stream)
retriever = declarative_stream.retriever # type: ignore[attr-defined]
if not isinstance(retriever, SimpleRetriever):
raise NotImplementedError(
f"Stream '{stream_name}' uses {type(retriever).__name__}, but fetch_record() "
"only supports SimpleRetriever."
)
empty_slice = StreamSlice(partition={}, cursor_slice={})
base_path = retriever.requester.get_path(
stream_state={},
stream_slice=empty_slice,
next_page_token=None,
)
if base_path:
fetch_path = f"{base_path.rstrip('/')}/{primary_key_value}"
else:
fetch_path = primary_key_value
response = retriever.requester.send_request(
path=fetch_path,
stream_state={},
stream_slice=empty_slice,
next_page_token=None,
request_headers=retriever._request_headers( # noqa: SLF001
stream_slice=empty_slice,
next_page_token=None,
),
request_params=retriever._request_params( # noqa: SLF001
stream_slice=empty_slice,
next_page_token=None,
),
request_body_data=retriever._request_body_data( # noqa: SLF001
stream_slice=empty_slice,
next_page_token=None,
),
request_body_json=retriever._request_body_json( # noqa: SLF001
stream_slice=empty_slice,
next_page_token=None,
),
)
if response is None:
msg = (
f"No response received when fetching record with primary key "
f"'{primary_key_value}' from stream '{stream_name}'."
)
raise exc.AirbyteRecordNotFoundError(
stream_name=stream_name,
primary_key_value=primary_key_value,
connector_name=self.name,
message=msg,
)
records_schema = {}
if hasattr(declarative_stream, "schema_loader"):
schema_loader = declarative_stream.schema_loader
if hasattr(schema_loader, "get_json_schema"):
with contextlib.suppress(Exception):
records_schema = schema_loader.get_json_schema()
records = list(
retriever.record_selector.select_records(
response=response,
stream_state={},
records_schema=records_schema,
stream_slice=empty_slice,
next_page_token=None,
)
)
if not records:
try:
response_json = response.json()
if isinstance(response_json, dict) and response_json:
return response_json
except Exception:
pass
msg = (
f"Record with primary key '{primary_key_value}' "
f"not found in stream '{stream_name}'."
)
raise exc.AirbyteRecordNotFoundError(
stream_name=stream_name,
primary_key_value=primary_key_value,
connector_name=self.name,
message=msg,
)
first_record = records[0]
if hasattr(first_record, "data"):
return dict(first_record.data) # type: ignore[arg-type]
return dict(first_record) # type: ignore[arg-type]