Skip to content
Browse files

full API coverage, full backward compatibility with v0.3; version 0.4

  • Loading branch information...
1 parent ad0403e commit 9df116409c1550372e9ba353ea52bf9610f38096 @featalion featalion committed
Showing with 101 additions and 22 deletions.
  1. +99 −20 iron_mq.py
  2. +2 −2 setup.py
View
119 iron_mq.py
@@ -16,44 +16,37 @@ def __init__(self, mq, name):
mq -- An instance of IronMQ.
name -- The name of the queue.
"""
-
self.client = mq.client
self.name = name
-
def info(self):
"""Execute an HTTP request to get details on a queue, and
return it.
"""
-
url = "queues/%s" % (self.name,)
result = self.client.get(url)
- return result["body"]
+ return result["body"]
def size(self):
"""Queue size"""
return self.info()['size']
-
def id(self):
"""Queue ID"""
return self.info()['id']
-
def total_messages(self):
"""Queue total messages count"""
return self.info()['total_messages']
-
def clear(self):
"""Executes an HTTP request to clear all contents of a queue.
"""
-
url = "queues/%s/clear" % (self.name,)
result = self.client.post(url)
- return result['body']
+ return result['body']
def delete(self, message_id):
"""Execute an HTTP request to delete a message from queue.
@@ -61,11 +54,10 @@ def delete(self, message_id):
Arguments:
message_id -- The ID of the message to be deleted.
"""
-
url = "queues/%s/messages/%s" % (self.name, message_id)
result = self.client.delete(url)
- return result["body"]
+ return result["body"]
def post(self, *messages):
"""Executes an HTTP request to create message on the queue.
@@ -74,8 +66,7 @@ def post(self, *messages):
Arguments:
messages -- An array of messages to be added to the queue.
"""
-
- url = "queues/%s/messages" % (self.name,)
+ url = "queues/%s/messages" % self.name
msgs = [{'body':msg} if isinstance(msg, basestring) else msg
for msg in messages]
@@ -86,21 +77,107 @@ def post(self, *messages):
return result['body']
-
def get(self, max=None):
"""Executes an HTTP request to get a message off of a queue.
Keyword arguments:
max -- The maximum number of messages to pull. Defaults to 1.
"""
-
- n = ""
+ url = "queues/%s/messages" % self.name
if max is not None:
- n = "&n=%s" % max
- url = "queues/%s/messages?%s" % (self.name, n)
+ url = "%s?n=%s" % (url, max)
+
result = self.client.get(url)
+
return result['body']
+ def peek(self, max=None):
+ url = "queues/%s/messages/peek" % self.name
+ if max is not None:
+ url = "%s?n=%s" % (url, max)
+
+ response = self.client.get(url)
+
+ return response['body']
+
+ def touch(self, message_id):
+ url = "queues/%s/messages/%s/touch" % (self.name, message_id)
+
+ response = self.client.post(url, body=json.dumps({}),
+ headers={"Content-Type":"application/json"})
+
+ return response['body']
+
+ def release(self, message_id, delay=0):
+ url = "queues/%s/messages/%s/release" % (self.name, message_id)
+ body = {}
+ if delay > 0:
+ body['delay'] = delay
+ body = json.dumps(body)
+
+ response = self.client.post(url, body=body,
+ headers={"Content-Type":"application/json"})
+
+ return response['body']
+
+ 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}]
+ body = json.dumps(body)
+
+ response = self.client.post(url, body=body,
+ headers={"Content-Type":"application/json"})
+
+ return response['body']
+
+ def delete_queue(self):
+ url = "queues/%s" % self.name
+
+ response = self.client.delete(url)
+
+ return response['body']
+
+ def add_subscribers(self, *subscribers):
+ url = "queues/%s/subscribers" % self.name
+ body = json.dumps(self._prepare_subscribers(*subscribers))
+
+ response = self.client.post(url, body=body,
+ headers={"Content-Type":"application/json"})
+
+ return response['body']
+
+ def remove_subscribers(self, *subscribers):
+ url = "queues/%s/subscribers" % self.name
+ body = json.dumps(self._prepare_subscribers(*subscribers))
+
+ response = self.client.delete(url, body=body,
+ headers={"Content-Type":"application/json"})
+
+ return response['body']
+
+ def get_message_push_statuses(self, message_id):
+ url = "queues/%s/messages/%s/subscribers" % (self.name, message_id)
+
+ response = self.client.get(url)
+
+ return response['body']
+
+ def delete_message_push_status(self, message_id, subscriber_id):
+ url = "queues/%s/messages/%s/subscribers/%s" % (self.name, message_id, subscriber_id)
+
+ response = self.client.delete(url)
+
+ return response['body']
+
+ def _prepare_subscribers(self, *subscribers):
+ subscrs = [{'url': ss} for ss in subscribers]
+
+ return {'subscribers': subscrs}
class IronMQ:
NAME = "iron_mq_python"
@@ -119,7 +196,7 @@ def __init__(self, name=None, **kwargs):
version=IronMQ.VERSION, product="iron_mq", **kwargs)
- def queues(self, page=None):
+ def queues(self, page=None, per_page=None):
"""Execute an HTTP request to get a list of queues and return it.
Keyword arguments:
@@ -129,12 +206,15 @@ def queues(self, page=None):
options = {}
if page is not None:
options['page'] = page
+ if per_page is not None:
+ options['per_page'] = per_page
query = urllib.urlencode(options)
url = "queues"
if query != "":
url = "%s?%s" % (url, query)
result = self.client.get(url)
+
return [queue["name"] for queue in result["body"]]
@@ -144,7 +224,6 @@ def queue(self, queue_name):
Arguments:
queue_name -- The name of the queue.
"""
-
return Queue(self, queue_name)
View
4 setup.py
@@ -4,7 +4,7 @@
name = "iron-mq",
py_modules = ["iron_mq"],
install_requires = ["iron_core"],
- version = "0.3",
+ version = "0.4",
description = "Client library for IronMQ, a message queue in the cloud",
author = "Iron.io",
author_email = "thirdparty@iron.io",
@@ -25,7 +25,7 @@
---------------------
This package wraps the IronMQ API. It provides a simple and intuitive way
-to interact with the IronMW service.
+to interact with the IronMQ service.
IronMQ is a service provided by Iron.io. A flexible, scalable,
and durable messaging system lets different parts of a cloud app

0 comments on commit 9df1164

Please sign in to comment.
Something went wrong with that request. Please try again.