Pretty print json content in http response
Because all content of valence http response is jsonable object, use flask.jsonify to pretty print it. The wrapper function make_response() is added in to utils module. Change-Id: I99cecb8a538182691936b8a42252feff6a5374d0 Closes-Bug: #1647888
This commit is contained in:
parent
9e4760872f
commit
3bac5afbb4
@ -22,6 +22,7 @@ from flask_restful import Resource
|
|||||||
from valence.api import base
|
from valence.api import base
|
||||||
from valence.api import link
|
from valence.api import link
|
||||||
from valence.api import types
|
from valence.api import types
|
||||||
|
from valence.common import utils
|
||||||
from valence.redfish import redfish as rfs
|
from valence.redfish import redfish as rfs
|
||||||
|
|
||||||
|
|
||||||
@ -87,7 +88,9 @@ class Root(Resource):
|
|||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
obj = RootBase.convert()
|
obj = RootBase.convert()
|
||||||
return json.loads(json.dumps(obj, default=lambda o: o.as_dict()))
|
return utils.make_response(
|
||||||
|
200,
|
||||||
|
json.loads(json.dumps(obj, default=lambda o: o.as_dict())))
|
||||||
|
|
||||||
|
|
||||||
class PODMProxy(Resource):
|
class PODMProxy(Resource):
|
||||||
|
@ -20,6 +20,7 @@ from flask_restful import Resource
|
|||||||
from valence.api import base
|
from valence.api import base
|
||||||
from valence.api import link
|
from valence.api import link
|
||||||
from valence.api import types
|
from valence.api import types
|
||||||
|
from valence.common import utils
|
||||||
|
|
||||||
|
|
||||||
class MediaType(base.APIBase):
|
class MediaType(base.APIBase):
|
||||||
@ -107,4 +108,6 @@ class V1(Resource):
|
|||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
vobj = V1Base.convert()
|
vobj = V1Base.convert()
|
||||||
return json.loads(json.dumps(vobj, default=lambda o: o.as_dict()))
|
return utils.make_response(
|
||||||
|
200,
|
||||||
|
json.loads(json.dumps(vobj, default=lambda o: o.as_dict())))
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
# Copyright 2016 Intel Corporation
|
# Copyright (c) 2016 Intel, Inc.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
# not use this file except in compliance with the License. You may obtain
|
# not use this file except in compliance with the License. You may obtain
|
||||||
# a copy of the License at
|
# a copy of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
"""This Module contains common function used across
|
"""This Module contains common function used across
|
||||||
the project
|
the project
|
||||||
@ -20,6 +20,9 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from flask import jsonify
|
||||||
|
import six
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -76,3 +79,30 @@ def match_conditions(json_content, filter_conditions):
|
|||||||
LOG.warn(" Filter string mismatch ")
|
LOG.warn(" Filter string mismatch ")
|
||||||
LOG.debug(" JSON CONTENT " + str(is_conditions_passed))
|
LOG.debug(" JSON CONTENT " + str(is_conditions_passed))
|
||||||
return is_conditions_passed
|
return is_conditions_passed
|
||||||
|
|
||||||
|
|
||||||
|
def make_response(status_code, content="", headers=None):
|
||||||
|
"""Wrapper function to create flask http response.
|
||||||
|
|
||||||
|
:param json_content: content of http response, should be json object
|
||||||
|
:param status_code: status code of http response, set default to 200
|
||||||
|
:param headers: additional headers of http response, should be dict
|
||||||
|
:returns: return_type -- flask Response object
|
||||||
|
"""
|
||||||
|
|
||||||
|
response = jsonify(content)
|
||||||
|
|
||||||
|
if isinstance(status_code, int):
|
||||||
|
response.status_code = status_code
|
||||||
|
else:
|
||||||
|
raise ValueError("Response status_code should be int object.")
|
||||||
|
|
||||||
|
# Set additional header for http response
|
||||||
|
if headers:
|
||||||
|
if isinstance(headers, dict):
|
||||||
|
for header, value in six.iteritems(headers):
|
||||||
|
response.headers[header] = value
|
||||||
|
else:
|
||||||
|
raise ValueError("Response headers should be dict object.")
|
||||||
|
|
||||||
|
return response
|
||||||
|
0
valence/tests/unit/common/__init__.py
Normal file
0
valence/tests/unit/common/__init__.py
Normal file
55
valence/tests/unit/common/test_utils.py
Normal file
55
valence/tests/unit/common/test_utils.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# Copyright (c) 2016 Intel, Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
from valence.common import utils
|
||||||
|
|
||||||
|
|
||||||
|
class TestMakeResponse(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
app = Flask(__name__)
|
||||||
|
self.app_context = app.test_request_context()
|
||||||
|
self.app_context.push()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.app_context.pop()
|
||||||
|
|
||||||
|
def test_make_response(self):
|
||||||
|
expect = {"key": "value"}
|
||||||
|
resp = utils.make_response(200, expect)
|
||||||
|
result = json.loads(resp.data.decode())
|
||||||
|
self.assertEqual(expect, result)
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
|
||||||
|
def test_make_response_with_wrong_status_code(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
utils.make_response(status_code="wrong_code")
|
||||||
|
|
||||||
|
def test_make_response_with_headers(self):
|
||||||
|
expect = {"key": "value"}
|
||||||
|
resp = utils.make_response(200, expect,
|
||||||
|
headers={"header": "header_value"})
|
||||||
|
result = json.loads(resp.data.decode())
|
||||||
|
self.assertEqual(expect, result)
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
self.assertEqual("header_value", resp.headers.get("header"))
|
||||||
|
|
||||||
|
def test_make_response_with_wrong_headers(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
utils.make_response(200, headers=("header", "header_value"))
|
Loading…
x
Reference in New Issue
Block a user