2012-10-01 15:36:44 -04:00
|
|
|
#!/usr/bin/python
|
2012-11-02 10:40:13 -04:00
|
|
|
# Copyright (C) 2012 Humbug, Inc. All rights reserved.
|
2012-10-01 15:36:44 -04:00
|
|
|
import simplejson
|
2012-10-04 16:13:47 -04:00
|
|
|
import requests
|
2012-10-01 15:36:44 -04:00
|
|
|
import time
|
2012-10-02 15:47:59 -04:00
|
|
|
import traceback
|
2012-10-18 11:32:58 -04:00
|
|
|
import urlparse
|
2012-10-22 14:31:21 -04:00
|
|
|
import sys
|
2012-10-03 17:21:09 -04:00
|
|
|
|
2012-10-04 18:06:54 -04:00
|
|
|
# Check that we have a recent enough version
|
2012-11-01 17:50:38 -04:00
|
|
|
# Older versions don't provide the 'json' attribute on responses.
|
2012-10-04 18:06:54 -04:00
|
|
|
assert(requests.__version__ > '0.12')
|
|
|
|
|
2012-10-01 15:36:44 -04:00
|
|
|
class HumbugAPI():
|
2012-10-22 14:31:21 -04:00
|
|
|
def __init__(self, email, api_key, verbose=False, retry_on_errors=True,
|
2012-10-27 11:36:55 -04:00
|
|
|
site="https://humbughq.com", client="API"):
|
2012-10-01 15:36:44 -04:00
|
|
|
self.api_key = api_key
|
|
|
|
self.email = email
|
|
|
|
self.verbose = verbose
|
|
|
|
self.base_url = site
|
2012-10-22 14:31:21 -04:00
|
|
|
self.retry_on_errors = retry_on_errors
|
2012-10-23 10:59:42 -04:00
|
|
|
self.client_name = client
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-11-09 14:16:22 -05:00
|
|
|
def do_api_query(self, request, url, longpolling = False):
|
2012-10-22 14:31:21 -04:00
|
|
|
had_error_retry = False
|
2012-10-04 18:06:54 -04:00
|
|
|
request["email"] = self.email
|
|
|
|
request["api-key"] = self.api_key
|
2012-10-23 10:59:42 -04:00
|
|
|
request["client"] = self.client_name
|
2012-10-25 17:45:11 -04:00
|
|
|
request["failures"] = 0
|
2012-11-07 17:22:19 -05:00
|
|
|
|
|
|
|
for (key, val) in request.iteritems():
|
|
|
|
if not (isinstance(val, str) or isinstance(val, unicode)):
|
|
|
|
request[key] = simplejson.dumps(val)
|
|
|
|
|
2012-10-04 18:06:54 -04:00
|
|
|
while True:
|
|
|
|
try:
|
2012-11-09 14:16:22 -05:00
|
|
|
res = requests.post(urlparse.urljoin(self.base_url, url), data=request,
|
|
|
|
verify=True, timeout=55)
|
2012-10-22 14:31:21 -04:00
|
|
|
|
|
|
|
# On 50x errors, try again after a short sleep
|
|
|
|
if str(res.status_code).startswith('5') and self.retry_on_errors:
|
|
|
|
if self.verbose:
|
|
|
|
if not had_error_retry:
|
2012-11-09 15:38:11 -05:00
|
|
|
sys.stdout.write("connection error %s -- retrying." % (res.status_code,))
|
2012-10-22 14:31:21 -04:00
|
|
|
had_error_retry = True
|
2012-10-25 17:45:11 -04:00
|
|
|
request["failures"] += 1
|
2012-10-22 14:31:21 -04:00
|
|
|
else:
|
|
|
|
sys.stdout.write(".")
|
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
2012-10-04 18:06:54 -04:00
|
|
|
continue
|
2012-11-09 15:38:34 -05:00
|
|
|
except (requests.exceptions.Timeout, requests.exceptions.SSLError) as e:
|
|
|
|
# Timeouts are either a Timeout or an SSLError; we
|
|
|
|
# want the later exception handlers to deal with any
|
|
|
|
# non-timeout other SSLErrors
|
|
|
|
if (isinstance(e, requests.exceptions.SSLError) and
|
|
|
|
str(e) != "The read operation timed out"):
|
|
|
|
raise
|
2012-11-09 14:16:22 -05:00
|
|
|
if longpolling:
|
|
|
|
# When longpolling, we expect the timeout to fire,
|
|
|
|
# and the correct response is to just retry
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return {'msg': "Connection error:\n%s" % traceback.format_exc(),
|
|
|
|
"result": "connection-error"}
|
2012-10-04 18:06:54 -04:00
|
|
|
except requests.exceptions.ConnectionError:
|
2012-10-22 14:31:21 -04:00
|
|
|
if self.retry_on_errors:
|
|
|
|
if self.verbose:
|
|
|
|
if not had_error_retry:
|
|
|
|
sys.stdout.write("connection error -- retrying.")
|
|
|
|
had_error_retry = True
|
2012-10-25 17:45:11 -04:00
|
|
|
request["failures"] += 1
|
2012-10-22 14:31:21 -04:00
|
|
|
else:
|
|
|
|
sys.stdout.write(".")
|
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
2012-10-04 18:06:54 -04:00
|
|
|
return {'msg': "Connection error:\n%s" % traceback.format_exc(),
|
|
|
|
"result": "connection-error"}
|
|
|
|
except Exception:
|
2012-10-22 14:31:21 -04:00
|
|
|
# We'll split this out into more cases as we encounter new bugs.
|
2012-10-04 18:06:54 -04:00
|
|
|
return {'msg': "Unexpected error:\n%s" % traceback.format_exc(),
|
|
|
|
"result": "unexpected-error"}
|
|
|
|
|
2012-10-22 14:31:21 -04:00
|
|
|
if self.verbose and had_error_retry:
|
|
|
|
print "Success!"
|
2012-10-04 18:06:54 -04:00
|
|
|
if res.json is not None:
|
|
|
|
return res.json
|
|
|
|
return {'msg': res.text, "result": "http-error",
|
|
|
|
"status_code": res.status_code}
|
|
|
|
|
|
|
|
def send_message(self, request):
|
|
|
|
return self.do_api_query(request, "/api/v1/send_message")
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-10-04 18:06:54 -04:00
|
|
|
def get_messages(self, request = {}):
|
2012-11-09 14:16:22 -05:00
|
|
|
return self.do_api_query(request, "/api/v1/get_messages",
|
|
|
|
longpolling=True)
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-11-07 13:07:47 -05:00
|
|
|
def get_profile(self, request = {}):
|
|
|
|
return self.do_api_query(request, "/api/v1/get_profile")
|
|
|
|
|
2012-10-11 13:31:21 -04:00
|
|
|
def get_public_streams(self, request = {}):
|
|
|
|
return self.do_api_query(request, "/api/v1/get_public_streams")
|
|
|
|
|
|
|
|
def get_subscriptions(self, request = {}):
|
|
|
|
return self.do_api_query(request, "/api/v1/get_subscriptions")
|
|
|
|
|
2012-10-11 15:34:17 -04:00
|
|
|
def subscribe(self, streams):
|
2012-11-07 17:22:19 -05:00
|
|
|
request = {'streams': streams}
|
2012-10-11 15:34:17 -04:00
|
|
|
return self.do_api_query(request, "/api/v1/subscribe")
|
|
|
|
|
2012-10-12 17:19:49 -04:00
|
|
|
def call_on_each_message(self, callback, options = {}):
|
2012-10-01 15:36:44 -04:00
|
|
|
max_message_id = None
|
|
|
|
while True:
|
2012-10-04 16:13:47 -04:00
|
|
|
if max_message_id is not None:
|
|
|
|
options["first"] = "0"
|
|
|
|
options["last"] = str(max_message_id)
|
|
|
|
res = self.get_messages(options)
|
|
|
|
if 'error' in res.get('result'):
|
2012-10-01 15:36:44 -04:00
|
|
|
if self.verbose:
|
2012-10-04 18:06:54 -04:00
|
|
|
if res["result"] == "http-error":
|
2012-10-22 14:31:21 -04:00
|
|
|
print "HTTP error fetching messages -- probably a server restart"
|
2012-10-04 16:13:47 -04:00
|
|
|
elif res["result"] == "connection-error":
|
2012-10-22 14:31:21 -04:00
|
|
|
print "Connection error fetching messages -- probably server is temporarily down?"
|
2012-10-04 16:13:47 -04:00
|
|
|
else:
|
|
|
|
print "Server returned error:\n%s" % res["msg"]
|
|
|
|
# TODO: Make this back off once it's more reliable
|
2012-10-01 15:36:44 -04:00
|
|
|
time.sleep(1)
|
2012-10-02 15:47:59 -04:00
|
|
|
continue
|
2012-10-10 16:27:22 -04:00
|
|
|
for message in sorted(res['messages'], key=lambda x: int(x["id"])):
|
|
|
|
max_message_id = max(max_message_id, int(message["id"]))
|
2012-10-01 15:36:44 -04:00
|
|
|
callback(message)
|