Skip to content
Browse files

Merge pull request #11 from romand-ironio/master

library API update
  • Loading branch information...
2 parents ebf809d + 7a8bb1d commit b1c13839676c1abdfc61c29d95b7c0a505eda05b @treeder treeder committed
Showing with 218 additions and 110 deletions.
  1. +63 −18 README.md
  2. +114 −73 iron_mq.py
  3. +41 −19 test.py
View
81 README.md
@@ -10,57 +10,102 @@ To start using iron_mq_python, you need to sign up and get an OAuth2 token.
2. Get an OAuth2 Token at http://hud.iron.io/tokens
## Install iron_mq_python
-Just copy `iron_mq.py` and include it in your script:
+
+```sh
+pip install iron_mq_python
+```
+
+or just copy `iron_mq.py` and include it in your script:
```python
from iron_mq import *
```
-## Configure
-Two ways to configure IronWorker:
-* Passing named arguments:
+## Configure
```python
-ironmq = IronMQ(token="xxxxx", project_id="xxxxx")
+ironmq = IronMQ()
```
-* Passing an ini file name that stores your configuration options. Rename sample_config.ini to config.ini and include your Iron.io credentials (`token` and `project_id`):
-```python
-ironmq = IronMQ(config='config.ini')
+will try reasonable defaults, accepting following optionally:
+
+```
+ironmq = IronMQ(host="mq-aws-us-east-1.iron.io",
+ project_id="500f7b....b0f302e9",
+ token="Et1En7.....0LuW39Q",
+ protocol="https", port=443,
+ api_version=1,
+ config_file=None)
```
## The Basics
-### **Push** a message on the queue:
+### Listing queues
+
+```python
+ironmq.queues()
+```
+returns list of queues names
+we get queue by name:
```python
-ironmq.postMessage(queue_name="test_queue", messages=["Hello world"])
+queue = ironmq.queue("test_queue")
```
-More complex example:
+### **Push** a message(s) on the queue:
+
+```python
+queue.post("Hello world")
+```
+
+Message can be described by dict:
```python
message = {
- "body" => "Test Message",
- "timeout" => 120, # Timeout, in seconds. After timeout, item will be placed back on queue. Defaults to 60.
- 'delay' => 5, # The item will not be available on the queue until this many seconds have passed. Defaults to 0.
- 'expires_in' => 2*24*3600 # How long, in seconds, to keep the item on the queue before it is deleted.
+ "body" : "Test Message",
+ "timeout" : 120, # Timeout, in seconds. After timeout, item will be placed back on queue. Defaults to 60.
+ "delay" : 5, # The item will not be available on the queue until this many seconds have passed. Defaults to 0.
+ "expires_in" : 2*24*3600 # How long, in seconds, to keep the item on the queue before it is deleted.
}
-ironmq.postMessage(queue_name="test_queue", messages=[message])
+queue.post(message)
+```
+
+We can post several messages at once:
+```python
+queue.post("more", "and more", "and more")
+queue.post(*[str(i) for i in range(10)])
```
### **Pop** a message off the queue:
```python
-ironmq.getMessage(queue_name="test_queue")
+queue.get()
```
When you pop/get a message from the queue, it will NOT be deleted.
It will eventually go back onto the queue after a timeout if you don't delete it (default timeout is 60 seconds).
### **Delete** a message from the queue:
```python
-ironmq.deleteMessage(queue_name="test_queue", message_id=message_id)
+queue.delete(message_id)
```
Delete a message from the queue when you're done with it.
+### ***Clear*** a queue:
+```python
+queue.clear()
+```
+
+### Get queue ***size***, ***id***, ***total_messages*** and whole ***info***
+```python
+queue.info()
+ # {u'id': u'502d03d3211a8f5e7742d224',
+ # u'name': u'queue12',
+ # u'reserved': 0,
+ # u'size': 15,
+ # u'total_messages': 17}
+queue.size() # 15
+queue.name
+queue.total_messages() # 17
+queue.id() # u'502d03d3211a8f5e7742d224'
+```
# Full Documentation
View
187 iron_mq.py
@@ -5,124 +5,165 @@
except:
import simplejson as json
-class IronMQ:
- NAME = "iron_mq_python"
- VERSION = "0.3"
+class Queue:
client = None
name = None
- def __init__(self, name=None, **kwargs):
- """Prepare a configured instance of the API wrapper and return it.
+ def __init__(self, mq, name):
+ """Creates object for manipulating a queue.
- Keyword arguments are passed directly to iron_core_python; consult its
- documentation for a full list and possible values."""
- if name is not None:
- self.name = name
- self.client = iron_core.IronClient(name=IronMQ.NAME,
- version=IronMQ.VERSION, product="iron_mq", **kwargs)
+ Arguments:
+ mq -- An instance of IronMQ.
+ name -- The name of the queue.
+ """
- def getQueues(self, page=None, project_id=None):
- """Execute an HTTP request to get a list of queues and return it.
+ self.client = mq.client
+ self.name = name
- Keyword arguments:
- project_id -- The project ID to get queues from. Defaults to the
- project ID set when initialising the wrapper.
- page -- The 0-based page to get queues from. Defaults to None, which
- omits the parameter.
+
+ def info(self):
+ """Execute an HTTP request to get details on a queue, and
+ return it.
"""
- options = {}
- if page is not None:
- options['page'] = page
- query = urllib.urlencode(options)
- url = "queues"
- if query != "":
- url = "%s?%s" % (url, query)
+ url = "queues/%s" % (self.name,)
result = self.client.get(url)
- return [queue["name"] for queue in result["body"]]
+ return result["body"]
- def getQueueDetails(self, queue_name, project_id=None):
- """Execute an HTTP request to get details on a specific queue, and
- return it.
- Keyword arguments:
- queue_name -- The name of the queue to get the details of. (Required)
- project_id -- The ID of the project the queue belongs to. Defaults to
- the project ID set when initialising the wrapper.
+ 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" % (queue_name,)
- result = self.client.get(url)
- queue = result["body"]
- return queue
- def deleteMessage(self, queue_name, message_id, project_id=None):
- """Execute an HTTP request to delete a code package.
+ url = "queues/%s/clear" % (self.name,)
+ result = self.client.post(url)
+ return result['body']
- Keyword arguments:
- queue_name -- The name of the queue the message is in. (Required)
- message_id -- The ID of the message to be deleted. (Required)
- project_id -- The ID of the project that contains the queue that
- contains the message. Defaults to the project ID set
- when initialising the wrapper.
+
+ def delete(self, message_id):
+ """Execute an HTTP request to delete a message from queue.
+
+ Arguments:
+ 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=[], project_id=None):
+
+ def post(self, *messages):
"""Executes an HTTP request to create message on the queue.
+ Creates queue if not existed.
- Keyword arguments:
- queue_name -- The name of the queue to add the message to. (Required)
+ Arguments:
messages -- An array of messages to be added to the queue.
- Defaults to [].
- project_id -- The ID of the project the queue is under. Defaults to
- the project ID set when the wrapper was initialised.
"""
- 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, project_id=None):
+
+ def get(self, max=None):
"""Executes an HTTP request to get a message off of a queue.
Keyword arguments:
- queue_name -- The name of the queue a message is being fetched from.
- (Required)
max -- The maximum number of messages to pull. Defaults to 1.
- project_id -- The ID of the project that contains the queue the message
- is to be pulled from. Defaults to the project ID set when
- the wrapper was initialised.
"""
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, project_id=None):
- """Executes an HTTP request to clear all contents of a queue.
+
+class IronMQ:
+ NAME = "iron_mq_python"
+ VERSION = "0.3"
+ client = None
+ name = None
+
+ def __init__(self, name=None, **kwargs):
+ """Prepare a configured instance of the API wrapper and return it.
+
+ Keyword arguments are passed directly to iron_core_python; consult its
+ documentation for a full list and possible values."""
+ if name is not None:
+ self.name = name
+ self.client = iron_core.IronClient(name=IronMQ.NAME,
+ version=IronMQ.VERSION, product="iron_mq", **kwargs)
+
+
+ def queues(self, page=None):
+ """Execute an HTTP request to get a list of queues and return it.
Keyword arguments:
- queue_name -- The name of the queue a messages are being cleared from.
- (Required)
- project_id -- The ID of the project that contains the queue that is
- being cleared. Defaults to the project ID set when the
- wrapper was initialised.
+ page -- The 0-based page to get queues from. Defaults to None, which
+ omits the parameter.
+ """
+ options = {}
+ if page is not None:
+ options['page'] = 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"]]
+
+
+ def queue(self, queue_name):
+ """Returns Queue object.
+
+ Arguments:
+ queue_name -- The name of the queue.
"""
+ return Queue(self, queue_name)
- url = "queues/%s/clear" % (queue_name,)
- result = self.client.post(url)
- return result['body']
-
+
+ # DEPRECATED
+
+ def getQueues(self, page=None, project_id=None):
+ return self.queues(page=page)
+
+ def getQueueDetails(self, queue_name, project_id=None):
+ return self.queue(queue_name).info()
+
+ def deleteMessage(self, queue_name, message_id, project_id=None):
+ return self.queue(queue_name).delete(message_id)
+
+ def postMessage(self, queue_name, messages=[], project_id=None):
+ return self.queue(queue_name).post(*messages)
+
+ def getMessage(self, queue_name, max=None, project_id=None):
+ return self.queue(queue_name).get(max=max)
+
+ def clearQueue(self, queue_name, project_id=None):
+ return self.queue(queue_name).clear()
View
60 test.py
@@ -9,35 +9,57 @@ 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)
+
+ def test_deprecated(self):
+ self.assertEqual(self.mq.queues(), self.mq.getQueues())
+
+ name = 'test_queue'
+ q = self.mq.queue(name)
+
+ self.assertEqual('Cleared', self.mq.clearQueue(name)['msg'])
+
+ self.assertEqual(0, q.size())
+
+ info = self.mq.getQueueDetails(name)
+ self.assertEqual(q.info(), info)
+
+ msg_id = self.mq.postMessage(name, ['hello mq'])['ids'][0]
+
+ resp = self.mq.getMessage(name)
+ self.assertEqual(msg_id, resp['messages'][-1]['id'])
+ self.assertEqual('Deleted', self.mq.deleteMessage(name, msg_id)['msg'])
if __name__ == '__main__':
unittest.main()

0 comments on commit b1c1383

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