generate_swagger.py 4.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
from flask import Flask
from flasgger import Swagger

# from . import properties
from formats.message_wrapper import message_wrapper_schema
from formats.mission_plan import mission_plan_schema
from formats.observation import observation_schema
from formats.planning_configuration import planning_configuration_schema
from formats.platform_status import platform_status_message_schema
10
from formats.acknowledgement import acknowledgement_schema
11 12 13 14 15 16 17

app = Flask(__name__)

swagger_config = {
    "headers": [],
    "openapi": "3.0.2",
    "swagger_ui": True,
18
    "specs_route": "/",
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    "info": {
        "title": "Backbone Message Formats",
        "version": "0.1",
        "description": "SoAR message schemas (i.e. formats)",
    },
    "specs": [
        {
            "endpoint": "swagger",
            "route": "/swagger.json",
            "rule_filter": lambda rule: True,
            "model_filter": lambda tag: True,
        }
    ],
    "components": {
        "schemas": {
            "Message": message_wrapper_schema,
            "MissionPlan": mission_plan_schema,
            "Observation": observation_schema,
            "PlanningConfiguration": planning_configuration_schema,
            "PlatformStatus": platform_status_message_schema,
39
            "Acknowledgement": acknowledgement_schema,
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        },
    },
    "paths": {
        "/all_messages": {
            "get": {
                "description": "Returns all messages from the system.",
                "responses": {
                    "200": {
                        "description": "A list of messages.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "oneOf": [
                                        {
                                            "$ref": "#/components/"
55
                                            + "schemas/Acknowledgement"
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlatformStatus"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/MissionPlan"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/Observation"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlanningConfiguration"
                                        },
                                        {
                                            "$ref": "#/components/"
                                            + "schemas/PlatformStatus"
                                        },
                                    ],
                                    "discriminator": {
                                        "propertyName": "message_type",
                                    },
                                }
                            }
                        },
                    }
                },
            }
        },
    },
    "produces": ["application/json"],
    "consumes": ["application/json"],
}
message_types = [
93
    # "Message",
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    "Acknowledgement",
    "MissionPlan",
    "Observation",
    "PlanningConfiguration",
    "PlatformStatus",
]
for item in message_types:
    swagger_config["paths"]["/" + str(item.lower())] = {
        "get": {
            "description": "Returns message for " + item,
            "responses": {
                "200": {
                    "description": item + " message.",
                    "content": {
                        "application/json": {
                            "schema": {
                                "allOf": [
                                    {
                                        "$ref": "#/components/schemas"
                                        + "/" + item,
                                    },
                                ],
                                "discriminator": {
                                    "propertyName": "message_type",
                                },
                            }
                        }
                    },
                }
            },
        }
    }

swag = Swagger(app, config=swagger_config, merge=True)

# app.add_url_rule(
#     '/coordinates',
#     view_func=Coordinates.as_view('coordinates'),
#     methods=['GET']
# )

if __name__ == "__main__":
    app.run(debug=True)