-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbase_integration.py
More file actions
61 lines (51 loc) · 2.36 KB
/
base_integration.py
File metadata and controls
61 lines (51 loc) · 2.36 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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
from abc import ABC, abstractmethod
from typing import Generic, Iterable, Optional, TypeVar
from airbyte_cdk.connector import TConfig
from airbyte_cdk.models import AirbyteRecordMessage, AirbyteStateMessage, SyncMode, Type
from airbyte_cdk.sources.embedded.catalog import (
create_configured_catalog,
get_stream,
get_stream_names,
)
from airbyte_cdk.sources.embedded.runner import SourceRunner
from airbyte_cdk.sources.embedded.tools import get_defined_id
from airbyte_cdk.sources.utils.schema_helpers import check_config_against_spec_or_exit
TOutput = TypeVar("TOutput")
class BaseEmbeddedIntegration(ABC, Generic[TConfig, TOutput]):
def __init__(self, runner: SourceRunner[TConfig], config: TConfig):
check_config_against_spec_or_exit(config, runner.spec())
self.source = runner
self.config = config
self.last_state: Optional[AirbyteStateMessage] = None
@abstractmethod
def _handle_record(self, record: AirbyteRecordMessage, id: Optional[str]) -> Optional[TOutput]:
"""
Turn an Airbyte record into the appropriate output type for the integration.
"""
pass
def _load_data(
self, stream_name: str, state: Optional[AirbyteStateMessage] = None
) -> Iterable[TOutput]:
catalog = self.source.discover(self.config)
stream = get_stream(catalog, stream_name)
if not stream:
raise ValueError(
f"Stream {stream_name} not found, the following streams are available: {', '.join(get_stream_names(catalog))}"
)
if SyncMode.incremental not in stream.supported_sync_modes:
configured_catalog = create_configured_catalog(stream, sync_mode=SyncMode.full_refresh)
else:
configured_catalog = create_configured_catalog(stream, sync_mode=SyncMode.incremental)
for message in self.source.read(self.config, configured_catalog, state):
if message.type == Type.RECORD:
output = self._handle_record(
message.record,
get_defined_id(stream, message.record.data), # type: ignore[union-attr, arg-type]
)
if output:
yield output
elif message.type is Type.STATE and message.state:
self.last_state = message.state