From f861c085645e97a415cc56deffb4dd96405847c5 Mon Sep 17 00:00:00 2001 From: Willi Date: Wed, 24 Jul 2024 16:51:03 +0530 Subject: [PATCH 1/2] rest_api paginator: Allows using json_response in place of json_link --- sources/rest_api/config_setup.py | 3 ++- sources/rest_api/typing.py | 3 ++- tests/rest_api/test_configurations.py | 28 ++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/sources/rest_api/config_setup.py b/sources/rest_api/config_setup.py index dfbb98fb7..021025f3a 100644 --- a/sources/rest_api/config_setup.py +++ b/sources/rest_api/config_setup.py @@ -37,7 +37,7 @@ try: from dlt.sources.helpers.rest_client.paginators import JSONLinkPaginator except ImportError: - from dlt.sources.helpers.rest_client.paginators import JSONResponsePaginator as JSONLinkPaginator # type: ignore + from dlt.sources.helpers.rest_client.paginators import JSONResponsePaginator as JSONLinkPaginator # type: ignore from dlt.sources.helpers.rest_client.detector import single_entity_path from dlt.sources.helpers.rest_client.exceptions import IgnoreResponseException @@ -66,6 +66,7 @@ PAGINATOR_MAP: Dict[PaginatorType, Type[BasePaginator]] = { "json_link": JSONLinkPaginator, + "json_response": JSONLinkPaginator, # deprecated. Use json_link instead. Will be removed in upcoming release "header_link": HeaderLinkPaginator, "auto": None, "single_page": SinglePagePaginator, diff --git a/sources/rest_api/typing.py b/sources/rest_api/typing.py index 871ccd1a0..4c3d258fb 100644 --- a/sources/rest_api/typing.py +++ b/sources/rest_api/typing.py @@ -38,7 +38,7 @@ try: from dlt.sources.helpers.rest_client.paginators import JSONLinkPaginator except ImportError: - from dlt.sources.helpers.rest_client.paginators import JSONResponsePaginator as JSONLinkPaginator # type: ignore + from dlt.sources.helpers.rest_client.paginators import JSONResponsePaginator as JSONLinkPaginator # type: ignore from dlt.sources.helpers.rest_client.auth import ( AuthConfigBase, @@ -49,6 +49,7 @@ PaginatorType = Literal[ "json_link", + "json_response", # deprecated. Use json_link instead. Will be removed in upcoming release "header_link", "auto", "single_page", diff --git a/tests/rest_api/test_configurations.py b/tests/rest_api/test_configurations.py index d41eb4288..6ee87a6ca 100644 --- a/tests/rest_api/test_configurations.py +++ b/tests/rest_api/test_configurations.py @@ -60,7 +60,9 @@ try: from dlt.sources.helpers.rest_client.paginators import JSONLinkPaginator except ImportError: - from dlt.sources.helpers.rest_client.paginators import JSONResponsePaginator as JSONLinkPaginator + from dlt.sources.helpers.rest_client.paginators import ( + JSONResponsePaginator as JSONLinkPaginator, + ) from dlt.sources.helpers.rest_client.auth import ( @@ -162,6 +164,30 @@ def test_page_number_paginator_creation() -> None: pytest.fail("DictValidationException was unexpectedly raised") +def test_json_link_paginator(mock_api_server) -> None: + """ + Delete this test as soon as we stop supporting the deprecated key json_response + for the JSONLinkPaginator + """ + config: RESTAPIConfig = { # type: ignore + "client": {"base_url": "https://api.example.com"}, + "resources": [ + { + "name": "posts", + "endpoint": { + "path": "posts", + "paginator": { + "type": "json_response", + "next_url_path": "links.next", + }, + }, + }, + ], + } + + rest_api_source(config) + + @pytest.mark.parametrize("auth_type", get_args(AuthType)) @pytest.mark.parametrize( "section", ("SOURCES__REST_API__CREDENTIALS", "SOURCES__CREDENTIALS", "CREDENTIALS") From ac6be0311f2ecff3d5faf8556dd04fcad6de01fd Mon Sep 17 00:00:00 2001 From: Willi Date: Wed, 24 Jul 2024 16:58:17 +0530 Subject: [PATCH 2/2] renames test for clarity --- tests/rest_api/test_configurations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rest_api/test_configurations.py b/tests/rest_api/test_configurations.py index 6ee87a6ca..59509c59e 100644 --- a/tests/rest_api/test_configurations.py +++ b/tests/rest_api/test_configurations.py @@ -164,7 +164,7 @@ def test_page_number_paginator_creation() -> None: pytest.fail("DictValidationException was unexpectedly raised") -def test_json_link_paginator(mock_api_server) -> None: +def test_allow_deprecated_json_response_paginator(mock_api_server) -> None: """ Delete this test as soon as we stop supporting the deprecated key json_response for the JSONLinkPaginator