Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature subscriber headers #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions iron_mq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
except:
import simplejson as json


class Queue:
client = None
name = None
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -149,19 +150,24 @@ 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
if subscribers is not None:
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):
Expand Down Expand Up @@ -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"
Expand All @@ -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.

Expand All @@ -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 != "":
Expand All @@ -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.

Expand All @@ -282,7 +292,6 @@ def queue(self, queue_name):
"""
return Queue(self, queue_name)


# DEPRECATED

def getQueues(self, page=None, project_id=None):
Expand Down