I'm currently working with Google's BigQuery API, which when called, occasionally gives me:
apiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/bigquery/v2/projects/some_job?alt=json returned "Unexpected. Please try again.">
It's kind of a silly thing to return, but anyway, when I get this for any method called, I'd want to just sleep a second or two and then try again. Basically, I'd want to wrap every method with something like:
def new_method
try:
method()
except apiclient.errors.HttpError, e:
if e.resp.status == 500:
sleep(2)
new_method()
else:
raise e
What's a good way of doing this?
I don't want to explicitly redefine every method in the class. I just want to apply something automatically to every method in the class, so I'm covered for the future. Ideally, I'd take a class object, o, and make a wrapper around it that redefines every method in the class with this try except wrapper so I get some new object, p, that automatically retries when it gets a 500 error.