From aaa62c0837ebdb10bbf2f35145353a4626b1b87a Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 2 Oct 2012 16:18:58 -0400 Subject: [PATCH] Refactor how the API handles options. (imported from commit caff4a0b8b2d88e5e60fe3a7727b6d4ac5f63bd0) --- common.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/common.py b/common.py index 8d8ef80..76d9a36 100644 --- a/common.py +++ b/common.py @@ -22,22 +22,20 @@ class HumbugAPI(): 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()]) + def get_messages(self, options = {}): + options["email"] = self.email + options["api-key"] = self.api_key + submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in options.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): + def call_on_each_message(self, callback, options = {}): max_message_id = None while True: try: - messages = self.get_messages(max_message_id) + options["first"] = "0" + options["last"] = str(last_received) + messages = self.get_messages(options) except HTTPError, e: # 502/503 typically means the server was restarted; sleep # a bit, then try again