def _get(self, req, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) kwargs = {} # NOTE(kgriffs): This syntax ensures that # we don't clobber default values with None. req.get_param('marker', store=kwargs) req.get_param_as_int('limit', store=kwargs) req.get_param_as_bool('echo', store=kwargs) req.get_param_as_bool('include_claimed', store=kwargs) try: 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 validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.QueueDoesNotExist as ex: LOG.debug(ex) messages = None except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be listed.') raise wsgi_errors.HTTPServiceUnavailable(description) if not messages: messages = [] else: # Found some messages, so prepare the response kwargs['marker'] = next(results) base_path = req.path.rsplit('/', 1)[0] messages = [wsgi_utils.format_message_v1_1(m, base_path, m['claim_id']) for m in messages] links = [] if messages: links = [ { 'rel': 'next', 'href': req.path + falcon.to_query_str(kwargs) } ] return { 'messages': messages, 'links': links }
def on_post(self, req, resp, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) try: # Place JSON size restriction before parsing self._validate.message_length(req.content_length) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) # Deserialize and validate the incoming messages document = wsgi_utils.deserialize(req.stream, req.content_length) if 'messages' not in document: description = _(u'No messages were found in the request body.') raise wsgi_errors.HTTPBadRequestAPI(description) messages = wsgi_utils.sanitize(document['messages'], self._message_post_spec, doctype=wsgi_utils.JSONArray) try: self._validate.message_posting(messages) if not self._queue_controller.exists(queue_name, project_id): self._queue_controller.create(queue_name, project=project_id) message_ids = self._message_controller.post( queue_name, messages=messages, project=project_id, client_uuid=client_uuid) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise falcon.HTTPNotFound() except storage_errors.MessageConflict as ex: LOG.exception(ex) description = _(u'No messages could be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) # Prepare the response ids_value = ','.join(message_ids) resp.location = req.path + '?ids=' + ids_value hrefs = [req.path + '/' + id for id in message_ids] body = {'resources': hrefs} resp.body = utils.to_json(body) resp.status = falcon.HTTP_201
def on_post(self, req, resp, project_id, queue_name): LOG.debug( u"Messages collection POST - queue: %(queue)s, " u"project: %(project)s", {"queue": queue_name, "project": project_id}, ) client_uuid = wsgi_helpers.get_client_uuid(req) try: # Place JSON size restriction before parsing self._validate.message_length(req.content_length) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) # Deserialize and validate the request body document = wsgi_utils.deserialize(req.stream, req.content_length) messages = wsgi_utils.sanitize(document, MESSAGE_POST_SPEC, doctype=wsgi_utils.JSONArray) try: self._validate.message_posting(messages) message_ids = self._message_controller.post( queue_name, messages=messages, project=project_id, client_uuid=client_uuid ) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise falcon.HTTPNotFound() except storage_errors.MessageConflict as ex: LOG.exception(ex) description = _(u"No messages could be enqueued.") raise wsgi_errors.HTTPServiceUnavailable(description) except Exception as ex: LOG.exception(ex) description = _(u"Messages could not be enqueued.") raise wsgi_errors.HTTPServiceUnavailable(description) # Prepare the response ids_value = ",".join(message_ids) resp.location = req.path + "?ids=" + ids_value hrefs = [req.path + "/" + id for id in message_ids] # NOTE(kgriffs): As of the Icehouse release, drivers are # no longer allowed to enqueue a subset of the messages # submitted by the client; it's all or nothing. Therefore, # 'partial' is now always False in the v1.0 API, and the # field has been removed in v1.1. body = {"resources": hrefs, "partial": False} resp.body = utils.to_json(body) resp.status = falcon.HTTP_201
def on_post(self, req, resp, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) try: # Place JSON size restriction before parsing self._validate.message_length(req.content_length) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) # Deserialize and validate the request body document = wsgi_utils.deserialize(req.stream, req.content_length) messages = wsgi_utils.sanitize(document, MESSAGE_POST_SPEC, doctype=wsgi_utils.JSONArray) try: self._validate.message_posting(messages) message_ids = self._message_controller.post( queue_name, messages=messages, project=project_id, client_uuid=client_uuid) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise wsgi_errors.HTTPNotFound(six.text_type(ex)) except storage_errors.MessageConflict as ex: LOG.exception(ex) description = _(u'No messages could be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) # Prepare the response ids_value = ','.join(message_ids) resp.location = req.path + '?ids=' + ids_value hrefs = [req.path + '/' + id for id in message_ids] # NOTE(kgriffs): As of the Icehouse release, drivers are # no longer allowed to enqueue a subset of the messages # submitted by the client; it's all or nothing. Therefore, # 'partial' is now always False in the v1.0 API, and the # field has been removed in v1.1. body = {'resources': hrefs, 'partial': False} resp.body = utils.to_json(body) resp.status = falcon.HTTP_201
def _get(self, req, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) kwargs = {} # NOTE(kgriffs): This syntax ensures that # we don't clobber default values with None. req.get_param("marker", store=kwargs) req.get_param_as_int("limit", store=kwargs) req.get_param_as_bool("echo", store=kwargs) req.get_param_as_bool("include_claimed", store=kwargs) try: 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 validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise falcon.HTTPNotFound() except Exception as ex: LOG.exception(ex) description = _(u"Messages could not be listed.") raise wsgi_errors.HTTPServiceUnavailable(description) if not messages: return None # Found some messages, so prepare the response kwargs["marker"] = next(results) base_path = req.path.rsplit("/", 1)[0] messages = [wsgi_utils.format_message_v1(m, base_path) for m in messages] return {"messages": messages, "links": [{"rel": "next", "href": req.path + falcon.to_query_str(kwargs)}]}
def on_post(self, req, resp, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) try: # NOTE(flwang): Replace 'exists' with 'get_metadata' won't impact # the performance since both of them will call # collection.find_one() queue_meta = None try: queue_meta = self._queue_controller.get_metadata( queue_name, project_id) except storage_errors.DoesNotExist as ex: self._validate.queue_identification(queue_name, project_id) self._queue_controller.create(queue_name, project=project_id) # NOTE(flwang): Queue is created in lazy mode, so no metadata # set. queue_meta = {} queue_max_msg_size = queue_meta.get('_max_messages_post_size', None) queue_default_ttl = queue_meta.get('_default_message_ttl', None) # TODO(flwang): To avoid any unexpected regression issue, we just # leave the _message_post_spec attribute of class as it's. It # should be removed in Newton release. if queue_default_ttl: message_post_spec = ( ('ttl', int, queue_default_ttl), ('body', '*', None), ) else: message_post_spec = ( ('ttl', int, self._default_message_ttl), ('body', '*', None), ) # Place JSON size restriction before parsing self._validate.message_length(req.content_length, max_msg_post_size=queue_max_msg_size) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) # Deserialize and validate the incoming messages document = wsgi_utils.deserialize(req.stream, req.content_length) if 'messages' not in document: description = _(u'No messages were found in the request body.') raise wsgi_errors.HTTPBadRequestAPI(description) messages = wsgi_utils.sanitize(document['messages'], message_post_spec, doctype=wsgi_utils.JSONArray) try: self._validate.message_posting(messages) message_ids = self._message_controller.post( queue_name, messages=messages, project=project_id, client_uuid=client_uuid) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise wsgi_errors.HTTPNotFound(six.text_type(ex)) except storage_errors.MessageConflict as ex: LOG.exception(ex) description = _(u'No messages could be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) # Prepare the response ids_value = ','.join(message_ids) resp.location = req.path + '?ids=' + ids_value hrefs = [req.path + '/' + id for id in message_ids] body = {'resources': hrefs} resp.body = utils.to_json(body) resp.status = falcon.HTTP_201
def _get(self, req, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) kwargs = {} # NOTE(kgriffs): This syntax ensures that # we don't clobber default values with None. req.get_param('marker', store=kwargs) req.get_param_as_int('limit', store=kwargs) req.get_param_as_bool('echo', store=kwargs) req.get_param_as_bool('include_claimed', store=kwargs) req.get_param_as_bool('include_delayed', store=kwargs) try: queue_meta = {} try: # NOTE(cdyangzhenyu): In order to determine whether the # queue has a delay attribute, the metadata of the queue # is obtained here. This may have a little performance impact. # So maybe a refactor is needed in the future. queue_meta = self._queue_controller.get_metadata( queue_name, project_id) except storage_errors.DoesNotExist as ex: LOG.exception(ex) queue_delay = queue_meta.get('_default_message_delay') if not queue_delay: # NOTE(cdyangzhenyu): If the queue without the metadata # attribute _default_message_delay, we don't filter # for delay messages. kwargs['include_delayed'] = True 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 validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.QueueDoesNotExist as ex: LOG.debug(ex) messages = None except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be listed.') raise wsgi_errors.HTTPServiceUnavailable(description) if not messages: messages = [] else: # Found some messages, so prepare the response kwargs['marker'] = next(results) base_path = req.path.rsplit('/', 1)[0] messages = [ wsgi_utils.format_message_v1_1(m, base_path, m['claim_id']) for m in messages ] links = [] if messages: links = [{ 'rel': 'next', 'href': req.path + falcon.to_query_str(kwargs) }] return {'messages': messages, 'links': links}
def on_post(self, req, resp, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) try: # NOTE(flwang): Replace 'exists' with 'get_metadata' won't impact # the performance since both of them will call # collection.find_one() queue_meta = None try: queue_meta = self._queue_controller.get_metadata(queue_name, project_id) except storage_errors.DoesNotExist as ex: self._validate.queue_identification(queue_name, project_id) self._queue_controller.create(queue_name, project=project_id) # NOTE(flwang): Queue is created in lazy mode, so no metadata # set. queue_meta = {} queue_max_msg_size = queue_meta.get('_max_messages_post_size', None) queue_default_ttl = queue_meta.get('_default_message_ttl', None) # TODO(flwang): To avoid any unexpected regression issue, we just # leave the _message_post_spec attribute of class as it's. It # should be removed in Newton release. if queue_default_ttl: message_post_spec = (('ttl', int, queue_default_ttl), ('body', '*', None),) else: message_post_spec = (('ttl', int, self._default_message_ttl), ('body', '*', None),) # Place JSON size restriction before parsing self._validate.message_length(req.content_length, max_msg_post_size=queue_max_msg_size) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) # Deserialize and validate the incoming messages document = wsgi_utils.deserialize(req.stream, req.content_length) if 'messages' not in document: description = _(u'No messages were found in the request body.') raise wsgi_errors.HTTPBadRequestAPI(description) messages = wsgi_utils.sanitize(document['messages'], message_post_spec, doctype=wsgi_utils.JSONArray) try: self._validate.message_posting(messages) message_ids = self._message_controller.post( queue_name, messages=messages, project=project_id, client_uuid=client_uuid) except validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.DoesNotExist as ex: LOG.debug(ex) raise wsgi_errors.HTTPNotFound(six.text_type(ex)) except storage_errors.MessageConflict as ex: LOG.exception(ex) description = _(u'No messages could be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be enqueued.') raise wsgi_errors.HTTPServiceUnavailable(description) # Prepare the response ids_value = ','.join(message_ids) resp.location = req.path + '?ids=' + ids_value hrefs = [req.path + '/' + id for id in message_ids] body = {'resources': hrefs} resp.body = utils.to_json(body) resp.status = falcon.HTTP_201
def _get(self, req, project_id, queue_name): client_uuid = wsgi_helpers.get_client_uuid(req) kwargs = {} # NOTE(kgriffs): This syntax ensures that # we don't clobber default values with None. req.get_param('marker', store=kwargs) req.get_param_as_int('limit', store=kwargs) req.get_param_as_bool('echo', store=kwargs) req.get_param_as_bool('include_claimed', store=kwargs) req.get_param_as_bool('include_delayed', store=kwargs) try: queue_meta = {} try: # NOTE(cdyangzhenyu): In order to determine whether the # queue has a delay attribute, the metadata of the queue # is obtained here. This may have a little performance impact. # So maybe a refactor is needed in the future. queue_meta = self._queue_controller.get_metadata(queue_name, project_id) except storage_errors.DoesNotExist as ex: LOG.exception(ex) queue_delay = queue_meta.get('_default_message_delay') if not queue_delay: # NOTE(cdyangzhenyu): If the queue without the metadata # attribute _default_message_delay, we don't filter # for delay messages. kwargs['include_delayed'] = True 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 validation.ValidationFailed as ex: LOG.debug(ex) raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex)) except storage_errors.QueueDoesNotExist as ex: LOG.debug(ex) messages = None except Exception as ex: LOG.exception(ex) description = _(u'Messages could not be listed.') raise wsgi_errors.HTTPServiceUnavailable(description) if not messages: messages = [] else: # Found some messages, so prepare the response kwargs['marker'] = next(results) base_path = req.path.rsplit('/', 1)[0] messages = [wsgi_utils.format_message_v1_1(m, base_path, m['claim_id']) for m in messages] links = [] if messages: links = [ { 'rel': 'next', 'href': req.path + falcon.to_query_str(kwargs) } ] return { 'messages': messages, 'links': links }