123
faklfjal
发
发觉了房间阿里
#-*- coding: utf-8 -*-
__author__ = 'haiqiang'
from flask_restplus import Api, Resource, fields
from flask import Flask
import sys
# print sys.getdefaultencoding()
# reload(sys)
# sys.setdefaultencoding("utf-8")
app = Flask(__name__)
api = Api(app)
@api.route('/somewhere', endpoint='doc')
class Somewhere(Resource):
def get(self):
return {}
def post(self):
api.abort(403)
u'''doc 用于描述API细节 最终用于 Swagger API 声明。适用于类或者方法'''
@api.route('/my_doc/<id>', endpoint="my-doc")
@api.doc(params={'id':'An ID'})
class MyDoc(Resource):
def get(self, id):
return {}
@api.doc(response={403:'Not Authorized'})
def post(self, id):
api.abort(403)
u'''model 用于声明 API 可以 serialize (序列化) 的模型,
还可以通过 __schema_format__ 和 __schema_type__ 指定类型'''
u'''extend 可用于注册 augmented (增强)模型,
减少重复的域'''
u'''inherit 以 Swagger way 方式扩展模型,并处理多态,
在 Swagger 的模型定义中同时注册 parent 和 child'''
u'''marshal_with 用于说明方法'''
marshal_with_fields = api.model('Resource', {
'name': fields.String,
})
@api.route('/my_marshal_with/<id>', endpoint='my-marshal-with')
class MyMarshalWith(Resource):
@api.marshal_with(marshal_with_fields, as_list=True)
def get(self):
return {}
# return get_objects()
@api.marshal_with(marshal_with_fields)
def post(self):
return {}
u'''except 指定期望的输入域,
是doc(body=<fields>)的简化形式'''
expect_fields = api.model("Resource", {
'name': fields.String
})
@api.route('/my_expect')
class MyExpect(Resource):
@api.expect(expect_fields)
def get(self):
return {}
@api.expect([expect_fields])
def post(self):
return {}
u'''response 已知的响应,
是doc(response='...')的简化形式,
response model 可以作为第三个参数'''
response_model = api.model('Model', {
'name': fields.String,
})
@api.route('/my_response')
class MyResponse(Resource):
@api.response(200, 'Success')
@api.response(400, 'Validation Error', response_model)
@api.marshal_with(response_model, code=201, description='Object created')
def get(self):
return {}
@api.response(200, 'Success', response_model)
def post(self):
return {}
if __name__ == '__main__':
app.debug = True
app.run()
网友评论