Debian Codegraph @

examples/marshmallow_apispec.py

raw symbols SCIP
1 """
2 Another example using Marshmallow schemas in SwaggerView (MethodView)
3 """
4 # coding: utf-8
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 # parameters = [
30 # {
31 # 'name': 'body',
32 # 'in': 'body',
33 # 'schema': User
34 # }
35 # ]
36
37 responses = {
38 200: {
39 'description': 'A single user',
40 'schema': User
41 }
42 }
43
44 # optional
45 # definitions = {
46 # 'User': User # if nto informed will be extracted from usage above
47 # }
48 tags = ['users']
49 # consumes = ['application/json']
50 # produces = ['application/json']
51 # schemes = ['http', 'https']
52 # security = []
53 # deprecated = False
54 # operationId = 'user'
55 # externalDocs = {'description': 'foo', 'url': 'bar.com'}
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)