Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improving lib api by @treeder request

  • Loading branch information...
commit 07eeaa9157a4896af05915648bf4c90b05033318 1 parent 108e5ae
@romand romand authored
Showing with 87 additions and 55 deletions.
  1. +65 −36 iron_mq.py
  2. +22 −19 test.py
View
101 iron_mq.py
@@ -22,7 +22,7 @@ def __init__(self, name=None, **kwargs):
version=IronMQ.VERSION, product="iron_mq", **kwargs)
- def getQueues(self, page=None):
+ def queues(self, page=None):
"""Execute an HTTP request to get a list of queues and return it.
Keyword arguments:
@@ -41,60 +41,101 @@ def getQueues(self, page=None):
return [queue["name"] for queue in result["body"]]
- def getQueueDetails(self, queue_name):
- """Execute an HTTP request to get details on a specific queue, and
- return it.
+ def queue(self, queue_name):
+ """Returns Queue object.
Arguments:
- queue_name -- The name of the queue to get the details of.
+ queue_name -- The name of the queue.
+ """
+
+ return Queue(self, queue_name)
+
+
+class Queue:
+ client = None
+ name = None
+
+ def __init__(self, mq, name):
+ """Creates object for manipulating a queue.
+
+ Arguments:
+ 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" % (queue_name,)
+ url = "queues/%s" % (self.name,)
result = self.client.get(url)
- queue = result["body"]
- return queue
+ 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 deleteMessage(self, queue_name, message_id):
- """Execute an HTTP request to delete a code package.
+
+ 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']
+
+
+ def delete(self, message_id):
+ """Execute an HTTP request to delete a message from queue.
Arguments:
- queue_name -- The name of the queue the message is in.
message_id -- The ID of the message to be deleted.
"""
- url = "queues/%s/messages/%s" % (queue_name, message_id)
+ url = "queues/%s/messages/%s" % (self.name, message_id)
result = self.client.delete(url)
return result["body"]
- def postMessage(self, queue_name, messages=[]):
+ def post(self, *messages):
"""Executes an HTTP request to create message on the queue.
+ Creates queue if not existed.
Arguments:
- queue_name -- The name of the queue to add the message to.
-
- Keyword arguments:
messages -- An array of messages to be added to the queue.
- Defaults to [].
"""
- url = "queues/%s/messages" % ( queue_name,)
+ url = "queues/%s/messages" % (self.name,)
+
msgs = [{'body':msg} if isinstance(msg, basestring) else msg
for msg in messages]
data = json.dumps({"messages": msgs})
- result = self.client.post(url=url, body=data, headers= {"Content-Type": "application/json"})
+ result = self.client.post(url=url, body=data,
+ headers={"Content-Type":"application/json"})
return result['body']
- def getMessage(self, queue_name, max=None):
+ def get(self, max=None):
"""Executes an HTTP request to get a message off of a queue.
- Arguments:
- queue_name -- The name of the queue a message is being fetched from.
-
Keyword arguments:
max -- The maximum number of messages to pull. Defaults to 1.
"""
@@ -102,19 +143,7 @@ def getMessage(self, queue_name, max=None):
n = ""
if max is not None:
n = "&n=%s" % max
- url = "queues/%s/messages?%s" % ( queue_name, n)
+ url = "queues/%s/messages?%s" % (self.name, n)
result = self.client.get(url)
return result['body']
-
- def clearQueue(self, queue_name):
- """Executes an HTTP request to clear all contents of a queue.
-
- Arguments:
- queue_name -- The name of the queue a messages are being cleared from.
- """
-
- url = "queues/%s/clear" % (queue_name,)
- result = self.client.post(url)
- return result['body']
-
View
41 test.py
@@ -9,34 +9,37 @@ def setUp(self):
self.mq = IronMQ()
def test_postMessage(self):
- queue = self.mq.getQueueDetails("test_queue")
- self.mq.postMessage("test_queue", ["test message"])
- queue2 = self.mq.getQueueDetails("test_queue")
- self.assertEqual(queue["size"], (queue2["size"] - 1))
+ q = self.mq.queue("test_queue")
+ old_size = q.size()
+ q.post("test message")
+ self.assertEqual(old_size, q.size() - 1)
def test_getMessage(self):
msg = "%s" % time.time()
- self.mq.postMessage("test_queue", [msg])
- message = self.mq.getMessage("test_queue")
+ q = self.mq.queue("test_queue")
+ q.post(msg)
+ message = q.get()
message = "%s" % message["messages"][0]["body"]
self.assertEqual(msg, message)
def test_deleteMessage(self):
- queue = self.mq.getQueueDetails("test_queue")
- msg = self.mq.postMessage("test_queue", ["test"])
- queue2 = self.mq.getQueueDetails("test_queue")
- self.mq.deleteMessage("test_queue", msg["ids"][0])
- queue3 = self.mq.getQueueDetails("test_queue")
- self.assertEqual(queue["size"], (queue2["size"] - 1))
- self.assertEqual(queue["size"], queue3["size"])
+ q = self.mq.queue("test_queue")
+ size = q.size()
+
+ msg = q.post("test")
+ self.assertEqual(size, q.size() - 1)
+
+ q.delete(msg["ids"][0])
+ self.assertEqual(size, q.size())
def test_clearQueue(self):
- queue = self.mq.postMessage("test_queue", ["%s" % time.time()])
- size = self.mq.getQueueDetails("test_queue")
- self.assertTrue(size["size"] > 0)
- self.mq.clearQueue("test_queue")
- new_size = self.mq.getQueueDetails("test_queue")
- self.assertEqual(new_size["size"], 0)
+ q = self.mq.queue("test_queue")
+
+ q.post("%s" % time.time())
+ self.assertTrue(q.size() > 0)
+
+ q.clear()
+ self.assertEqual(q.size(), 0)
if __name__ == '__main__':
Please sign in to comment.
Something went wrong with that request. Please try again.