api: Clean up code duplication and add automatic retries on 503s.

(imported from commit 5c0654d1048a97a0f451c560544266a4261d9426)
This commit is contained in:
Tim Abbott 2012-10-04 18:06:54 -04:00
parent 283b973bc5
commit 0867c25d51

View file

@ -7,6 +7,9 @@ import traceback
# TODO: Drop verify=False once we have real certificates # TODO: Drop verify=False once we have real certificates
# Or switch to specifying a testing cert manually # Or switch to specifying a testing cert manually
# Check that we have a recent enough version
assert(requests.__version__ > '0.12')
class HumbugAPI(): class HumbugAPI():
def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"): def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"):
self.api_key = api_key self.api_key = api_key
@ -14,38 +17,38 @@ class HumbugAPI():
self.verbose = verbose self.verbose = verbose
self.base_url = site self.base_url = site
def send_message(self, submit_hash): def do_api_query(self, request, url):
submit_hash["email"] = self.email request["email"] = self.email
submit_hash["api-key"] = self.api_key request["api-key"] = self.api_key
try: while True:
res = requests.post(self.base_url + "/api/v1/send_message", try:
data=submit_hash, res = requests.post(self.base_url + url,
verify=False, data=request,
auth=requests.auth.HTTPDigestAuth('tabbott', 'xxxxxxxxxxxxxxxxx')) verify=False,
# TODO: Add some sort of automated retry for certain errors auth=requests.auth.HTTPDigestAuth('tabbott',
except requests.exceptions.ConnectionError: 'xxxxxxxxxxxxxxxxx'))
return {'msg': "Connection error\n%s" % traceback.format_exc(), if res.status_code == requests.codes.service_unavailable:
"result": "connection-error"} # On 503 errors, try again after a short sleep
if res.json is not None: time.sleep(0.5)
return res.json continue
return {'msg': res.text, "result": "unexpected-error", except requests.exceptions.ConnectionError:
"status_code": res.status_code} return {'msg': "Connection error:\n%s" % traceback.format_exc(),
"result": "connection-error"}
except Exception:
# we'll split this out into more cases as we encounter new bugs.
return {'msg': "Unexpected error:\n%s" % traceback.format_exc(),
"result": "unexpected-error"}
def get_messages(self, options = {}): if res.json is not None:
options["email"] = self.email return res.json
options["api-key"] = self.api_key return {'msg': res.text, "result": "http-error",
try: "status_code": res.status_code}
res = requests.post(self.base_url + "/api/v1/get_messages",
data=options, def send_message(self, request):
verify=False, return self.do_api_query(request, "/api/v1/send_message")
auth=requests.auth.HTTPDigestAuth('tabbott', 'xxxxxxxxxxxxxxxxx'))
except requests.exceptions.ConnectionError: def get_messages(self, request = {}):
return {'msg': "Connection error\n%s" % traceback.format_exc(), return self.do_api_query(request, "/api/v1/get_messages")
"result": "connection-error"}
if res.json is not None:
return res.json
return {'msg': res.text, "result": "unexpected-error",
"status_code": res.status_code}
def call_on_each_message(self, callback, options = {}): def call_on_each_message(self, callback, options = {}):
max_message_id = None max_message_id = None
@ -56,10 +59,10 @@ class HumbugAPI():
res = self.get_messages(options) res = self.get_messages(options)
if 'error' in res.get('result'): if 'error' in res.get('result'):
if self.verbose: if self.verbose:
if res["result"] == "unexpected-error": if res["result"] == "http-error":
print "Unexpected error -- probably a server restart" print "Unexpected error -- probably a server restart"
elif res["result"] == "connection-error": elif res["result"] == "connection-error":
print "Connection error -- probably server is down?" print "Connection error -- probably server is temporarily down?"
else: else:
print "Server returned error:\n%s" % res["msg"] print "Server returned error:\n%s" % res["msg"]
# TODO: Make this back off once it's more reliable # TODO: Make this back off once it's more reliable