In flask-restplus
, I want to model the response body which has nested list strucure, so whenever make api call, response body will be returned what I expected. In responce body, it has a nested structure, I don't know how to document that. Am I gonna use fields.Dict()
? can anyone point me out here how to make this happen in flask-restplus
?
response body:
{
"score": 0,
"category": "low",
"guidance": "string",
"is_ready": true,
"used_features": [
{
"name": "hear_rate",
"value": 1002,
"range_value": [
10,
1000,
10000,
20000
],
"range_frequency": [
80,
15,
2,
1
],
"importance": 1
},
{
"name": "pressure",
"value": 400,
"range_value": [
10,
1000,
3000
],
"range_frequency": [
85,
10,
5
],
"importance": 2
}
]
}
my partial solution:
here is my partial solution
from flask import Flask, jsonify
from flask_restplus import Api, Resource, fields, reqparse, inputs
app = Flask(__name__)
api = Api(app)
ns = api.namespace('ns')
payload = api.model('Payload', {
'score': fields.Integer,
'category': fields.String,
'guidance': fields.String,
'is_ready': fields.Boolean,
## how to add used features arrays
})
@ns.route('/')
class AResource(Resource):
@ns.expect(payload)
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('score', type=str, required=True)
parser.add_argument('category', type=str, required=True)
parser.add_argument('guidance', type=str, required=True)
parser.add_argument('category', type=str, required=True)
parser.add_argument('is_ready', type= bool, required=True)
try: # Will raise an error if date can't be parsed.
args = parser.parse_args() # type "dict"
return jsonify(args)
except:
return None, 400
if __name__ == '__main__':
app.run(debug=True)
in my attempted code, I couldn't come up with a solution of how to model used_features
dictionary. Is there any way to fix the defect of above attempt? can anyone point me out how to make this work where I can model the response body correctly? Am I gonna use Dict
or Nested
in my code? any further thought? thanks