From 9afae5d227510e7399b5c629e2b5b290c67385ee Mon Sep 17 00:00:00 2001 From: Alex Bair Date: Fri, 22 Nov 2024 12:57:44 -0500 Subject: [PATCH] source-facebook-marketing: validate account ids --- .../source_facebook_marketing/source.py | 4 +++- .../source_facebook_marketing/utils.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/source-facebook-marketing/source_facebook_marketing/source.py b/source-facebook-marketing/source_facebook_marketing/source.py index d767460fb4..f44a432fac 100644 --- a/source-facebook-marketing/source_facebook_marketing/source.py +++ b/source-facebook-marketing/source_facebook_marketing/source.py @@ -34,7 +34,7 @@ Videos, ) -from .utils import validate_end_date, validate_start_date +from .utils import validate_end_date, validate_start_date, validate_account_ids logger = logging.getLogger("airbyte") UNSUPPORTED_FIELDS = {"unique_conversions", "unique_ctr", "unique_clicks", @@ -96,6 +96,8 @@ def streams(self, config: Mapping[str, Any]) -> List[Type[Stream]]: api = API(access_token=config.credentials.access_token) + validate_account_ids(api, config.account_ids) + insights_args = dict( api=api, start_date=config.start_date, end_date=config.end_date, insights_lookback_window=config.insights_lookback_window, account_ids=config.account_ids, ) diff --git a/source-facebook-marketing/source_facebook_marketing/utils.py b/source-facebook-marketing/source_facebook_marketing/utils.py index b0a52abf4a..b02a10cc02 100644 --- a/source-facebook-marketing/source_facebook_marketing/utils.py +++ b/source-facebook-marketing/source_facebook_marketing/utils.py @@ -3,9 +3,13 @@ # import logging +from typing import List import pendulum from pendulum import DateTime +from estuary_cdk.flow import ValidationError + +from source_facebook_marketing.api import API, FacebookAPIException logger = logging.getLogger("airbyte") @@ -45,3 +49,15 @@ def validate_end_date(start_date: DateTime, end_date: DateTime) -> DateTime: logger.warning(message) return start_date return end_date + +def validate_account_ids(api: API, account_ids: List[str]): + errs = [] + for account_id in account_ids: + try: + api._find_account(account_id) + except FacebookAPIException as err: + msg = f"Error when validating account ID {account_id}: {err}" + errs.append(msg) + + if len(errs) > 0: + raise ValidationError(errs)