| 1 | try: |
| 2 | import simplejson as json |
| 3 | except ImportError: |
| 4 | import json |
| 5 | try: |
| 6 | from http import HTTPStatus |
| 7 | except ImportError: |
| 8 | import httplib as HTTPStatus |
| 9 | from flask import Flask |
| 10 | from flask import jsonify |
| 11 | from flasgger import Swagger |
| 12 | from flasgger import swag_from |
| 13 | |
| 14 | app = Flask(__name__) |
| 15 | swag = Swagger(app) |
| 16 | |
| 17 | |
| 18 | @app.route("/officer/<int:priority>", methods=['POST']) |
| 19 | @swag_from("officer_specs.yml") |
| 20 | def create_officer(priority): |
| 21 | return 'Request for officer creation successfully received' \ |
| 22 | ' (priority: %i)'.format(priority), HTTPStatus.OK |
| 23 | |
| 24 | |
| 25 | @app.route('/schema/<string:schema_id>', methods=['GET']) |
| 26 | def get_schema(schema_id): |
| 27 | """ |
| 28 | Test schema retrieval |
| 29 | |
| 30 | This endpoint returns a schema known to Flasgger |
| 31 | --- |
| 32 | tags: |
| 33 | - schema |
| 34 | produces: |
| 35 | - application/json |
| 36 | parameters: |
| 37 | - in: path |
| 38 | name: schema_id |
| 39 | type: string |
| 40 | description: schema_id to be retrieved |
| 41 | required: true |
| 42 | |
| 43 | responses: |
| 44 | 200: |
| 45 | description: The requested schema |
| 46 | """ |
| 47 | return jsonify(swag.get_schema(schema_id)), HTTPStatus.OK |
| 48 | |
| 49 | |
| 50 | def test_swag(client, specs_data): |
| 51 | """ |
| 52 | This test is runs automatically in Travis CI |
| 53 | |
| 54 | :param client: Flask app test client |
| 55 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 56 | """ |
| 57 | response = client.get('/schema/officer') |
| 58 | assert response.status_code == HTTPStatus.OK |
| 59 | |
| 60 | retrieved_schema = json.loads(response.data.decode('utf-8')) |
| 61 | actual_schema = specs_data['/apispec_1.json']['definitions']['Officer'] |
| 62 | try: |
| 63 | assert retrieved_schema.viewitems() >= actual_schema.viewitems() |
| 64 | except AttributeError: |
| 65 | assert retrieved_schema.items() >= actual_schema.items() |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | app.run(debug=True) |