Refactor how the API handles options.

(imported from commit caff4a0b8b2d88e5e60fe3a7727b6d4ac5f63bd0)
This commit is contained in:
Tim Abbott 2012-10-02 16:18:58 -04:00
parent bebed43a23
commit aaa62c0837

View file

@ -22,22 +22,20 @@ class HumbugAPI():
res = self.browser.open(self.base_url + "/api/v1/send_message", submit_data) res = self.browser.open(self.base_url + "/api/v1/send_message", submit_data)
return simplejson.loads(res.read()) return simplejson.loads(res.read())
def get_messages(self, last_received = None): def get_messages(self, options = {}):
submit_hash = {} options["email"] = self.email
submit_hash["email"] = self.email options["api-key"] = self.api_key
submit_hash["api-key"] = self.api_key submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in options.items()])
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) res = self.browser.open(self.base_url + "/api/v1/get_updates", submit_data)
return simplejson.loads(res.read())['zephyrs'] return simplejson.loads(res.read())['zephyrs']
def call_on_each_message(self, callback): def call_on_each_message(self, callback, options = {}):
max_message_id = None max_message_id = None
while True: while True:
try: try:
messages = self.get_messages(max_message_id) options["first"] = "0"
options["last"] = str(last_received)
messages = self.get_messages(options)
except HTTPError, e: except HTTPError, e:
# 502/503 typically means the server was restarted; sleep # 502/503 typically means the server was restarted; sleep
# a bit, then try again # a bit, then try again