From 2c903ec486aada9dbb9ae07ae73a9d1391328b7d Mon Sep 17 00:00:00 2001 From: Leo Schick <67712864+leo-schick@users.noreply.github.com> Date: Fri, 10 Mar 2023 18:02:14 +0100 Subject: [PATCH] support reading "expires_in" when the API passes the value as string (#23921) Co-authored-by: Sherif A. Nada --- .../airbyte_cdk/sources/streams/http/auth/oauth.py | 2 +- .../unit_tests/sources/streams/http/auth/test_auth.py | 5 +++-- .../requests_native_auth/test_requests_native_auth.py | 11 +++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/airbyte-cdk/python/airbyte_cdk/sources/streams/http/auth/oauth.py b/airbyte-cdk/python/airbyte_cdk/sources/streams/http/auth/oauth.py index e6ea4e27d108..343884e3c3ad 100644 --- a/airbyte-cdk/python/airbyte_cdk/sources/streams/http/auth/oauth.py +++ b/airbyte-cdk/python/airbyte_cdk/sources/streams/http/auth/oauth.py @@ -95,7 +95,7 @@ def refresh_access_token(self) -> Tuple[str, int]: ) response.raise_for_status() response_json = response.json() - return response_json["access_token"], response_json["expires_in"] + return response_json["access_token"], int(response_json["expires_in"]) except requests.exceptions.RequestException as e: if e.response.status_code == 429 or e.response.status_code >= 500: raise DefaultBackoffException(request=e.response.request, response=e.response) diff --git a/airbyte-cdk/python/unit_tests/sources/streams/http/auth/test_auth.py b/airbyte-cdk/python/unit_tests/sources/streams/http/auth/test_auth.py index 3191332580f2..7a664ac841a2 100644 --- a/airbyte-cdk/python/unit_tests/sources/streams/http/auth/test_auth.py +++ b/airbyte-cdk/python/unit_tests/sources/streams/http/auth/test_auth.py @@ -136,9 +136,10 @@ def test_refresh_access_token(self, requests_mock): refresh_access_token_headers=TestOauth2Authenticator.refresh_access_token_headers, ) - token = oauth.refresh_access_token() + token, expires_in = oauth.refresh_access_token() - assert ("token", 10) == token + assert isinstance(expires_in, int) + assert ("token", 10) == (token, expires_in) for header in self.refresh_access_token_headers: assert header in mock_refresh_token_call.last_request.headers assert self.refresh_access_token_headers[header] == mock_refresh_token_call.last_request.headers[header] diff --git a/airbyte-cdk/python/unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py b/airbyte-cdk/python/unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py index 97e37960a719..6724dd0343d6 100644 --- a/airbyte-cdk/python/unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py +++ b/airbyte-cdk/python/unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py @@ -159,15 +159,17 @@ def test_refresh_access_token(self, mocker): resp.status_code = 200 mocker.patch.object(resp, "json", return_value={"access_token": "access_token", "expires_in": 1000}) mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True) - token = oauth.refresh_access_token() + token, expires_in = oauth.refresh_access_token() - assert ("access_token", 1000) == token + assert isinstance(expires_in, int) + assert ("access_token", 1000) == (token, expires_in) # Test with expires_in as str mocker.patch.object(resp, "json", return_value={"access_token": "access_token", "expires_in": "2000"}) - token = oauth.refresh_access_token() + token, expires_in = oauth.refresh_access_token() - assert ("access_token", 2000) == token + assert isinstance(expires_in, int) + assert ("access_token", 2000) == (token, expires_in) @pytest.mark.parametrize("error_code", (429, 500, 502, 504)) def test_refresh_access_token_retry(self, error_code, requests_mock): @@ -184,6 +186,7 @@ def test_refresh_access_token_retry(self, error_code, requests_mock): ] ) token, expires_in = oauth.refresh_access_token() + assert isinstance(expires_in, int) assert (token, expires_in) == ("token", 10) assert requests_mock.call_count == 3