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