facebook-python-business-sdk
facebook-python-business-sdk copied to clipboard
Fix FacebookResponse.is_success to properly handle API returns in csv format
The SDK breaks when trying to run an API call that returns a string body (in csv format, for example).
This is what happens:
json_body = self.json()
- it fails to convert the body from string to dict, so it returns the string body itself.
if isinstance(json_body, collections_abc.Mapping) and 'error' in json_body:
- if fails because it checks if
json_bodyis a dict when looking for a dictionary key in it
elif bool(json_body):
- it's basically just checking that
json_bodyis not empty (str or dict) or False.
if 'success' in json_body:
- it doesn't check
json_bodyis a dict - whenever
json_bodyis a string the code is looking for a substring instead of a dictionary key
return json_body['success']
- if the string body contains 'success', it will throw an exception when trying to use a string to index a string
- I've seen this happening when trying to read ad insights with 'success' in a campaign name
return 'Service Unavailable' not in json_body
- if the string body doesn't contain 'success' but it contains 'Service Unavailable', for any reason, this would return False even if the API call was successful.
@stcheng has imported this pull request. If you are a Meta employee, you can view this in D86377538.