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

rest_api: filter, exclude, transform API responses #495

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f64bd42
handle_filter_exclude_transform
francescomucio Jun 12, 2024
5f9835e
fixed typing for callable
francescomucio Jun 12, 2024
65471f5
fixed typing
francescomucio Jun 13, 2024
85dd1d7
fix for linter
francescomucio Jun 13, 2024
abe9337
fix linting error
francescomucio Jun 13, 2024
c59e40c
added identity functions, and jsonpath instead of string for the excl…
francescomucio Jun 14, 2024
08c29f2
fixing types
francescomucio Jun 14, 2024
d9ba2ae
Merge branch 'master' into 494_restapi_filter_exclude_transform
francescomucio Jul 15, 2024
d2dcd3c
Merge branch 'dlt-hub:master' into 494_restapi_filter_exclude_transform
francescomucio Aug 11, 2024
b7fba2a
used processing_steps instead of multiple arguments
francescomucio Aug 12, 2024
784106b
handle processing_steps with native add_map and add_filter
francescomucio Aug 12, 2024
0ae9523
cleaned up code and added tests
francescomucio Aug 15, 2024
c65d26f
fixed a few things removed by mistake
francescomucio Aug 15, 2024
6ed8f11
fixed typing and added a test
francescomucio Aug 15, 2024
ad01964
removed unnecessary default
francescomucio Aug 15, 2024
99ff64a
added example of anonymization for a column in the test
francescomucio Aug 15, 2024
d376292
clean up code
francescomucio Aug 15, 2024
ae894ce
added rule exception for map/filter
francescomucio Aug 15, 2024
83b7373
Update sources/rest_api/__init__.py
burnash Aug 16, 2024
57293eb
Update sources/rest_api/__init__.py
burnash Aug 16, 2024
b548656
Remove commented code
burnash Aug 16, 2024
95838a2
Remove commented code
burnash Aug 16, 2024
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
30 changes: 27 additions & 3 deletions sources/rest_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def create_resources(
request_params = endpoint_config.get("params", {})
request_json = endpoint_config.get("json", None)
paginator = create_paginator(endpoint_config.get("paginator"))
row_filter = endpoint_resource.pop("filter", None)
francescomucio marked this conversation as resolved.
Show resolved Hide resolved
transform = endpoint_resource.pop("transform", None)
exclude_columns = endpoint_resource.pop("exclude_columns", [])

resolved_param: ResolvedParam = resolved_param_map[resource_name]

Expand Down Expand Up @@ -254,21 +257,39 @@ def paginate_resource(
client: RESTClient = client,
incremental_object: Optional[Incremental[Any]] = incremental_object,
incremental_param: IncrementalParam = incremental_param,
row_filter: Any = row_filter,
transform: Any = transform,
exclude_columns: List[str] = exclude_columns,
) -> Generator[Any, None, None]:
def exclude_elements(item: Any, exclude_columns: List[str]) -> Any:
for key in exclude_columns:
del item[key]
francescomucio marked this conversation as resolved.
Show resolved Hide resolved
return item

if incremental_object:
params[incremental_param.start] = incremental_object.last_value
if incremental_param.end:
params[incremental_param.end] = incremental_object.end_value

yield from client.paginate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to keep the from statement.

for page in client.paginate(
francescomucio marked this conversation as resolved.
Show resolved Hide resolved
burnash marked this conversation as resolved.
Show resolved Hide resolved
method=method,
path=path,
params=params,
json=json,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it a typo or there's a reason to remove json param?

paginator=paginator,
data_selector=data_selector,
hooks=hooks,
)
):
filtered_page = filter(row_filter, page) if row_filter else page
francescomucio marked this conversation as resolved.
Show resolved Hide resolved
transformed_page = (
map(transform, filtered_page) if transform else filtered_page
)
final_page = (
map(exclude_elements, transformed_page, exclude_columns)
if exclude_elements
else transformed_page
)

yield final_page

resources[resource_name] = dlt.resource(
paginate_resource,
Expand All @@ -281,6 +302,9 @@ def paginate_resource(
paginator=paginator,
data_selector=endpoint_config.get("data_selector"),
hooks=hooks,
row_filter=row_filter,
transform=transform,
exclude_columns=exclude_columns,
)

else:
Expand Down
4 changes: 4 additions & 0 deletions sources/rest_api/typing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Expand Down Expand Up @@ -243,6 +244,9 @@ class ResourceBase(TypedDict, total=False):
table_format: Optional[TTableHintTemplate[TTableFormat]]
selected: Optional[bool]
parallelized: Optional[bool]
row_filter: Optional[Callable[[Any], None]]
exclude_columns: Optional[List[str]]
transform: Optional[Callable[[Any], None]]


class EndpointResourceBase(ResourceBase, total=False):
Expand Down
Loading