| 1 |
""" |
| 2 |
Another example using Marshmallow schemas in SwaggerView (MethodView) |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
from flask import Flask, jsonify, request |
| 7 |
|
| 8 |
from flasgger import Schema, Swagger, SwaggerView, fields |
| 9 |
|
| 10 |
app = Flask(__name__) |
| 11 |
app.config['SWAGGER'] = { |
| 12 |
"title": "API using Marshmallow", |
| 13 |
"uiversion": 2 |
| 14 |
} |
| 15 |
|
| 16 |
swag = Swagger(app) |
| 17 |
|
| 18 |
|
| 19 |
class User(Schema): |
| 20 |
username = fields.Str(required=True) |
| 21 |
age = fields.Int(required=True, min=18) |
| 22 |
tags = fields.List(fields.Str()) |
| 23 |
|
| 24 |
|
| 25 |
class UserPostView(SwaggerView): |
| 26 |
|
| 27 |
parameters = User |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
responses = { |
| 38 |
200: { |
| 39 |
'description': 'A single user', |
| 40 |
'schema': User |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
tags = ['users'] |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
summary = "Will be overwritten by first line of docstring" |
| 57 |
description = "will be overwritten by otehr lines" |
| 58 |
|
| 59 |
def post(self): |
| 60 |
""" |
| 61 |
A simple post |
| 62 |
Do it |
| 63 |
--- |
| 64 |
# This value overwrites the attributes above |
| 65 |
deprecated: true |
| 66 |
""" |
| 67 |
return jsonify(request.json) |
| 68 |
|
| 69 |
|
| 70 |
app.add_url_rule( |
| 71 |
'/user', |
| 72 |
view_func=UserPostView.as_view('user'), |
| 73 |
methods=['POST'] |
| 74 |
) |
| 75 |
|
| 76 |
if __name__ == "__main__": |
| 77 |
app.run(debug=True) |