-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[low-code-connectors] Replace JelloExtractor with DpathExtractor (#15514
) * Handle extracting no records from root * handle missing keys * record extractor interface * dpath extractor * docstring * handle extract root array * Update airbyte-cdk/python/airbyte_cdk/sources/declarative/extractors/jello.py Co-authored-by: Sherif A. Nada <snadalive@gmail.com> * Update airbyte-cdk/python/airbyte_cdk/sources/declarative/extractors/record_selector.py Co-authored-by: Sherif A. Nada <snadalive@gmail.com> * update docstring * respect extractor interface * edge case handling * document * use dpath by default * delete jello extractor * bump cdk version * delete jello dependency * Update reference docs templates * update template Co-authored-by: Sherif A. Nada <snadalive@gmail.com>
- Loading branch information
1 parent
8c30067
commit 6332fd6
Showing
21 changed files
with
240 additions
and
156 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
# | ||
# 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. | ||
If the field pointer points to an array, that array is returned. | ||
If the field pointer points to an object, that object is returned wrapped as an array. | ||
If the field pointer points to an empty object, an empty array is returned. | ||
If the field pointer points to a non-existing path, an empty array is returned. | ||
Examples of instantiating this transform: | ||
``` | ||
extractor: | ||
type: DpathExtractor | ||
field_pointer: | ||
- "root" | ||
- "data" | ||
``` | ||
``` | ||
extractor: | ||
type: DpathExtractor | ||
field_pointer: | ||
- "root" | ||
- "{{ options['field'] }}" | ||
``` | ||
``` | ||
extractor: | ||
type: DpathExtractor | ||
field_pointer: [] | ||
``` | ||
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) | ||
if len(self.field_pointer) == 0: | ||
extracted = response_body | ||
else: | ||
pointer = [pointer.eval(self.config) for pointer in self.field_pointer] | ||
extracted = dpath.util.get(response_body, pointer, default=[]) | ||
if isinstance(extracted, list): | ||
return extracted | ||
elif extracted: | ||
return [extracted] | ||
else: | ||
return [] |
43 changes: 0 additions & 43 deletions
43
airbyte-cdk/python/airbyte_cdk/sources/declarative/extractors/jello.py
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
# | ||
# 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. | ||
""" | ||
|
||
@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
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
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
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
Oops, something went wrong.