Skip to content

Commit

Permalink
Monkey patch the original function(call) of facebook business sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sgandhi1311 committed Apr 5, 2024
1 parent 6fc1b04 commit 178731e
Show file tree
Hide file tree
Showing 575 changed files with 49 additions and 92,332 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
install_requires=[
'attrs==17.3.0',
'backoff==2.2.1',
'facebook_business==19.0.2',
'pendulum==1.2.0',
'requests==2.20.0',
'singer-python==6.0.0',
Expand Down
49 changes: 48 additions & 1 deletion tap_facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import backoff

import sys
sys.path.append('/opt/code/tap-facebook/tap_facebook/')

import re
import singer
import singer.metrics as metrics
from singer import utils, metadata
Expand Down Expand Up @@ -90,6 +90,53 @@

CONFIG = {}

def retry_on_summary_param_error(backoff_type, exception, **wait_gen_kwargs):
"""
At times, the Facebook Graph API exhibits erratic behavior,
triggering errors related to the Summary parameter with a status code of 400.
However, upon retrying, the API functions as expected.
"""
def log_retry_attempt(details):
_, exception, _ = sys.exc_info()
LOGGER.info("Retrying the API call to fix Summary param error")

def should_retry_api_error(exception):

# Define the regular expression pattern
pattern = r'\(#100\) Cannot include [\w, ]+ in summary param because they weren\'t there while creating the report run(?:\. All available values are: )?'
if isinstance(exception, FacebookRequestError):
return (exception.http_status()==400 and re.match(pattern, exception._error['message']))
return False

return backoff.on_exception(
backoff_type,
exception,
jitter=None,
on_backoff=log_retry_attempt,
giveup=lambda exc: not should_retry_api_error(exc),
**wait_gen_kwargs
)

original_call = FacebookAdsApi.call

@retry_on_summary_param_error(backoff.expo, (FacebookRequestError), max_tries=5, factor=5)
def call_with_retry(self, method, path, params=None, headers=None, files=None, url_override=None, api_version=None,):
"""
Adding the retry decorator on the original function call
"""
return original_call(
self,
method,
path,
params,
headers,
files,
url_override,
api_version,)

FacebookAdsApi.call = call_with_retry


class TapFacebookException(Exception):
pass

Expand Down
15 changes: 0 additions & 15 deletions tap_facebook/facebook_business/__init__.py

This file was deleted.

Empty file.
Loading

0 comments on commit 178731e

Please sign in to comment.