Skip to content

Commit ccac289

Browse files
committed
minimal change to insert default expiration
1 parent 3e82436 commit ccac289

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

airbyte_cdk/sources/declarative/auth/oauth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def get_token_expiry_date(self) -> AirbyteDateTime:
239239
def _has_access_token_been_initialized(self) -> bool:
240240
return self._access_token is not None
241241

242-
def set_token_expiry_date(self, value: Union[str, int]) -> None:
242+
def set_token_expiry_date(self, value: AirbyteDateTime) -> None:
243243
self._token_expiry_date = self._parse_token_expiration_date(value)
244244

245245
def get_assertion_name(self) -> str:

airbyte_cdk/sources/streams/http/requests_native_auth/abstract_oauth.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def build_refresh_request_headers(self) -> Mapping[str, Any] | None:
130130
headers = self.get_refresh_request_headers()
131131
return headers if headers else None
132132

133-
def refresh_access_token(self) -> Tuple[str, Union[str, int]]:
133+
def refresh_access_token(self) -> Tuple[str, Optional[str]]:
134134
"""
135135
Returns the refresh token and its expiration datetime
136136
@@ -147,6 +147,15 @@ def refresh_access_token(self) -> Tuple[str, Union[str, int]]:
147147
# ----------------
148148
# PRIVATE METHODS
149149
# ----------------
150+
151+
def _default_token_expiry_date(self) -> str:
152+
"""
153+
Returns the default token expiry date
154+
"""
155+
if self.token_expiry_is_time_of_expiration:
156+
return str(ab_datetime_now() + timedelta(hours=1))
157+
else:
158+
return "3600"
150159

151160
def _wrap_refresh_token_exception(
152161
self, exception: requests.exceptions.RequestException
@@ -316,9 +325,15 @@ def _extract_token_expiry_date(self, response_data: Mapping[str, Any]) -> Any:
316325
response_data (Mapping[str, Any]): The response data from which to extract the token_expiry_date.
317326
318327
Returns:
319-
str: The extracted token_expiry_date.
328+
The extracted token_expiry_date or None if not found.
320329
"""
321-
return self._find_and_get_value_from_response(response_data, self.get_expires_in_name())
330+
expires_in = self._find_and_get_value_from_response(response_data, self.get_expires_in_name())
331+
# If the access token expires in is None, we do not know when the token will expire
332+
# 1 hour was chosen as a middle ground to avoid unnecessary frequent refreshes and token expiration
333+
if expires_in is None:
334+
return self._default_token_expiry_date()
335+
else:
336+
return expires_in
322337

323338
def _find_and_get_value_from_response(
324339
self,

airbyte_cdk/sources/streams/http/requests_native_auth/oauth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def get_access_token(self) -> str:
355355
self._emit_control_message()
356356
return self.access_token
357357

358-
def refresh_access_token(self) -> Tuple[str, str, str]: # type: ignore[override]
358+
def refresh_access_token(self) -> Tuple[str, Optional[str], str]: # type: ignore[override]
359359
"""
360360
Refreshes the access token by making a handled request and extracting the necessary token information.
361361

0 commit comments

Comments
 (0)