Debian Codegraph @

examples/definition_object_test.py

raw symbols SCIP
1 """
2 A test using simple class based definitions
3 """
4 from flask import Flask, jsonify, request
5 from flask.views import View
6
7 from flasgger import Swagger
8
9 app = Flask(__name__)
10
11 # Flasgger is initialized like a standard flask extension.
12 # You can also use .init_app() with the "app factory" pattern.
13 swag = Swagger(app)
14
15
16 # Definitions can be registered as objects or functions.
17 # Docstring lines (before '---') are used as the description.
18 # YAML after '---' defines the Swagger definition schema.
19 @swag.definition('return_test_1')
20 class ReturnTest(object):
21 """
22 test return
23 ---
24 properties:
25 result:
26 type: string
27 description: The test
28 default: 'test1'
29 """
30
31 def __init__(self, size):
32 self.size = int(size)
33
34 def dump(self):
35 return {"result": "test1" * self.size}
36
37
38 @swag.definition('return_test_2')
39 def return_test(size):
40 """
41 another test return
42 ---
43 properties:
44 result:
45 type: string
46 description: The test
47 default: 'test2'
48 """
49 size = int(size)
50
51 return {"result": "test2" * size}
52
53
54 # Flask endpoints with flasgger docstrings are automatically registered.
55 # The first line of the docstring is used as the summary/
56 # The following lines (before '---') are used as the description.
57 # YAML after '---' defines the Swagger path schema.
58 @app.route("/recs", methods=['GET'])
59 def recs():
60 """
61 A simple test API
62 This endpoint does nothing
63 Only returns "test"
64 ---
65 tags:
66 - testapi
67 parameters:
68 - name: size
69 in: query
70 type: string
71 description: size of elements
72 responses:
73 200:
74 description: A single user item
75 schema:
76 $ref: '#/definitions/return_test_1'
77 """
78 message = ReturnTest(request.args.get('size', 1)).dump()
79 return jsonify(message)
80
81
82 class Foo(View):
83 methods = ['GET', 'POST']
84
85 def dispatch_request(self):
86 """
87 A simple test API
88 This endpoint does nothing
89 Only returns "test"
90 ---
91 tags:
92 - rohit
93 parameters:
94 - name: size
95 in: query
96 type: string
97 description: size of elements
98 responses:
99 200:
100 description: A single user item
101 schema:
102 $ref: '#/definitions/return_test_2'
103 """
104 message = return_test(request.args.get('size', 1))
105 return jsonify(message)
106
107
108 app.add_url_rule('/dispatch_request', view_func=Foo.as_view('foo'))
109
110 if __name__ == "__main__":
111 app.run(debug=True)