diff --git a/common.py b/common.py new file mode 100644 index 0000000..8d8ef80 --- /dev/null +++ b/common.py @@ -0,0 +1,55 @@ +#!/usr/bin/python +import mechanize +import urllib +import simplejson +from urllib2 import HTTPError +import time + +class HumbugAPI(): + def __init__(self, email, api_key, verbose=False, site="https://app.humbughq.com"): + self.browser = mechanize.Browser() + self.browser.set_handle_robots(False) + self.browser.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki") + 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 + submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in submit_hash.items()]) + res = self.browser.open(self.base_url + "/api/v1/send_message", submit_data) + return simplejson.loads(res.read()) + + def get_messages(self, last_received = None): + submit_hash = {} + submit_hash["email"] = self.email + submit_hash["api-key"] = self.api_key + if last_received is not None: + submit_hash["first"] = "0" + submit_hash["last"] = str(last_received) + submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in submit_hash.items()]) + res = self.browser.open(self.base_url + "/api/v1/get_updates", submit_data) + return simplejson.loads(res.read())['zephyrs'] + + def call_on_each_message(self, callback): + max_message_id = None + while True: + try: + messages = self.get_messages(max_message_id) + except HTTPError, e: + # 502/503 typically means the server was restarted; sleep + # a bit, then try again + if self.verbose: + print "HTTP Error getting zephyrs; trying again soon." + print e + time.sleep(1) + except Exception, e: + # For other errors, just try again + print e + time.sleep(2) + continue + for message in sorted(messages, key=lambda x: x["id"]): + max_message_id = max(max_message_id, message["id"]) + callback(message) diff --git a/examples/curl-examples b/examples/curl-examples new file mode 100644 index 0000000..b12ee10 --- /dev/null +++ b/examples/curl-examples @@ -0,0 +1,4 @@ +#!/bin/sh +# Two quick API tests using curl +curl 127.0.0.1:8000/api/send_message -d "api-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -d "email=tabbott@humbughq.com" -d "type=personal" -d "new_zephyr=test" -d "recipient=tabbott@humbughq.com" +curl 127.0.0.1:8000/api/get_updates_new -d "api-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -d "email=tabbott@humbughq.com diff --git a/examples/print-messages b/examples/print-messages new file mode 100755 index 0000000..c7f20e7 --- /dev/null +++ b/examples/print-messages @@ -0,0 +1,38 @@ +#!/usr/bin/python +import mechanize +import urllib +import sys +import os +import optparse + +usage = """print-message --user= [options] + +Prints out each message received by the indicated user. + +Example: print-messages --user=tabbott@humbughq.com --site=http://127.0.0.1:8000 +""" +parser = optparse.OptionParser(usage=usage) +parser.add_option('--site', + dest='site', + default="https://app.humbughq.com/", + action='store') +parser.add_option('--api-key', + dest='api_key', + default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + action='store') +parser.add_option('--user', + dest='user', + action='store') +(options, args) = parser.parse_args() + +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) +import api.common +client = api.common.HumbugAPI(email=options.user, + api_key=options.api_key, + verbose=True, + site=options.site) + +def print_message(message): + print message + +client.call_on_each_message(print_message) diff --git a/examples/send-message b/examples/send-message new file mode 100755 index 0000000..3e446d1 --- /dev/null +++ b/examples/send-message @@ -0,0 +1,40 @@ +#!/usr/bin/python +import mechanize +import urllib +import sys +import os +import optparse + +usage = """send-message --user= [options] + +Sends a test message from by the provided user to tabbott@humbhughq.com. + +Example: send-message --user=tabbott@humbughq.com --site=http://127.0.0.1:8000 +""" +parser = optparse.OptionParser(usage=usage) +parser.add_option('--site', + dest='site', + default="https://app.humbughq.com/", + action='store') +parser.add_option('--api-key', + dest='api_key', + default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + action='store') +parser.add_option('--user', + dest='user', + action='store') +(options, args) = parser.parse_args() + +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) +import api.common +client = api.common.HumbugAPI(email=options.user, + api_key=options.api_key, + verbose=True, + site=options.site) + +message_data = { + "type": "personal", + "new_zephyr": "test", + "recipient": "tabbott@humbughq.com", +} +print client.send_message(message_data)