-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add new InterpolatedRequestOptionsProvider that encapsulates all variations of request arguments #13472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new InterpolatedRequestOptionsProvider that encapsulates all variations of request arguments #13472
Changes from 4 commits
6a088c8
178ab0f
02b5404
f7087b9
dc9697a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from typing import Any, Mapping, MutableMapping, Optional, Union | ||
|
||
from airbyte_cdk.sources.declarative.requesters.interpolated_request_input_provider import InterpolatedRequestInputProvider | ||
from airbyte_cdk.sources.declarative.requesters.request_options.request_options_provider import RequestOptionsProvider | ||
|
||
|
||
class InterpolatedRequestOptionsProvider(RequestOptionsProvider): | ||
def __init__(self, *, config, request_parameters=None, request_body_data=None, request_body_json=None): | ||
if request_parameters is None: | ||
request_parameters = {} | ||
if request_body_data is None: | ||
request_body_data = "" | ||
if request_body_json is None: | ||
request_body_json = {} | ||
|
||
if request_body_json and request_body_data: | ||
raise ValueError("RequestOptionsProvider should only contain either 'request_body_data' or 'request_body_json' not both") | ||
|
||
self._parameter_interpolator = InterpolatedRequestInputProvider(config=config, request_inputs=request_parameters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we generify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see what you mean, it does seem a bit weird to have so many underlying interpolators. The tricky part is that as part of initializing the map interpolator, we pass in the template mapping that we're going to interpolate. So even though the inputs to eval are the same, the underlying interpolator will have to change. A few options we have are:
Option 1 feels like an absolute mess to couple object field initialization with eval. But option 2 seems doable. However, I'm still think that having 3 different interpolators despite being pretty verbose is still the clearest way to organize how a template is linked to an interpolator. With option 2 we're just hiding the complexity under another class although with the benefit of only needing one jinja interpolation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per slack discussion: |
||
self._body_data_interpolator = InterpolatedRequestInputProvider(config=config, request_inputs=request_body_data) | ||
self._body_json_interpolator = InterpolatedRequestInputProvider(config=config, request_inputs=request_body_json) | ||
|
||
def request_params( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> MutableMapping[str, Any]: | ||
interpolated_value = self._parameter_interpolator.request_inputs(stream_state, stream_slice, next_page_token) | ||
if isinstance(interpolated_value, dict): | ||
return interpolated_value | ||
return {} | ||
|
||
def request_body_data( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Optional[Union[Mapping, str]]: | ||
return self._body_data_interpolator.request_inputs(stream_state, stream_slice, next_page_token) | ||
|
||
def request_body_json( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Optional[Mapping]: | ||
return self._body_json_interpolator.request_inputs(stream_state, stream_slice, next_page_token) | ||
|
||
def request_kwargs( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did a cursory search of which integrations actually use this |
||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Mapping[str, Any]: | ||
# todo: there are a few integrations that override the request_kwargs() method, but the use case for why kwargs over existing | ||
# constructs is a little unclear. We may revisit this, but for now lets leave it out of the DSL | ||
return {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Mapping, MutableMapping, Optional, Union | ||
|
||
|
||
class RequestOptionsProvider(ABC): | ||
@abstractmethod | ||
def request_params( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> MutableMapping[str, Any]: | ||
pass | ||
|
||
@abstractmethod | ||
def request_body_data( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Optional[Union[Mapping, str]]: | ||
pass | ||
|
||
@abstractmethod | ||
def request_body_json( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Optional[Mapping]: | ||
pass | ||
|
||
@abstractmethod | ||
def request_kwargs( | ||
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None | ||
) -> Mapping[str, Any]: | ||
pass |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
class HttpMethod(Enum): | ||
GET = "GET" | ||
POST = "POST" | ||
|
||
|
||
class Requester(ABC): | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brianjlai should we also add the POST method to
HttpMethod
while we're at it so we can knock off #12884 at the same time?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already added "POST" to the enum in
airbyte-cdk/python/airbyte_cdk/sources/declarative/requesters/requester.py
. I tested it in factory and that did propagate POST to the stream. I wasn't aware of other places that need to be changed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brianjlai I missed that. looks good then!