Skip to content

Commit

Permalink
Fix/1849 Do Not Parse Ignored Empty Responses (#1851)
Browse files Browse the repository at this point in the history
* output response text rather trying to serialize to json
* Add test for ignoring no-content results
  • Loading branch information
TheOneTrueAnt authored Sep 24, 2024
1 parent db4f445 commit dcb1f0e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 1 addition & 2 deletions dlt/sources/rest_api/config_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@ def response_action_hook(response: Response, *args: Any, **kwargs: Any) -> None:
hook(response)
elif action_type == "ignore":
logger.info(
f"Ignoring response with code {response.status_code} "
f"and content '{response.json()}'."
f"Ignoring response with code {response.status_code} and content '{response.text}'."
)
raise IgnoreResponseException

Expand Down
10 changes: 10 additions & 0 deletions tests/sources/rest_api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ def post_detail_404(request, context):
context.status_code = 404
return {"error": "Post not found"}

@router.get(r"/posts/\d+/some_details_204")
def post_detail_204(request, context):
"""Return 204 No Content for post with id > 0. Used to test ignoring 204 responses."""
post_id = int(request.url.split("/")[-2])
if post_id < 1:
return {"id": post_id, "body": f"Post body {post_id}"}
else:
context.status_code = 204
return None

@router.get(r"/posts_under_a_different_key$")
def posts_with_results_key(request, context):
return paginate_by_page_number(request, generate_posts(), records_key="many-results")
Expand Down
40 changes: 40 additions & 0 deletions tests/sources/rest_api/integration/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,46 @@ def test_ignoring_endpoint_returning_404(mock_api_server):
]


def test_ignoring_endpoint_returning_204(mock_api_server):
mock_source = rest_api_source(
{
"client": {"base_url": "https://api.example.com"},
"resources": [
"posts",
{
"name": "post_details",
"endpoint": {
"path": "posts/{post_id}/some_details_204",
"params": {
"post_id": {
"type": "resolve",
"resource": "posts",
"field": "id",
}
},
"response_actions": [
{
"status_code": 204,
"action": "ignore",
},
],
},
},
],
}
)

res = list(mock_source.with_resources("posts", "post_details").add_limit(1))

assert res[:5] == [
{"id": 0, "body": "Post body 0"},
{"id": 0, "title": "Post 0"},
{"id": 1, "title": "Post 1"},
{"id": 2, "title": "Post 2"},
{"id": 3, "title": "Post 3"},
]


def test_source_with_post_request(mock_api_server):
class JSONBodyPageCursorPaginator(BaseReferencePaginator):
def update_state(self, response: Response, data: Optional[List[Any]] = None) -> None:
Expand Down

0 comments on commit dcb1f0e

Please sign in to comment.