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