Skip to content

Commit

Permalink
Move refresh token to make_request
Browse files Browse the repository at this point in the history
  • Loading branch information
maximearmstrong committed Aug 13, 2024
1 parent 5b35b92 commit f2fefcb
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

DEFAULT_POLL_INTERVAL_SECONDS = 10

# The access token expire every 3 minutes in Airbyte Cloud.
# Refresh is needed after 2.5 minutes to avoid the "token expired" error message.
AIRBYTE_CLOUD_REFRESH_TIMEDELTA_SECONDS = 150


class AirbyteState:
RUNNING = "running"
Expand Down Expand Up @@ -303,6 +307,23 @@ def all_additional_request_params(self) -> Mapping[str, Any]:
}
}

def make_request(
self,
endpoint: str,
data: Optional[Mapping[str, object]] = None,
method: str = "POST",
include_additional_request_params: bool = True,
) -> Optional[Mapping[str, object]]:
# Make sure the access token is refreshed before using it when calling the API.
if include_additional_request_params and self._needs_refreshed_access_token():
self._refresh_access_token()
return super().make_request(
endpoint=endpoint,
data=data,
method=method,
include_additional_request_params=include_additional_request_params,
)

def start_sync(self, connection_id: str) -> Mapping[str, object]:
job_sync = check.not_none(
self.make_request(
Expand Down Expand Up @@ -346,13 +367,13 @@ def _refresh_access_token(self) -> None:
self._access_token_timestamp = datetime.now().timestamp()

def _needs_refreshed_access_token(self) -> bool:
# The access token expire every 3 minutes in Airbyte Cloud.
# Refresh is needed after 2.5 minutes to avoid the "token expired" error message.
return (
not self._access_token_value
or not self._access_token_timestamp
or self._access_token_timestamp
<= datetime.timestamp(datetime.now() - timedelta(seconds=150))
<= datetime.timestamp(
datetime.now() - timedelta(seconds=AIRBYTE_CLOUD_REFRESH_TIMEDELTA_SECONDS)
)
)


Expand Down

0 comments on commit f2fefcb

Please sign in to comment.