Skip to content

Commit 66c1f7d

Browse files
committed
file-mode-api: refactor classes to independent files
1 parent a6658e6 commit 66c1f7d

File tree

7 files changed

+114
-50
lines changed

7 files changed

+114
-50
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .file_uploader import FileUploader
2+
from .base_file_uploader import BaseFileUploader
3+
from .base_file_writer import BaseFileWriter
4+
from .connector_builder_file_uploader import ConnectorBuilderFileUploader
5+
from .noop_file_writer import NoopFileWriter
6+
from .file_writer import FileWriter
7+
8+
__all__ = ["FileUploader", "FileWriter", "NoopFileWriter", "ConnectorBuilderFileUploader", "BaseFileUploader", "BaseFileWriter"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from dataclasses import dataclass
6+
7+
from abc import ABC, abstractmethod
8+
from airbyte_cdk.sources.declarative.types import Record
9+
10+
11+
@dataclass
12+
class BaseFileUploader(ABC):
13+
"""
14+
Base class for file uploader
15+
"""
16+
17+
@abstractmethod
18+
def upload(self, record: Record) -> None:
19+
"""
20+
Uploads the file to the specified location
21+
"""
22+
...
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from pathlib import Path
6+
7+
from abc import ABC, abstractmethod
8+
9+
class BaseFileWriter(ABC):
10+
"""
11+
Base File writer class
12+
"""
13+
14+
@abstractmethod
15+
def write(self, file_path: Path, content: bytes) -> int:
16+
"""
17+
Writes the file to the specified location
18+
"""
19+
...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from dataclasses import dataclass
6+
7+
from airbyte_cdk.sources.declarative.retrievers.file_uploader import BaseFileUploader, FileUploader
8+
from airbyte_cdk.sources.declarative.types import Record
9+
10+
11+
@dataclass
12+
class ConnectorBuilderFileUploader(BaseFileUploader):
13+
"""
14+
Connector builder file uploader
15+
Acts as a decorator or wrapper around a FileUploader instance, copying the attributes from record.file_reference into the record.data.
16+
"""
17+
file_uploader: FileUploader
18+
19+
def upload(self, record: Record) -> None:
20+
self.file_uploader.upload(record=record)
21+
for file_reference_attribute in [file_reference_attribute for file_reference_attribute in record.file_reference.__dict__ if not file_reference_attribute.startswith('_')]:
22+
record.data[file_reference_attribute] = getattr(record.file_reference, file_reference_attribute)

airbyte_cdk/sources/declarative/retrievers/file_uploader.py renamed to airbyte_cdk/sources/declarative/retrievers/file_uploader/file_uploader.py

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pathlib import Path
1010
from typing import Any, Mapping, Optional, Union
1111

12-
from abc import ABC, abstractmethod
1312
from airbyte_cdk.models import AirbyteRecordMessageFileReference
1413
from airbyte_cdk.sources.declarative.extractors.record_extractor import RecordExtractor
1514
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import (
@@ -23,54 +22,20 @@
2322
from airbyte_cdk.sources.types import Config
2423
from airbyte_cdk.sources.utils.files_directory import get_files_directory
2524

26-
logger = logging.getLogger("airbyte")
27-
28-
@dataclass
29-
class BaseFileUploader(ABC):
30-
"""
31-
Base class for file uploader
32-
"""
33-
34-
@abstractmethod
35-
def upload(self, record: Record) -> None:
36-
"""
37-
Uploads the file to the specified location
38-
"""
39-
...
40-
41-
class BaseFileWriter(ABC):
42-
"""
43-
Base File writer class
44-
"""
45-
46-
@abstractmethod
47-
def write(self, file_path: Path, content: bytes) -> int:
48-
"""
49-
Writes the file to the specified location
50-
"""
51-
...
52-
53-
class FileWriter(BaseFileWriter):
25+
from .base_file_uploader import BaseFileUploader
26+
from .base_file_writer import BaseFileWriter
5427

55-
def write(self, file_path: Path, content: bytes) -> int:
56-
"""
57-
Writes the file to the specified location
58-
"""
59-
with open(str(file_path), "wb") as f:
60-
f.write(content)
61-
62-
return file_path.stat().st_size
28+
logger = logging.getLogger("airbyte")
6329

64-
class NoopFileWriter(BaseFileWriter):
6530

66-
def write(self, file_path: Path, content: bytes) -> int:
67-
"""
68-
Noop file writer
69-
"""
70-
return 0
7131

7232
@dataclass
7333
class FileUploader(BaseFileUploader):
34+
"""
35+
File uploader class
36+
Handles the upload logic: fetching the download target, making the request via its requester, determining the file path, and calling self.file_writer.write()
37+
Different types of file_writer:BaseFileWriter can be injected to handle different file writing strategies.
38+
"""
7439
requester: Requester
7540
download_target_extractor: RecordExtractor
7641
config: Config
@@ -136,11 +101,4 @@ def upload(self, record: Record) -> None:
136101
)
137102

138103

139-
@dataclass
140-
class ConnectorBuilderFileUploader(BaseFileUploader):
141-
file_uploader: FileUploader
142104

143-
def upload(self, record: Record) -> None:
144-
self.file_uploader.upload(record=record)
145-
for file_reference_attribute in [file_reference_attribute for file_reference_attribute in record.file_reference.__dict__ if not file_reference_attribute.startswith('_')]:
146-
record.data[file_reference_attribute] = getattr(record.file_reference, file_reference_attribute)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from pathlib import Path
6+
7+
from airbyte_cdk.sources.declarative.retrievers.file_uploader import BaseFileWriter
8+
9+
10+
class FileWriter(BaseFileWriter):
11+
12+
def write(self, file_path: Path, content: bytes) -> int:
13+
"""
14+
Writes the file to the specified location
15+
"""
16+
with open(str(file_path), "wb") as f:
17+
f.write(content)
18+
19+
return file_path.stat().st_size
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
3+
#
4+
5+
from pathlib import Path
6+
7+
from airbyte_cdk.sources.declarative.retrievers.file_uploader import BaseFileWriter
8+
9+
10+
class NoopFileWriter(BaseFileWriter):
11+
12+
def write(self, file_path: Path, content: bytes) -> int:
13+
"""
14+
Noop file writer
15+
"""
16+
return 0

0 commit comments

Comments
 (0)