facebook / facebook-python-business-sdk

Python SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
1.28k stars 631 forks source link

Add success and failure callbacks to get_insights call #534

Closed igsoblechero closed 5 years ago

igsoblechero commented 5 years ago

Important note: I know that the changes I propose are made in an auto-generated class; I just need these changes to be done in the automated way there is and then just discard my pull request.

The get_insights call doesn't allow receiving success and failure callbacks for the batch process, and therefore I cannot batch these calls and get all the data as it is done in this example, line 140, for creating ads: https://github.com/facebook/facebook-python-business-sdk/blob/master/examples/ad_creation_utils.py

As you may know, the batch object is already prepared to receive several callbacks in the request.add_to_batch call, so I'm not really proposing a change on your inner features but just having this feature in the get_insights method.

In case this is feature is not intended to be used this way and, therefore, the changes make no sense, I'd like to know how should I add these callbacks -or, otherwise, how should I manage all the Facebook responses in just one callback.

bra-fsn commented 5 years ago

This issue is present not only with the getinsights, but all methods which has the batch parameter without success and failure (basically all except remote* methods), making all other methods unusable for batches. Solving this is very easy, I wonder why it hasn't yet happened. Until this is done, you can use something like this as a workaround:

    def callback_success(response):
        print("Success", response.body())
    callback_success = partial(callback_success)

    def callback_failure(response):
        print("Failed", response)
    callback_failure = partial(callback_failure)

    r = account.get_ad_creatives(fields=['object_story_spec'], pending=True)
    batch.add_request(r, success=callback_success, failure=callback_failure)

So you call the desired method with pending=True (instead of batch=batch), which just gives back the FacebookRequest object, which you can manually add to the batch with callbacks.

jingping2015 commented 5 years ago

Thanks for the feedback, we will fix this in our next release.

igsoblechero commented 5 years ago

Thank you two! The workaround is working fine, so I'm also happy with that.

jingping2015 commented 5 years ago

Thanks for the feedback, since this already be fixed in new release, I am going to close this request.