| 1 | """ |
| 2 | The simple example using OpenAPI 3.0 callbacks |
| 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': 'OA3 Callbacks', |
| 12 | 'openapi': '3.0.2' |
| 13 | } |
| 14 | Swagger(app) |
| 15 | |
| 16 | |
| 17 | @app.route('/run_callback/', methods=['POST']) |
| 18 | def run_callback(): |
| 19 | """Example endpoint that specifies OA3 callbacks |
| 20 | This is using docstring for specifications |
| 21 | --- |
| 22 | tags: |
| 23 | - callbacks |
| 24 | requestBody: |
| 25 | description: Test |
| 26 | required: true |
| 27 | content: |
| 28 | application/json: |
| 29 | schema: |
| 30 | properties: |
| 31 | callback_url: |
| 32 | type: string |
| 33 | format: uri |
| 34 | description: Callback URL for request |
| 35 | callbacks: |
| 36 | onSomeEvent: |
| 37 | '{$request.body.callback_url}': |
| 38 | post: |
| 39 | requestBody: |
| 40 | description: status payload |
| 41 | content: |
| 42 | application/json: |
| 43 | schema: |
| 44 | properties: |
| 45 | status: |
| 46 | type: string |
| 47 | """ |
| 48 | |
| 49 | return jsonify({'result': 'ok'}) |
| 50 | |
| 51 | |
| 52 | def test_swag(client, specs_data): |
| 53 | """ |
| 54 | This test is runs automatically in Travis CI |
| 55 | |
| 56 | :param client: Flask app test client |
| 57 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 58 | """ |
| 59 | for url, spec in specs_data.items(): |
| 60 | assert 'openapi' in spec |
| 61 | assert spec['openapi'] == '3.0.2' |
| 62 | |
| 63 | assert 'callbacks' in spec['paths']['/run_callback/']['post'] |
| 64 | assert 'onSomeEvent' in \ |
| 65 | spec['paths']['/run_callback/']['post']['callbacks'] |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | app.run(debug=True) |