2012-10-01 21:36:44 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
import simplejson
|
2012-10-04 22:13:47 +02:00
|
|
|
import requests
|
2012-10-01 21:36:44 +02:00
|
|
|
import time
|
2012-10-02 21:47:59 +02:00
|
|
|
import traceback
|
2012-10-01 21:36:44 +02:00
|
|
|
|
2012-10-04 22:13:47 +02:00
|
|
|
# TODO: Drop verify=False once we have real certificates
|
|
|
|
# Or switch to specifying a testing cert manually
|
2012-10-03 23:21:09 +02:00
|
|
|
|
2012-10-05 00:06:54 +02:00
|
|
|
# Check that we have a recent enough version
|
|
|
|
assert(requests.__version__ > '0.12')
|
|
|
|
|
2012-10-01 21:36:44 +02:00
|
|
|
class HumbugAPI():
|
|
|
|
def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"):
|
|
|
|
self.api_key = api_key
|
|
|
|
self.email = email
|
|
|
|
self.verbose = verbose
|
|
|
|
self.base_url = site
|
|
|
|
|
2012-10-05 00:06:54 +02:00
|
|
|
def do_api_query(self, request, url):
|
|
|
|
request["email"] = self.email
|
|
|
|
request["api-key"] = self.api_key
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
res = requests.post(self.base_url + url,
|
|
|
|
data=request,
|
|
|
|
verify=False,
|
|
|
|
auth=requests.auth.HTTPDigestAuth('tabbott',
|
|
|
|
'xxxxxxxxxxxxxxxxx'))
|
|
|
|
if res.status_code == requests.codes.service_unavailable:
|
|
|
|
# On 503 errors, try again after a short sleep
|
|
|
|
time.sleep(0.5)
|
|
|
|
continue
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
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"}
|
|
|
|
|
|
|
|
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 21:36:44 +02:00
|
|
|
|
2012-10-05 00:06:54 +02:00
|
|
|
def get_messages(self, request = {}):
|
|
|
|
return self.do_api_query(request, "/api/v1/get_messages")
|
2012-10-01 21:36:44 +02:00
|
|
|
|
2012-10-02 22:18:58 +02:00
|
|
|
def call_on_each_message(self, callback, options = {}):
|
2012-10-01 21:36:44 +02:00
|
|
|
max_message_id = None
|
|
|
|
while True:
|
2012-10-04 22:13:47 +02: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 21:36:44 +02:00
|
|
|
if self.verbose:
|
2012-10-05 00:06:54 +02:00
|
|
|
if res["result"] == "http-error":
|
2012-10-04 22:13:47 +02:00
|
|
|
print "Unexpected error -- probably a server restart"
|
|
|
|
elif res["result"] == "connection-error":
|
2012-10-05 00:06:54 +02:00
|
|
|
print "Connection error -- probably server is temporarily down?"
|
2012-10-04 22:13:47 +02:00
|
|
|
else:
|
|
|
|
print "Server returned error:\n%s" % res["msg"]
|
|
|
|
# TODO: Make this back off once it's more reliable
|
2012-10-01 21:36:44 +02:00
|
|
|
time.sleep(1)
|
2012-10-02 21:47:59 +02:00
|
|
|
continue
|
2012-10-10 22:27:22 +02: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 21:36:44 +02:00
|
|
|
callback(message)
|