Open JBarti opened 1 week ago
We've experienced the exact same issue on occasion as the status code isn't being acted on, even 5xx codes.
Think the simplest and cleanest solution might be to implement a proper Retry strategy directly at the low-level API request call being made: https://github.com/facebook/facebook-python-business-sdk/blob/0a8f34741eee83fbce4426b48312f7dc52a1265e/facebook_business/api.py#L301-L317
from urllib3 import Retry
from requests import Session()
from requests.adapters import HTTPAdapter
DEFAULT_RETRIES = 5 # Allows configuration.
DEFAULT_BACKOFF_FACTOR = 0.5 # Time doubles between API calls.
session = Session()
retry = Retry(
total=DEFAULT_RETRIES,
# Matches `settings.RETRY_HTTP_CODES` in lyst/checkout.
status_forcelist=[
HTTP_408_REQUEST_TIMEOUT,
HTTP_429_TOO_MANY_REQUESTS,
HTTP_500_INTERNAL_SERVER_ERROR,
HTTP_503_SERVICE_UNAVAILABLE,
HTTP_504_GATEWAY_TIMEOUT,
],
backoff_factor=DEFAULT_BACKOFF_FACTOR,
respect_retry_after_header=True,
)
adapter = HTTPAdapter(max_retries=retry)
# Multiple `mount`s shouldn't be an issue since it's just updating a dict key.
# Preferably declare them by descending prefix length as per below.
session.mount("https://", adapter)
session.mount("http://", adapter)
Happy to raise a PR if useful
While trying to gather all adcreatives from a customer ad account I managed to trigger an unexpected error in the Ads API. The library handles this response incorrectly by not raising an error even though the response status is 400.
Details
After around 10 pages are retrieved facebook returns the body in HTML format with a status code of 400:
is_success
method in thefacebook_business.api
module fails to identify this response as a failure and returnsTrue
.Code
The code provided encounters this issue when accessing the
/act_<account_id>/adcreatives
endpoint. Here's a breakdown of what the code does: