| 1 | """ |
| 2 | In this example a spec name is provided in order to trigger SwaggeUI's dropdown list of specs. |
| 3 | """ |
| 4 | from flask import Flask, jsonify |
| 5 | try: |
| 6 | from http import HTTPStatus |
| 7 | except ImportError: |
| 8 | import httplib as HTTPStatus |
| 9 | from flasgger import Swagger |
| 10 | from flasgger.utils import swag_from |
| 11 | |
| 12 | |
| 13 | swagger_config = { |
| 14 | "headers": [ |
| 15 | ], |
| 16 | "specs": [ |
| 17 | { |
| 18 | "version": "0.0.1", |
| 19 | "title": "Api v1", |
| 20 | "name": "v1", |
| 21 | "endpoint": 'v1_spec', |
| 22 | "description": 'This is the version 1 of our API', |
| 23 | "route": '/v1/spec', |
| 24 | "rule_filter": lambda rule: rule.rule.startswith('/v1/'), |
| 25 | }, |
| 26 | { |
| 27 | "version": "0.0.2", |
| 28 | "title": "Api v2", |
| 29 | "name": "v2", |
| 30 | "description": 'This is the version 2 of our API', |
| 31 | "endpoint": 'v2_spec', |
| 32 | "route": '/v2/spec', |
| 33 | "rule_filter": lambda rule: rule.rule.startswith('/v2/'), |
| 34 | } |
| 35 | ], |
| 36 | "static_url_path": "/flasgger_static", |
| 37 | } |
| 38 | |
| 39 | app = Flask(__name__) |
| 40 | swag = Swagger(app, config=swagger_config) |
| 41 | |
| 42 | |
| 43 | |
| 44 | @app.route('/v1/hello') |
| 45 | def v1_hello(): |
| 46 | """ |
| 47 | A test view |
| 48 | |
| 49 | --- |
| 50 | responses: |
| 51 | 200: |
| 52 | description: OK |
| 53 | """ |
| 54 | return jsonify(hello="world") |
| 55 | |
| 56 | |
| 57 | @app.route('/v2/hello') |
| 58 | def v2_hello(): |
| 59 | """ |
| 60 | A test view v2 |
| 61 | |
| 62 | --- |
| 63 | responses: |
| 64 | 200: |
| 65 | description: OK |
| 66 | """ |
| 67 | return jsonify(hello="world") |
| 68 | |
| 69 | |
| 70 | def test_swag(client, specs_data): |
| 71 | """ |
| 72 | This test is runs automatically in Travis CI |
| 73 | |
| 74 | :param client: Flask app test client |
| 75 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 76 | """ |
| 77 | assert client.get('/apidocs/').status_code == HTTPStatus.OK |
| 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | app.run(debug=True) |