Skip to content
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

Feature/python sync actions pagination #183

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python-sync-actions/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ keboola.json-to-csv
mock
freezegun
nested-lookup
python-dateutil
python-dateutil
49 changes: 44 additions & 5 deletions python-sync-actions/src/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,51 @@ def _parse_data(self, data, path) -> list:
value = value[key]
result = value
except KeyError:
raise UserException(f"Path {path.path} not found in the response data")
return [f"Path {path.path} not found in the response data"]

# TODO: check if the result is list
# if not isinstance(result, list):
# element_name = 'root' if path.path == '.' else path.path
# raise UserException(f"The {element_name} element of the response is not list, "
# "please change your Record Selector path to list")
if not isinstance(result, list):
element_name = 'root' if path.path == '.' else path.path
return [f"The {element_name} element of the response is not a list, "
f"please change your Record Selector path to list"]

return result

def _get_paginator(self, job):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tehle metody chybi return type a jmeno je dost zavadejici. Neni to paginator ale pagination parametry, ale chapu ze to je priprava/zbytek snahy mit opravdovy paginator. Taky chybi typehint u parametru funkce

"""
Get paginator object for the given job.

Args:
job: The job for which to get the paginator.

Returns:
The paginator object for the job.
"""

paginator = {}

if job.request_parameters.scroller:
paginator_params = job.api.pagination.get(job.request_parameters.scroller)
if not paginator_params:
raise UserException(f"Paginator '{job.request_parameters.scroller}' not found in the configuration.")

else:
paginator_params = job.api.pagination.get("common")

if paginator_params.get("method") == "offset":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if paginator_params.get("firstPageParams", True):
paginator[paginator_params.get("offsetParam", "offset")] = paginator_params.get("offset", 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady bych osobne preferoval, kdyby si ty jednotlivy metody abstrahoval do trid v Configuration nez davat ten kontext o tom jaky parametry to ma mit do component.py sem. Tj kazdej druh pagination by mel svoji classu

paginator[paginator_params.get("limitParam", "limit")] = paginator_params.get("limit")

elif paginator_params.get("method") == "pagenum":
if paginator_params.get("firstPageParams"):
paginator[paginator_params.get("pageParam", "page")] = paginator_params.get("firstPage", 1)

if paginator_params.get("limit"):
paginator[paginator_params.get("limitParam", "limit")] = paginator_params.get("limit")

return paginator

def make_call(self) -> tuple[list, any, str, str]:
"""
Make call to the API
Expand Down Expand Up @@ -294,6 +329,10 @@ def recursive_call(parent_result, config_index=0):
ssl_verify = api_cfg.ssl_verification
timeout = api_cfg.timeout
# additional_params = self._build_request_parameters(additional_params_cfg)

paginator = self._get_paginator(job)
query_parameters.update(paginator)

request_parameters = {'params': query_parameters,
'headers': new_headers,
'verify': ssl_verify,
Expand Down
15 changes: 13 additions & 2 deletions python-sync-actions/src/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ApiConfig(ConfigurationBase):
base_url: str
default_query_parameters: dict = field(default_factory=dict)
default_headers: dict = field(default_factory=dict)
pagination: dict = field(default_factory=dict)
authentication: Authentication = None
retry_config: RetryConfig = field(default_factory=RetryConfig)
ssl_verification: bool = True
Expand All @@ -83,6 +84,7 @@ class ApiRequest(ConfigurationBase):
query_parameters: dict = field(default_factory=dict)
continue_on_failure: bool = False
nested_job: dict = field(default_factory=dict)
scroller: str = None


@dataclass
Expand Down Expand Up @@ -183,8 +185,14 @@ def convert_to_v2(configuration: dict) -> list[Configuration]:
default_headers = _remove_auth_from_dict(default_headers_org, _return_ui_params(configuration))
default_query_parameters = _remove_auth_from_dict(default_query_parameters_org, _return_ui_params(configuration))

pagination = {}
if api_json.get('pagination', {}).get('scrollers'):
pagination = api_json.get('pagination', {}).get('scrollers')
else:
pagination['common'] = api_json.get('pagination', {})

api_config = ApiConfig(base_url=base_url, default_headers=default_headers,
default_query_parameters=default_query_parameters)
default_query_parameters=default_query_parameters, pagination=pagination)

api_config.retry_config = build_retry_config(configuration)
api_config.authentication = AuthMethodConverter.convert(configuration)
Expand Down Expand Up @@ -285,6 +293,8 @@ def build_api_request(configuration: dict) -> List[Tuple[ApiRequest, RequestCont

placeholders = endpoint_config.get('placeholders', {})

scroller = endpoint_config.get('scroller')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if isinstance(data_field, dict):
path = data_field.get('path')
delimiter = data_field.get("delimiter", ".")
Expand All @@ -297,7 +307,8 @@ def build_api_request(configuration: dict) -> List[Tuple[ApiRequest, RequestCont
endpoint_path=endpoint_path,
placeholders=placeholders,
headers=endpoint_config.get('headers', {}),
query_parameters=endpoint_config.get('params', {}), ),
query_parameters=endpoint_config.get('params', {}),
scroller=scroller),
request_content,
DataPath(path=path, delimiter=delimiter)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@
"dataField": {
"path": ".",
"delimiter": "."
},
"scroller": "events"
}
}
],
"includes": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@
"dataField": {
"path": ".",
"delimiter": "."
},
"scroller": "events"
}
}
],
"includes": {
Expand Down
Loading