def render(res=None, resp_type=None, status=None, **kwargs): if not res: res = {} if type(res) is dict: res.update(kwargs) elif kwargs: # can't merge kwargs into the non-dict res abort_and_log(500, _("Non-dict and non-empty kwargs passed to render")) status_code = getattr(flask.request, 'status_code', None) if status: status_code = status if not status_code: status_code = 200 if not resp_type: resp_type = getattr(flask.request, 'resp_type', RT_JSON) if not resp_type: resp_type = RT_JSON serializer = None if "application/json" in resp_type: resp_type = RT_JSON serializer = wsgi.JSONDictSerializer() else: abort_and_log(400, _("Content type '%s' isn't supported") % resp_type) body = serializer.serialize(res) resp_type = str(resp_type) return flask.Response(response=body, status=status_code, mimetype=resp_type)
def test_render_pagination(self): def _assert_response_equal(response_obj, response, status, mimetype): self.assertEqual(response_obj.response, [response.encode()]) self.assertEqual(response_obj.status, status) self.assertEqual(response_obj.mimetype, mimetype) from sahara.api.middleware import sahara_middleware # noqa app = sahara_middleware.build_app() with app.test_request_context(): _200_OK = '200 OK' serializer = wsgi.JSONDictSerializer() resp = \ api.render(self.page, 'application/json', 200, name='clusters') body = serializer.serialize(self.response) _assert_response_equal(resp, body, _200_OK, 'application/json') self.page.prev, self.page.next = 35, 49 resp = \ api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = \ {"prev": 35, "next": 49} body = serializer.serialize(paginate_response) _assert_response_equal(resp, body, _200_OK, 'application/json') self.page.prev, self.page.next = 7, None resp = \ api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = {"prev": 7, "next": None} body = serializer.serialize(paginate_response) _assert_response_equal(resp, body, _200_OK, 'application/json') self.page.prev, self.page.next = None, 14 resp = \ api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = {"prev": None, "next": 14} body = serializer.serialize(paginate_response) _assert_response_equal(resp, body, _200_OK, 'application/json') self.page.prev, self.page.next = None, 11 resp = \ api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = \ {"prev": None, "next": 11} body = serializer.serialize(paginate_response) _assert_response_equal(resp, body, _200_OK, 'application/json')
def test_render_pagination(self, flask, request): serializer = wsgi.JSONDictSerializer() request.status_code = 200 api.render(self.page, 'application/json', 200, name='clusters') body = serializer.serialize(self.response) flask.assert_called_with(response=body, status=200, mimetype='application/json') self.page.prev, self.page.next = 35, 49 api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = \ {"prev": 35, "next": 49} body = serializer.serialize(paginate_response) flask.assert_called_with(response=body, status=200, mimetype='application/json') self.page.prev, self.page.next = 7, None api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = {"prev": 7, "next": None} body = serializer.serialize(paginate_response) flask.assert_called_with(response=body, status=200, mimetype='application/json') self.page.prev, self.page.next = None, 14 api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = {"prev": None, "next": 14} body = serializer.serialize(paginate_response) flask.assert_called_with(response=body, status=200, mimetype='application/json') self.page.prev, self.page.next = None, 11 api.render(self.page, 'application/json', 200, name='clusters') paginate_response = copy.copy(self.response) paginate_response["markers"] = \ {"prev": None, "next": 11} body = serializer.serialize(paginate_response) flask.assert_called_with(response=body, status=200, mimetype='application/json') self.page.prev, self.page.next = None, 11 api.render(self.page, 'application/json', 200, name='clusters')
def render(res=None, resp_type=None, status=None, name=None, **kwargs): if not res and type(res) is not types.Page: res = {} if type(res) is dict: res.update(kwargs) elif type(res) is types.Page: result = {name: [item.to_dict() for item in res]} result.update(kwargs) if res.prev or res.next or ('marker' in get_request_args()): result["markers"] = {"prev": res.prev, "next": res.next} res = result elif kwargs: # can't merge kwargs into the non-dict res abort_and_log(500, _("Non-dict and non-empty kwargs passed to render")) status_code = getattr(flask.request, 'status_code', None) if status: status_code = status if not status_code: status_code = 200 if not resp_type: resp_type = getattr(flask.request, 'resp_type', RT_JSON) if not resp_type: resp_type = RT_JSON serializer = None if "application/json" in resp_type: resp_type = RT_JSON serializer = wsgi.JSONDictSerializer() else: raise ex.InvalidDataException( _("Content type '%s' isn't supported") % resp_type) body = serializer.serialize(res) resp_type = str(resp_type) return flask.Response(response=body, status=status_code, mimetype=resp_type)