在flask restplus中,我使用api.model
装饰器定义了响应主体数据结构,我希望api函数的输出应该产生我定义的确切数据结构。我不知道该怎么做。有人能告诉我如何验证烧瓶中的json响应内容吗?有没有用定义的模式验证json响应输出的解决方法?有什么想法吗?
电流输出:
这是我向api函数发出POST请求后的当前输出:myfunc
:
"{nrn "score": [72.188, 62.0955, 19.3374, 45.6086, 77.8891, 22.188, 45.9938, 91.9877, 14.2527, 1.5408, 62.5578],nrn "category": "low",nrn "direction": "text description",nrn "is_ready": true,nrn "features_used": {nrn "name": "heart_rate",nrn "value": null,nrn "range_value": [3.6667, 5, 6.3333, 7.6667, 9, 10.3333, 11.6667, 13, 14.3333],nrn "range_frequency": [0.0024, 0, 0.0049, 0.0016, 0.0073, 0.0195, 0.0098, 0.0138, 0.9406],nrn "level": 0nrn }nrn} n"
问题是当前输出的格式没有产生响应体定义的内容。如何解决此问题?如何验证烧瓶中的json响应内容?有什么可能做到这一点的方法吗?感谢
具有指定响应json主体的最小api
from flask import Flask, jsonify
from flask_restplus import Namespace, Resource, fields, reqparse
from flask_restplus import Api
app = Flask(__name__)
api = Api(app)
ns = api.namespace('hello-world')
used_features = {}
used_features['name'] = fields.String(attribute='name')
used_features['value'] = fields.Integer(attribute='value')
used_features['range_value'] = fields.List(
fields.Integer, attribute='range_value')
used_features['range_frequency'] = fields.List(
fields.Integer, attribute='range_frequency')
used_features['level'] = fields.Integer(attribute='level')
used_features_payload = api.model('feature_payload', used_features)
response_body= api.model('response', {
'score': fields.Integer,
'category': fields.String,
'direction': fields.String,
'is_ready': fields.Boolean,
'features_used': fields.Nested(used_features_payload)
})
目标:
我想验证当前输出的JSON模式。如何使用响应体JSON模式验证函数输出?知道吗?
您可以使用棉花糖验证您的响应。我发现你的问题如下:
from marshmallow import Schema, fields, post_load
from marshmallow import EXCLUDE
import json
from flask import jsonify
class Feature:
def __init__(self, name, value, range_value, range_frequency, importance):
self.name = name
self.value = value
self.range_value = range_value
self.range_frequency = range_frequency
self.importance = importance
class FeatureSchema(Schema):
value = fields.Integer()
name = fields.Str()
importance = fields.Integer()
range_value = fields.List(fields.Integer)
range_frequency = fields.List(fields.Integer)
@post_load
def make_feature(self, data, **kwargs):
return Feature(**data)
class App(object):
def __init__(self, score, category, guidance, readiness, features_used):
self.score = score
self.category = category
self.guidance = guidance
self.readiness = readiness
self.features_used = features_used
class RespSchema(Schema):
score = fields.Integer()
category = fields.Str()
guidance = fields.Str()
readiness = fields.Boolean()
features_used = fields.List(fields.Nested(FeatureSchema))
@post_load
def make_app(self, data, **kwargs):
return App(**data)
棉花糖对数据验证非常有效。
忽略代码中的技术细节,
要验证您的JSONschema或任何定义的schema,您可以使用JSONscheme包。
这个软件包使用起来相当简单。引用这里的用法部分。
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
您可以这样定义模式,然后只需传递对象以根据该模式进行验证。如果不匹配,验证器将抛出异常。
from jsonschema import validate
validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)