diff --git a/iron_mq.py b/iron_mq.py index ef7d448..7484319 100644 --- a/iron_mq.py +++ b/iron_mq.py @@ -5,6 +5,7 @@ except: import simplejson as json + class Queue: client = None name = None @@ -66,7 +67,7 @@ def delete_multiple(self, *messages): messages -- An array of messages to be deleted from the queue. """ url = "queues/%s/messages" % self.name - + data = json.dumps({"ids": messages}) result = self.client.delete(url=url, body=data, headers={"Content-Type":"application/json"}) @@ -149,6 +150,10 @@ def release(self, message_id, delay=0): return response['body'] + # Subscribers can be: + # 1) a url string + # 2) a dict of {'url': 'url.com', 'headers': {'Content-Type': 'plain/text'}} + # 3) a list of url strings or dicts def update(self, subscribers=None, **kwargs): url = "queues/%s" % self.name body = kwargs @@ -156,12 +161,13 @@ def update(self, subscribers=None, **kwargs): if isinstance(subscribers, list): body.update(self._prepare_subscribers(*subscribers)) else: - body['subscribers'] = [{'url': subscribers}] + if isinstance(subscribers, dict): + body['subscribers'] = [subscribers] + else: + body['subscribers'] = [{'url': subscribers}] body = json.dumps(body) - response = self.client.post(url, body=body, headers={"Content-Type":"application/json"}) - return response['body'] def delete_queue(self): @@ -231,10 +237,16 @@ def _prepare_alert_ids(self, *alert_ids): return {'alerts': alerts} def _prepare_subscribers(self, *subscribers): - subscrs = [{'url': ss} for ss in subscribers] + subscrs = [] + for ss in subscribers: + if isinstance(ss, dict): + subscrs.append(ss) + else: + subscrs.append({'url': ss}) return {'subscribers': subscrs} + class IronMQ: NAME = "iron_mq_python" VERSION = "0.5" @@ -251,7 +263,6 @@ def __init__(self, name=None, **kwargs): self.client = iron_core.IronClient(name=IronMQ.NAME, version=IronMQ.VERSION, product="iron_mq", **kwargs) - def queues(self, page=None, per_page=None): """Execute an HTTP request to get a list of queues and return it. @@ -264,7 +275,7 @@ def queues(self, page=None, per_page=None): options['page'] = page if per_page is not None: options['per_page'] = per_page - + query = urllib.urlencode(options) url = "queues" if query != "": @@ -273,7 +284,6 @@ def queues(self, page=None, per_page=None): return [queue["name"] for queue in result["body"]] - def queue(self, queue_name): """Returns Queue object. @@ -282,7 +292,6 @@ def queue(self, queue_name): """ return Queue(self, queue_name) - # DEPRECATED def getQueues(self, page=None, project_id=None):