Debian Codegraph @

examples/colors.py

raw symbols SCIP
1 """
2 The simple example using declared definitions.
3 """
4
5 from flask import Flask, jsonify
6
7 from flasgger import Swagger, utils
8
9 app = Flask(__name__)
10 app.config['SWAGGER'] = {
11 'title': 'Colors API'
12 }
13 Swagger(app)
14
15
16 @app.route('/colors/<palette>/')
17 def colors(palette):
18 """Example endpoint return a list of colors by palette
19 This is using docstring for specifications
20 ---
21 tags:
22 - colors
23 parameters:
24 - name: palette
25 in: path
26 type: string
27 enum: ['all', 'rgb', 'cmyk']
28 required: true
29 default: all
30 description: Which palette to filter?
31 operationId: get_colors
32 consumes:
33 - application/json
34 produces:
35 - application/json
36 security:
37 colors_auth:
38 - 'write:colors'
39 - 'read:colors'
40 schemes: ['http', 'https']
41 deprecated: false
42 externalDocs:
43 description: Project repository
44 url: http://github.com/rochacbruno/flasgger
45 definitions:
46 Palette:
47 type: object
48 properties:
49 palette_name:
50 type: array
51 items:
52 $ref: '#/definitions/Color'
53 Color:
54 type: string
55 responses:
56 200:
57 description: A list of colors (may be filtered by palette)
58 schema:
59 $ref: '#/definitions/Palette'
60 examples:
61 rgb: ['red', 'green', 'blue']
62 """
63 all_colors = {
64 'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
65 'rgb': ['red', 'green', 'blue']
66 }
67 if palette == 'all':
68 result = all_colors
69 else:
70 result = {palette: all_colors.get(palette)}
71
72 return jsonify(result)
73
74
75 def test_swag(client, specs_data):
76 """
77 This test is runs automatically in Travis CI
78
79 :param client: Flask app test client
80 :param specs_data: {'url': {swag_specs}} for every spec in app
81 """
82 for url, spec in specs_data.items():
83 assert 'Palette' in spec['definitions']
84 assert 'Color' in spec['definitions']
85 assert 'colors' in spec['paths']['/colors/{palette}/']['get']['tags']
86
87
88 if __name__ == "__main__":
89 app.run(debug=True)