Debian Codegraph @

examples/swagger_config_merge.py

raw symbols SCIP
1 """
2 In this example `openapi` version is used instead of `swagger` version.
3 """
4 from flask import Flask, jsonify
5 from flasgger import Swagger, swag_from
6
7 app = Flask(__name__)
8
9 swagger_config_partial = {
10 "specs": [
11 {
12 "endpoint": "swagger",
13 "route": "/characteristics/swagger.json",
14 "rule_filter": lambda rule: True, # all in
15 "model_filter": lambda tag: True, # all in
16 }
17 ],
18 "title": "Product Characteristics APIs",
19 "version": '',
20 "termsOfService": "",
21 "static_url_path": "/characteristics/static",
22 "specs_route": "/characteristics/swagger/",
23 "description": "",
24 "securityDefinitions": {
25 "oAuthSample": {
26 "type": "oauth2",
27 "flow": "application",
28 "tokenUrl": "https://api.pgsmartshopassistant.com/o/token/",
29 }
30 }
31 }
32
33
34 colors_spec = {
35 "tags": [
36 "colors"
37 ],
38 "parameters": [
39 {
40 "name": "palette",
41 "in": "path",
42 "type": "string",
43 "enum": [
44 "all",
45 "rgb",
46 "cmyk"
47 ],
48 "required": True,
49 "default": "all",
50 "description": "Which palette to filter?"
51 }
52 ],
53 "operationId": "get_colors",
54 "consumes": [
55 "application/json"
56 ],
57 "produces": [
58 "application/json"
59 ],
60 "security": {
61 "colors_oauth": {
62 "$ref": "#/securityDefinitions/oAuthSample"
63 }
64 },
65 "schemes": [
66 "http",
67 "https"
68 ],
69 "externalDocs": {
70 "description": "Project repository",
71 "url": "http://github.com/rochacbruno/flasgger"
72 },
73 "definitions": {
74 "Palette": {
75 "type": "object",
76 "properties": {
77 "palette_name": {
78 "type": "array",
79 "items": {
80 "$ref": "#/definitions/Color"
81 }
82 }
83 }
84 },
85 "Color": {
86 "type": "string"
87 }
88 },
89 "responses": {
90 "200": {
91 "description": "A list of colors (may be filtered by palette)",
92 "schema": {
93 "$ref": "#/definitions/Palette"
94 },
95 "examples": {
96 "rgb": [
97 "red",
98 "green",
99 "blue"
100 ]
101 }
102 }
103 }
104 }
105
106
107 @app.route('/colors/<palette>/')
108 @swag_from(colors_spec)
109 def colors(palette):
110 """
111 Example using a dictionary as specification
112 This is the description
113 You can also set 'summary' and 'description' in
114 specs_dict
115 ---
116 # values here overrides the specs dict
117 """
118 all_colors = {
119 'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
120 'rgb': ['red', 'green', 'blue']
121 }
122 if palette == 'all':
123 result = all_colors
124 else:
125 result = {palette: all_colors.get(palette)}
126
127 return jsonify(result)
128
129
130 swag = Swagger(app, config=swagger_config_partial, merge=True)
131
132
133 def test_swag(client, specs_data):
134 """
135 This test is runs automatically in Travis CI
136
137 :param client: Flask app test client
138 :param specs_data: {'url': {swag_specs}} for every spec in app
139 """
140 for spec in specs_data.values():
141 assert 'securityDefinitions' in spec
142 assert 'oAuthSample' in spec['securityDefinitions']
143
144
145 if __name__ == '__main__':
146 app.run(debug=True)