Debian Codegraph @

examples/colors_with_schema.py

raw symbols SCIP
1 """
2 Example using Marshmallow Schema and fields as definitions
3 """
4 # coding: utf-8
5 from flask import Flask, jsonify
6
7 from flasgger import Schema, Swagger, SwaggerView, fields
8
9
10 class Color(Schema):
11 name = fields.Str()
12
13
14 class Palette(Schema):
15 pallete_name = fields.Str()
16 colors = fields.Nested(Color, many=True)
17
18
19 class PaletteView(SwaggerView):
20 parameters = [
21 {
22 "name": "palette",
23 "in": "path",
24 "type": "string",
25 "enum": ["all", "rgb", "cmyk"],
26 "required": True,
27 "default": "all"
28 }
29 ]
30 # definitions = {'Palette': Palette, 'Color': Color}
31 responses = {
32 200: {
33 "description": "A list of colors (may be filtered by palette)",
34 "schema": Palette
35 }
36 }
37
38 def get(self, palette):
39 """
40 Colors API using schema
41 This example is using marshmallow schemas
42 """
43 all_colors = {
44 'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
45 'rgb': ['red', 'green', 'blue']
46 }
47 if palette == 'all':
48 result = all_colors
49 else:
50 result = {palette: all_colors.get(palette)}
51 return jsonify(result)
52
53
54 app = Flask(__name__)
55 swag = Swagger(app)
56
57 app.add_url_rule(
58 '/colors/<palette>',
59 view_func=PaletteView.as_view('colors'),
60 methods=['GET']
61 )
62
63 if __name__ == "__main__":
64 app.run(debug=True)