-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Low code CDK: Allow nested objects for request_body_json (#26474)
* allow nested JSON * add test for boolean * review comment * change for testing * try fix * try another fix * Revert "change for testing" This reverts commit 931b935. * Revert "try fix" This reverts commit 6f1c6c0.
- Loading branch information
Joe Reuter
committed
May 26, 2023
1 parent
0e8da0c
commit 4a041bf
Showing
7 changed files
with
152 additions
and
7 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
45 changes: 45 additions & 0 deletions
45
...e-cdk/python/airbyte_cdk/sources/declarative/interpolation/interpolated_nested_mapping.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,45 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from dataclasses import InitVar, dataclass | ||
from typing import Any, Mapping, Optional, Union | ||
|
||
from airbyte_cdk.sources.declarative.interpolation.jinja import JinjaInterpolation | ||
from airbyte_cdk.sources.declarative.types import Config | ||
|
||
NestedMappingEntry = Union[dict[str, "NestedMapping"], list["NestedMapping"], str, int, float, bool, None] | ||
NestedMapping = Union[dict[str, NestedMappingEntry], str] | ||
|
||
|
||
@dataclass | ||
class InterpolatedNestedMapping: | ||
""" | ||
Wrapper around a nested dict which can contain lists and primitive values where both the keys and values are interpolated recursively. | ||
Attributes: | ||
mapping (NestedMapping): to be evaluated | ||
""" | ||
|
||
mapping: NestedMapping | ||
parameters: InitVar[Mapping[str, Any]] | ||
|
||
def __post_init__(self, parameters: Optional[Mapping[str, Any]]): | ||
self._interpolation = JinjaInterpolation() | ||
self._parameters = parameters | ||
|
||
def eval(self, config: Config, **additional_parameters): | ||
return self._eval(self.mapping, config, **additional_parameters) | ||
|
||
def _eval(self, value, config, **kwargs): | ||
# Recursively interpolate dictionaries and lists | ||
if isinstance(value, str): | ||
return self._interpolation.eval(value, config, parameters=self._parameters, **kwargs) | ||
elif isinstance(value, dict): | ||
interpolated_dict = {self._eval(k, config, **kwargs): self._eval(v, config, **kwargs) for k, v in value.items()} | ||
return {k: v for k, v in interpolated_dict.items() if v is not None} | ||
elif isinstance(value, list): | ||
return [self._eval(v, config, **kwargs) for v in value] | ||
else: | ||
return value |
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
45 changes: 45 additions & 0 deletions
45
...rces/declarative/requesters/request_options/interpolated_nested_request_input_provider.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,45 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from dataclasses import InitVar, dataclass, field | ||
from typing import Any, Mapping, Optional, Union | ||
|
||
from airbyte_cdk.sources.declarative.interpolation.interpolated_nested_mapping import InterpolatedNestedMapping, NestedMapping | ||
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString | ||
from airbyte_cdk.sources.declarative.types import Config, StreamSlice, StreamState | ||
|
||
|
||
@dataclass | ||
class InterpolatedNestedRequestInputProvider: | ||
""" | ||
Helper class that generically performs string interpolation on a provided deeply nested dictionary or string input | ||
""" | ||
|
||
parameters: InitVar[Mapping[str, Any]] | ||
request_inputs: Optional[NestedMapping] = field(default=None) | ||
config: Config = field(default_factory=dict) | ||
_interpolator: Union[InterpolatedString, InterpolatedNestedMapping] = field(init=False, repr=False, default=None) | ||
_request_inputs: Union[str, Mapping[str, str]] = field(init=False, repr=False, default=None) | ||
|
||
def __post_init__(self, parameters: Mapping[str, Any]): | ||
|
||
self._request_inputs = self.request_inputs or {} | ||
if isinstance(self.request_inputs, str): | ||
self._interpolator = InterpolatedString(self.request_inputs, default="", parameters=parameters) | ||
else: | ||
self._interpolator = InterpolatedNestedMapping(self._request_inputs, parameters=parameters) | ||
|
||
def eval_request_inputs( | ||
self, stream_state: StreamState, stream_slice: Optional[StreamSlice] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Mapping[str, Any]: | ||
""" | ||
Returns the request inputs to set on an outgoing HTTP request | ||
:param stream_state: The stream state | ||
:param stream_slice: The stream slice | ||
:param next_page_token: The pagination token | ||
:return: The request inputs to set on an outgoing HTTP request | ||
""" | ||
kwargs = {"stream_state": stream_state, "stream_slice": stream_slice, "next_page_token": next_page_token} | ||
return self._interpolator.eval(self.config, **kwargs) |
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
45 changes: 45 additions & 0 deletions
45
...k/python/unit_tests/sources/declarative/interpolation/test_interpolated_nested_mapping.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,45 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import dpath.util | ||
import pytest | ||
from airbyte_cdk.sources.declarative.interpolation.interpolated_nested_mapping import InterpolatedNestedMapping | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, path, expected_value", | ||
[ | ||
("test_field_value", "nested/field", "value"), | ||
("test_number", "nested/number", 100), | ||
("test_interpolated_number", "nested/nested_array/1/value", 5), | ||
("test_interpolated_boolean", "nested/nested_array/2/value", True), | ||
("test_field_to_interpolate_from_config", "nested/config_value", "VALUE_FROM_CONFIG"), | ||
("test_field_to_interpolate_from_kwargs", "nested/kwargs_value", "VALUE_FROM_KWARGS"), | ||
("test_field_to_interpolate_from_parameters", "nested/parameters_value", "VALUE_FROM_PARAMETERS"), | ||
("test_key_is_interpolated", "nested/nested_array/0/key", "VALUE"), | ||
], | ||
) | ||
def test(test_name, path, expected_value): | ||
d = { | ||
"nested": { | ||
"field": "value", | ||
"number": 100, | ||
"nested_array": [ | ||
{"{{ parameters.k }}": "VALUE"}, | ||
{"value": "{{ config['num_value'] | int + 2 }}"}, | ||
{"value": "{{ True }}"}, | ||
], | ||
"config_value": "{{ config['c'] }}", | ||
"parameters_value": "{{ parameters['b'] }}", | ||
"kwargs_value": "{{ kwargs['a'] }}", | ||
} | ||
} | ||
|
||
config = {"c": "VALUE_FROM_CONFIG", "num_value": 3} | ||
kwargs = {"a": "VALUE_FROM_KWARGS"} | ||
mapping = InterpolatedNestedMapping(mapping=d, parameters={"b": "VALUE_FROM_PARAMETERS", "k": "key"}) | ||
|
||
interpolated = mapping.eval(config, **{"kwargs": kwargs}) | ||
|
||
assert dpath.util.get(interpolated, path) == expected_value |
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