Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add warning for access expiration date of 1 week #201

Merged
merged 9 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from dataclasses import dataclass
from time import sleep
from typing import Optional

import backoff
import pendulum
Expand Down Expand Up @@ -193,6 +194,17 @@ def call(
self._handle_call_rate_limit(response, params)
return response

def get_access_token_expiration(self) -> Optional[int]:
"""Get access token expiration time"""
try:
response = self.call("GET", "https://graph.facebook.com/debug_token", params={"input_token": MyFacebookAdsApi._access_token})
response_body = response.json()
return response_body.get("data", {}).get("expires_at", None)
except FacebookRequestError:
return None
except requests.exceptions.ConnectionError:
return None


class API:
"""Simple wrapper around Facebook API"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def read_records(
if job.interval.start == self._next_cursor_value:
self._advance_cursor()

expires_at = self._api.api.get_access_token_expiration()
if expires_at and pendulum.from_timestamp(expires_at) - pendulum.now() < pendulum.duration(days=7):
self.logger.warning(f"Access token expires at {pendulum.from_timestamp(expires_at)}. Please reconnect the source.")

@property
def state(self) -> MutableMapping[str, Any]:
"""State getter, the result can be stored by the source"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def read_records(
else:
yield record # execute_in_batch will emmit dicts

expires_at = self._api.api.get_access_token_expiration()
if expires_at and pendulum.from_timestamp(expires_at) - pendulum.now() < pendulum.duration(days=7):
self.logger.info(f"Access token expires at {pendulum.from_timestamp(expires_at)}")

@abstractmethod
def list_objects(self, params: Mapping[str, Any]) -> Iterable:
"""List FB objects, these objects will be loaded in read_records later with their details.
Expand Down Expand Up @@ -281,3 +285,7 @@ def read_records(
yield record.export_all_data()

self._cursor_value = self._max_cursor_value

expires_at = self._api.api.get_access_token_expiration()
if expires_at and pendulum.from_timestamp(expires_at) - pendulum.now() < pendulum.duration(days=7):
self.logger.info(f"Access token expires at {pendulum.from_timestamp(expires_at)}")
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def read_records(
record["thumbnail_data_url"] = fetch_thumbnail_data_url(thumbnail_url)
yield record

expires_at = self._api.api.get_access_token_expiration()
if expires_at and pendulum.from_timestamp(expires_at) - pendulum.now() < pendulum.duration(days=7):
self.logger.info(f"Access token expires at {pendulum.from_timestamp(expires_at)}")

def list_objects(self, params: Mapping[str, Any]) -> Iterable:
return self._api.account.get_ad_creatives(params=params)

Expand Down
Loading