2012-10-01 15:36:44 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
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-01 15:36:44 -04:00
|
|
|
|
2012-10-04 16:13:47 -04:00
|
|
|
# TODO: Drop verify=False once we have real certificates
|
|
|
|
# Or switch to specifying a testing cert manually
|
2012-10-03 17:21:09 -04:00
|
|
|
|
2012-10-01 15:36:44 -04: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
|
|
|
|
|
|
|
|
def send_message(self, submit_hash):
|
|
|
|
submit_hash["email"] = self.email
|
|
|
|
submit_hash["api-key"] = self.api_key
|
2012-10-04 16:13:47 -04:00
|
|
|
try:
|
|
|
|
res = requests.post(self.base_url + "/api/v1/send_message",
|
|
|
|
data=submit_hash,
|
|
|
|
verify=False,
|
|
|
|
auth=requests.auth.HTTPDigestAuth('tabbott', 'xxxxxxxxxxxxxxxxx'))
|
|
|
|
# TODO: Add some sort of automated retry for certain errors
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return {'msg': "Connection error\n%s" % traceback.format_exc(),
|
|
|
|
"result": "connection-error"}
|
|
|
|
if res.json is not None:
|
|
|
|
return res.json
|
|
|
|
return {'msg': res.text, "result": "unexpected-error",
|
|
|
|
"status_code": res.status_code}
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-10-02 16:18:58 -04:00
|
|
|
def get_messages(self, options = {}):
|
|
|
|
options["email"] = self.email
|
|
|
|
options["api-key"] = self.api_key
|
2012-10-04 16:13:47 -04:00
|
|
|
try:
|
|
|
|
res = requests.post(self.base_url + "/api/v1/get_messages",
|
|
|
|
data=options,
|
|
|
|
verify=False,
|
|
|
|
auth=requests.auth.HTTPDigestAuth('tabbott', 'xxxxxxxxxxxxxxxxx'))
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return {'msg': "Connection error\n%s" % traceback.format_exc(),
|
|
|
|
"result": "connection-error"}
|
|
|
|
if res.json is not None:
|
|
|
|
return res.json
|
|
|
|
return {'msg': res.text, "result": "unexpected-error",
|
|
|
|
"status_code": res.status_code}
|
2012-10-01 15:36:44 -04:00
|
|
|
|
2012-10-02 16:18:58 -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 16:13:47 -04:00
|
|
|
if res["result"] == "unexpected-error":
|
|
|
|
print "Unexpected error -- probably a server restart"
|
|
|
|
elif res["result"] == "connection-error":
|
|
|
|
print "Connection error -- probably server is down?"
|
|
|
|
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-04 16:13:47 -04:00
|
|
|
for message in sorted(res['messages'], key=lambda x: x["id"]):
|
2012-10-01 15:36:44 -04:00
|
|
|
max_message_id = max(max_message_id, message["id"])
|
|
|
|
callback(message)
|