Added iterable list of queues
Added get a list of all queues as an iterable list. Change-Id: Ie33d33e613c34a9d207075a13df6e8136d10e878 partially-implements: blueprint queues-list
This commit is contained in:
parent
18dc3183a9
commit
6f03d1cbc1
@ -91,6 +91,18 @@ class Client(object):
|
||||
"""
|
||||
return queues.Queue(self, ref, **kwargs)
|
||||
|
||||
def queues(self, **params):
|
||||
"""Gets a list of queues from the server
|
||||
|
||||
:returns: A list of queues
|
||||
:rtype: `list`
|
||||
"""
|
||||
req, trans = self._request_and_transport()
|
||||
|
||||
queue_list = core.queue_list(trans, req, **params)
|
||||
|
||||
return queues._QueueIterator(self, queue_list)
|
||||
|
||||
def follow(self, ref):
|
||||
"""Follows ref.
|
||||
|
||||
|
@ -98,6 +98,34 @@ def queue_delete(transport, request, name, callback=None):
|
||||
request, name, callback=callback)
|
||||
|
||||
|
||||
def queue_list(transport, request, callback=None, **kwargs):
|
||||
"""Gets a list of queues
|
||||
|
||||
:param transport: Transport instance to use
|
||||
:type transport: `transport.base.Transport`
|
||||
:param request: Request instance ready to be sent.
|
||||
:type request: `transport.request.Request`
|
||||
:param callback: Optional callable to use as callback.
|
||||
If specified, this request will be sent asynchronously.
|
||||
(IGNORED UNTIL ASYNC SUPPORT IS COMPLETE)
|
||||
:type callback: Callable object.
|
||||
:param kwargs: Optional arguments for this operation.
|
||||
- marker: Where to start getting queues from.
|
||||
- limit: Maximum number of queues to get.
|
||||
"""
|
||||
|
||||
request.operation = 'queue_list'
|
||||
|
||||
request.params.update(kwargs)
|
||||
|
||||
resp = transport.send(request)
|
||||
|
||||
if not resp.content:
|
||||
return {'links': [], 'queues': []}
|
||||
|
||||
return resp.deserialized_content
|
||||
|
||||
|
||||
def message_list(transport, request, queue_name, callback=None, **kwargs):
|
||||
"""Gets a list of messages in queue `queue_name`
|
||||
|
||||
|
@ -17,6 +17,61 @@ from marconiclient.queues.v1 import core
|
||||
from marconiclient.queues.v1 import message
|
||||
|
||||
|
||||
class _QueueIterator(object):
|
||||
"""Queue iterator
|
||||
|
||||
This iterator is not meant to be used outside
|
||||
the scope of this package. The iterator gets
|
||||
a dictionary as returned by the queue listing
|
||||
endpoint and iterates over the queues in the
|
||||
`queues` key.
|
||||
|
||||
If there are no queues left to return, the iterator
|
||||
will try to load more by following the `next` rel link type.
|
||||
|
||||
The iterator raises a StopIteration exception if the server
|
||||
doesn't return more messages after a `next-page` call.
|
||||
|
||||
:param client: The client instance used by the queue
|
||||
:type client: `v1.Client`
|
||||
:param queues: Response returned by the queues listing call
|
||||
:type queues: Dict
|
||||
"""
|
||||
|
||||
def __init__(self, client, queues):
|
||||
self._client = client
|
||||
|
||||
self._links = queues['links']
|
||||
self._queues = queues['queues']
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def _next_page(self):
|
||||
for link in self._links:
|
||||
if link['rel'] == 'next':
|
||||
|
||||
queues = self._client.follow(link['href'])
|
||||
|
||||
if queues:
|
||||
self._links = queues['links']
|
||||
self._queues = queues['queues']
|
||||
return
|
||||
|
||||
raise StopIteration
|
||||
|
||||
def __next__(self):
|
||||
try:
|
||||
q = self._queues.pop(0)
|
||||
except IndexError:
|
||||
self._next_page()
|
||||
return self.next()
|
||||
|
||||
return Queue(self._client, q["name"], False)
|
||||
|
||||
next = __next__
|
||||
|
||||
|
||||
class Queue(object):
|
||||
|
||||
def __init__(self, client, queue_name, auto_create=True):
|
||||
|
Loading…
Reference in New Issue
Block a user