-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'alex/selectNoRecords' into alex/configbasedsendgrid
- Loading branch information
Showing
9 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
airbyte-cdk/python/airbyte_cdk/sources/declarative/extractors/dpath_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from dataclasses import InitVar, dataclass | ||
from typing import Any, List, Mapping, Union | ||
|
||
import dpath.util | ||
import requests | ||
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder | ||
from airbyte_cdk.sources.declarative.decoders.json_decoder import JsonDecoder | ||
from airbyte_cdk.sources.declarative.extractors.record_extractor import RecordExtractor | ||
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString | ||
from airbyte_cdk.sources.declarative.types import Config, Record | ||
from dataclasses_jsonschema import JsonSchemaMixin | ||
|
||
|
||
@dataclass | ||
class DpathExtractor(RecordExtractor, JsonSchemaMixin): | ||
""" | ||
Record extractor that searches a decoded response over a path defined as an array of fields. | ||
Examples of instantiating this transform: | ||
``` | ||
extractor: | ||
type: DpathExtractor | ||
transform: | ||
- "root" | ||
- "data" | ||
``` | ||
``` | ||
extractor: | ||
type: DpathExtractor | ||
transform: | ||
- "root" | ||
- "{{ options['field'] }}" | ||
``` | ||
Attributes: | ||
transform (Union[InterpolatedString, str]): Pointer to the field that should be extracted | ||
config (Config): The user-provided configuration as specified by the source's spec | ||
decoder (Decoder): The decoder responsible to transfom the response in a Mapping | ||
""" | ||
|
||
field_pointer: List[Union[InterpolatedString, str]] | ||
config: Config | ||
options: InitVar[Mapping[str, Any]] | ||
decoder: Decoder = JsonDecoder(options={}) | ||
|
||
def __post_init__(self, options: Mapping[str, Any]): | ||
for pointer_index in range(len(self.field_pointer)): | ||
if isinstance(self.field_pointer[pointer_index], str): | ||
self.field_pointer[pointer_index] = InterpolatedString.create(self.field_pointer[pointer_index], options=options) | ||
|
||
def extract_records(self, response: requests.Response) -> List[Record]: | ||
response_body = self.decoder.decode(response) | ||
return dpath.util.get(response_body, [pointer.eval(self.config) for pointer in self.field_pointer], default=[]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
airbyte-cdk/python/airbyte_cdk/sources/declarative/extractors/record_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
import requests | ||
from airbyte_cdk.sources.declarative.types import Record | ||
|
||
|
||
@dataclass | ||
class RecordExtractor(ABC): | ||
""" | ||
Responsible for translating an HTTP response into a list of records by extracting records from the response and optionally filtering | ||
records based on a heuristic. | ||
""" | ||
|
||
@abstractmethod | ||
def extract_records( | ||
self, | ||
response: requests.Response, | ||
) -> List[Record]: | ||
""" | ||
Selects records from the response | ||
:param response: The response to extract the records from | ||
:return: List of Records extracted from the response | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
airbyte-cdk/python/unit_tests/sources/declarative/extractors/test_dpath_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import json | ||
|
||
import pytest | ||
import requests | ||
from airbyte_cdk.sources.declarative.decoders.json_decoder import JsonDecoder | ||
from airbyte_cdk.sources.declarative.extractors.dpath_extractor import DpathExtractor | ||
|
||
config = {"field": "record_array"} | ||
options = {"options_field": "record_array"} | ||
|
||
decoder = JsonDecoder(options={}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, field_pointer, body, expected_records", | ||
[ | ||
("test_extract_from_array", ["data"], {"data": [{"id": 1}, {"id": 2}]}, [{"id": 1}, {"id": 2}]), | ||
("test_nested_field", ["data", "records"], {"data": {"records": [{"id": 1}, {"id": 2}]}}, [{"id": 1}, {"id": 2}]), | ||
("test_field_in_config", ["{{ config['field'] }}"], {"record_array": [{"id": 1}, {"id": 2}]}, [{"id": 1}, {"id": 2}]), | ||
("test_field_in_options", ["{{ options['options_field'] }}"], {"record_array": [{"id": 1}, {"id": 2}]}, [{"id": 1}, {"id": 2}]), | ||
("test_field_does_not_exist", ["record"], {"id": 1}, []), | ||
], | ||
) | ||
def test_dpath_extractor(test_name, field_pointer, body, expected_records): | ||
extractor = DpathExtractor(field_pointer=field_pointer, config=config, decoder=decoder, options=options) | ||
|
||
response = create_response(body) | ||
actual_records = extractor.extract_records(response) | ||
|
||
assert actual_records == expected_records | ||
|
||
|
||
def create_response(body): | ||
response = requests.Response() | ||
response._content = json.dumps(body).encode("utf-8") | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters