-
Notifications
You must be signed in to change notification settings - Fork 47
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
burnash
merged 22 commits into
dlt-hub:master
from
francescomucio:494_restapi_filter_exclude_transform
Aug 16, 2024
Merged
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f64bd42
handle_filter_exclude_transform
francescomucio 5f9835e
fixed typing for callable
francescomucio 65471f5
fixed typing
francescomucio 85dd1d7
fix for linter
francescomucio abe9337
fix linting error
francescomucio c59e40c
added identity functions, and jsonpath instead of string for the excl…
francescomucio 08c29f2
fixing types
francescomucio d9ba2ae
Merge branch 'master' into 494_restapi_filter_exclude_transform
francescomucio d2dcd3c
Merge branch 'dlt-hub:master' into 494_restapi_filter_exclude_transform
francescomucio b7fba2a
used processing_steps instead of multiple arguments
francescomucio 784106b
handle processing_steps with native add_map and add_filter
francescomucio 0ae9523
cleaned up code and added tests
francescomucio c65d26f
fixed a few things removed by mistake
francescomucio 6ed8f11
fixed typing and added a test
francescomucio ad01964
removed unnecessary default
francescomucio 99ff64a
added example of anonymization for a column in the test
francescomucio d376292
clean up code
francescomucio ae894ce
added rule exception for map/filter
francescomucio 83b7373
Update sources/rest_api/__init__.py
burnash 57293eb
Update sources/rest_api/__init__.py
burnash b548656
Remove commented code
burnash 95838a2
Remove commented code
burnash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,188 @@ | ||
import dlt | ||
import pytest | ||
from dlt.sources.helpers.rest_client.paginators import SinglePagePaginator | ||
|
||
from sources.rest_api import rest_api_source, RESTAPIConfig | ||
from tests.utils import ALL_DESTINATIONS, assert_load_info, load_table_counts | ||
|
||
|
||
def _make_pipeline(destination_name: str): | ||
return dlt.pipeline( | ||
pipeline_name="rest_api", | ||
destination=destination_name, | ||
dataset_name="rest_api_data", | ||
full_refresh=True, | ||
) | ||
|
||
|
||
def test_rest_api_source_filtered(mock_api_server) -> None: | ||
config: RESTAPIConfig = { | ||
"client": { | ||
"base_url": "https://api.example.com", | ||
}, | ||
"resources": [ | ||
{ | ||
"name": "posts", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"filter": lambda x: x["id"] == 1}, | ||
], | ||
}, | ||
], | ||
} | ||
mock_source = rest_api_source(config) | ||
|
||
data = list(mock_source.with_resources("posts")) | ||
assert len(data) == 1 | ||
assert data[0]["title"] == "Post 1" | ||
|
||
|
||
def test_rest_api_source_map(mock_api_server) -> None: | ||
|
||
def lower_title(row): | ||
row["title"] = row["title"].lower() | ||
return row | ||
|
||
config: RESTAPIConfig = { | ||
"client": { | ||
"base_url": "https://api.example.com", | ||
}, | ||
"resources": [ | ||
{ | ||
"name": "posts", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"map": lower_title}, | ||
], | ||
}, | ||
], | ||
} | ||
mock_source = rest_api_source(config) | ||
|
||
data = list(mock_source.with_resources("posts")) | ||
|
||
assert all(record["title"].startswith("post ") for record in data) | ||
|
||
|
||
def test_rest_api_source_filter_and_map(mock_api_server) -> None: | ||
|
||
def id_by_10(row): | ||
row["id"] = row["id"] * 10 | ||
return row | ||
|
||
config: RESTAPIConfig = { | ||
"client": { | ||
"base_url": "https://api.example.com", | ||
}, | ||
"resources": [ | ||
{ | ||
"name": "posts", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"map": id_by_10}, | ||
{"filter": lambda x: x["id"] == 10}, | ||
], | ||
}, | ||
{ | ||
"name": "posts_2", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"filter": lambda x: x["id"] == 10}, | ||
{"map": id_by_10}, | ||
], | ||
}, | ||
], | ||
} | ||
mock_source = rest_api_source(config) | ||
|
||
data = list(mock_source.with_resources("posts")) | ||
assert len(data) == 1 | ||
assert data[0]["title"] == "Post 1" | ||
|
||
data = list(mock_source.with_resources("posts_2")) | ||
assert len(data) == 1 | ||
assert data[0]["id"] == 100 | ||
assert data[0]["title"] == "Post 10" | ||
|
||
|
||
def test_rest_api_source_filtered_child(mock_api_server) -> None: | ||
config: RESTAPIConfig = { | ||
"client": { | ||
"base_url": "https://api.example.com", | ||
}, | ||
"resources": [ | ||
{ | ||
"name": "posts", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"filter": lambda x: x["id"] in (1, 2)}, | ||
], | ||
}, | ||
{ | ||
"name": "comments", | ||
"endpoint": { | ||
"path": "/posts/{post_id}/comments", | ||
"params": { | ||
"post_id": { | ||
"type": "resolve", | ||
"resource": "posts", | ||
"field": "id", | ||
} | ||
}, | ||
}, | ||
"processing_steps": [ | ||
{"filter": lambda x: x["id"] == 1}, | ||
], | ||
}, | ||
], | ||
} | ||
mock_source = rest_api_source(config) | ||
|
||
data = list(mock_source.with_resources("comments")) | ||
assert len(data) == 2 | ||
# assert data[0]["title"] == "Post 1" | ||
burnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_rest_api_source_filtered_and_map_child(mock_api_server) -> None: | ||
|
||
def extend_body(row): | ||
row["body"] = f"{row['_posts_title']} - {row['body']}" | ||
return row | ||
|
||
config: RESTAPIConfig = { | ||
"client": { | ||
"base_url": "https://api.example.com", | ||
}, | ||
"resources": [ | ||
{ | ||
"name": "posts", | ||
"endpoint": "posts", | ||
"processing_steps": [ | ||
{"filter": lambda x: x["id"] in (1, 2)}, | ||
], | ||
}, | ||
{ | ||
"name": "comments", | ||
"endpoint": { | ||
"path": "/posts/{post_id}/comments", | ||
"params": { | ||
"post_id": { | ||
"type": "resolve", | ||
"resource": "posts", | ||
"field": "id", | ||
} | ||
}, | ||
}, | ||
"include_from_parent": ["title"], | ||
"processing_steps": [ | ||
{"map": extend_body}, | ||
{"filter": lambda x: x["body"].startswith("Post 2")}, | ||
], | ||
}, | ||
], | ||
} | ||
mock_source = rest_api_source(config) | ||
|
||
data = list(mock_source.with_resources("comments")) | ||
# assert len(data) == 1 | ||
burnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert data[0]["body"] == "Post 2 - Comment 0 for post 2" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
awesome!