def message_get(self, req): """Gets a message from a queue :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') message_id = req._body.get('message_id') LOG.debug(u'Message get - message: %(message)s, ' u'queue: %(queue)s, project: %(project)s', {'message': message_id, 'queue': queue_name, 'project': project_id}) try: message = self._message_controller.get( queue_name, message_id, project=project_id) except storage_errors.DoesNotExist as ex: LOG.debug(ex) headers = {'status': 404} return api_utils.error_response(req, ex, headers) # Prepare response message = api_utils.format_message(message) headers = {'status': 200} body = {'messages': message} return response.Response(req, body, headers)
def message_get_many(self, req): """Gets a set of messages from a queue :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') message_ids = list(req._body.get('message_ids')) LOG.debug(u'Message get - queue: %(queue)s, ' u'project: %(project)s', { 'queue': queue_name, 'project': project_id }) try: self._validate.message_listing(limit=len(message_ids)) messages = self._message_controller.bulk_get( queue_name, message_ids=message_ids, project=project_id) except validation.ValidationFailed as ex: LOG.debug(ex) headers = {'status': 400} return api_utils.error_response(req, ex, headers) # Prepare response messages = list(messages) messages = [api_utils.format_message(message) for message in messages] headers = {'status': 200} body = {'messages': messages} return response.Response(req, body, headers)
def message_list(self, req): """Gets a list of messages on a queue :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') LOG.debug(u'Message list - queue: %(queue)s, ' u'project: %(project)s', { 'queue': queue_name, 'project': project_id }) try: kwargs = api_utils.get_headers(req) self._validate.client_id_uuid_safe(req._headers.get('Client-ID')) client_uuid = api_utils.get_client_uuid(req) self._validate.message_listing(**kwargs) results = self._message_controller.list(queue_name, project=project_id, client_uuid=client_uuid, **kwargs) # Buffer messages cursor = next(results) messages = list(cursor) except (ValueError, api_errors.BadRequest, validation.ValidationFailed) as ex: LOG.debug(ex) headers = {'status': 400} return api_utils.error_response(req, ex, headers) except storage_errors.DoesNotExist as ex: LOG.debug(ex) headers = {'status': 404} return api_utils.error_response(req, ex, headers) if messages: # Found some messages, so prepare the response kwargs['marker'] = next(results) messages = [ api_utils.format_message(message) for message in messages ] headers = {'status': 200} body = {'messages': messages} return response.Response(req, body, headers)
def message_list(self, req): """Gets a list of messages on a queue :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') LOG.debug(u'Message list - queue: %(queue)s, ' u'project: %(project)s', {'queue': queue_name, 'project': project_id}) try: kwargs = api_utils.get_headers(req) self._validate.client_id_uuid_safe(req._headers.get('Client-ID')) client_uuid = api_utils.get_client_uuid(req) self._validate.message_listing(**kwargs) results = self._message_controller.list( queue_name, project=project_id, client_uuid=client_uuid, **kwargs) # Buffer messages cursor = next(results) messages = list(cursor) except (ValueError, api_errors.BadRequest, validation.ValidationFailed) as ex: LOG.debug(ex) headers = {'status': 400} return api_utils.error_response(req, ex, headers) except storage_errors.DoesNotExist as ex: LOG.debug(ex) headers = {'status': 404} return api_utils.error_response(req, ex, headers) if messages: # Found some messages, so prepare the response kwargs['marker'] = next(results) messages = [api_utils.format_message(message) for message in messages] headers = {'status': 200} body = {'messages': messages} return response.Response(req, body, headers)
def claim_get(self, req): """Gets a claim :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') claim_id = req._body.get('claim_id') LOG.debug( u'Claim get - claim: %(claim_id)s, ' u'queue: %(queue_name)s, project: %(project_id)s', { 'queue_name': queue_name, 'project_id': project_id, 'claim_id': claim_id }) try: meta, msgs = self._claim_controller.get(queue_name, claim_id=claim_id, project=project_id) # Buffer claimed messages # TODO(vkmc): Optimize along with serialization (see below) meta['messages'] = list(msgs) except storage_errors.DoesNotExist as ex: LOG.debug(ex) error = _('Claim %s does not exist.') % claim_id headers = {'status': 404} return api_utils.error_response(req, ex, headers, error) # Serialize claimed messages # TODO(vkmc): Optimize meta['messages'] = [ api_utils.format_message(msg, claim_id) for msg in meta['messages'] ] del meta['id'] headers = {'status': 200} body = meta return response.Response(req, body, headers)
def claim_get(self, req): """Gets a claim :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') claim_id = req._body.get('claim_id') LOG.debug(u'Claim get - claim: %(claim_id)s, ' u'queue: %(queue_name)s, project: %(project_id)s', {'queue_name': queue_name, 'project_id': project_id, 'claim_id': claim_id}) try: meta, msgs = self._claim_controller.get( queue_name, claim_id=claim_id, project=project_id) # Buffer claimed messages # TODO(vkmc): Optimize along with serialization (see below) meta['messages'] = list(msgs) except storage_errors.DoesNotExist as ex: LOG.debug(ex) error = _('Claim %s does not exist.') % claim_id headers = {'status': 404} return api_utils.error_response(req, ex, headers, error) # Serialize claimed messages # TODO(vkmc): Optimize meta['messages'] = [api_utils.format_message(msg, claim_id) for msg in meta['messages']] del meta['id'] headers = {'status': 200} body = meta return response.Response(req, body, headers)
def message_get_many(self, req): """Gets a set of messages from a queue :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') message_ids = list(req._body.get('message_ids')) LOG.debug(u'Message get - queue: %(queue)s, ' u'project: %(project)s', {'queue': queue_name, 'project': project_id}) try: self._validate.message_listing(limit=len(message_ids)) messages = self._message_controller.bulk_get( queue_name, message_ids=message_ids, project=project_id) except validation.ValidationFailed as ex: LOG.debug(ex) headers = {'status': 400} return api_utils.error_response(req, ex, headers) # Prepare response messages = list(messages) messages = [api_utils.format_message(message) for message in messages] headers = {'status': 200} body = {'messages': messages} return response.Response(req, body, headers)
def claim_create(self, req): """Creates a claim :param req: Request instance ready to be sent. :type req: `api.common.Request` :return: resp: Response instance :type: resp: `api.common.Response` """ project_id = req._headers.get('X-Project-ID') queue_name = req._body.get('queue_name') LOG.debug(u'Claims create - queue: %(queue)s, ' u'project: %(project)s', {'queue': queue_name, 'project': project_id}) self._claim_post_spec = ( ('ttl', int, self._defaults.claim_ttl), ('grace', int, self._defaults.claim_grace), ) # Claim some messages # NOTE(vkmc): We build a dict with the ttl and grace # This is the metadata the storage is waiting for kwargs = api_utils.get_headers(req) # Read claim metadata (e.g., ttl) and raise appropriate # errors as needed. metadata = api_utils.sanitize(kwargs, self._claim_post_spec) limit = (None if kwargs.get('limit') is None else kwargs.get('limit')) claim_options = {} if limit is None else {'limit': limit} try: self._validate.claim_creation(metadata, limit=limit) except (ValueError, validation.ValidationFailed) as ex: LOG.debug(ex) headers = {'status': 400} return api_utils.error_response(req, ex, headers) cid, msgs = self._claim_controller.create( queue_name, metadata=metadata, project=project_id, **claim_options) # Buffer claimed messages # TODO(vkmc): optimize, along with serialization (below) resp_msgs = list(msgs) # Serialize claimed messages, if any. This logic assumes # the storage driver returned well-formed messages. if len(resp_msgs) != 0: resp_msgs = [api_utils.format_message(msg, cid) for msg in resp_msgs] headers = {'status': 201} body = {'claim_id': cid, 'messages': resp_msgs} else: headers = {'status': 204} body = {'claim_id': cid} return response.Response(req, body, headers)