diff --git a/README.md b/README.md index 7b278a67..270b407f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The required python libraries: - [flask-restful](https://github.com/flask-restful/flask-restful/) - [mypysql](https://pymysql.readthedocs.io/en/latest/) - [requests](https://2.python-requests.org/en/master/) + - [pyyaml](https://pyyaml.org/wiki/PyYAMLDocumentation) (For the swagger generation) ## Setup and running the API The database connection needs to be set up first. This is set in config.json diff --git a/src/swagger/swagger_generator.py b/src/swagger/swagger_generator.py index 5a65aad5..c8f8b9d9 100644 --- a/src/swagger/swagger_generator.py +++ b/src/swagger/swagger_generator.py @@ -1,6 +1,8 @@ import re from pathlib import Path +import yaml + from common.config import config @@ -42,185 +44,5 @@ def write_swagger_spec(self): target.write(self.get_yaml_paths()) target.close() - @staticmethod - def get_yaml_top(): - """ - Gets the top part of the openapi spec without the paths - - :return: String containing the top part of the openapi spec - """ - return (f'''openapi: "3.0.0" -info: - title: DataGateway API - description: ICAT API to interface with the Data Gateway - version: "0" -servers: - - url: http://localhost:5000 - -paths:''') - - def get_yaml_paths(self): - """ - Gets the paths for the openapi spec - - :return: String containing the paths and their methods of the openapi spec - """ - base = "" - for endpoint in self.endpoints: - base += f''' - /{endpoint.lower()}: - get: - summary: Get {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - tags: - - Entities - parameters: - - name: where - in: query - description: Apply a where filter to the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - required: false - schema: - type: object - - name: limit - in: query - description: Limit the number of {SwaggerGenerator.pascal_to_normal(endpoint).lower()} returned - required: false - schema: - type: object - - name: skip - in: query - description: Skip the number of {SwaggerGenerator.pascal_to_normal(endpoint).lower()} returned - required: false - schema: - type: object - - name: order - in: query - description: order the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} by the given field - required: false - schema: - type: object - - name: include - in: query - description: include the related entities given - required: false - schema: - type: object - responses: - '200': - description: The {SwaggerGenerator.pascal_to_normal(endpoint).lower()} found. - '404': - description: When no result is found - post: - summary: Create one of more {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - tags: - - Entities - responses: - '200': - description: The created {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - patch: - summary: Update one or multiple {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - tags: - - Entities - responses: - 200: - description: The updated {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - /{endpoint.lower()}/count: - get: - summary: Return the count of the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - tags: - - Entities - parameters: - - name: where - in: query - description: Apply a where filter to the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - required: false - schema: - type: object - responses: - 200: - description: The count of {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - /{endpoint.lower()}/findOne: - get: - summary: Return the first {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matching a given filter - tags: - - Entities - parameters: - - name: where - in: query - description: Apply a where filter to the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - required: false - schema: - type: object - - name: skip - in: query - description: Skip the number of {SwaggerGenerator.pascal_to_normal(endpoint).lower()} returned - required: false - schema: - type: object - - name: order - in: query - description: order the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} by the given field - required: false - schema: - type: object - - name: include - in: query - description: include the related entities given - required: false - schema: - type: object - responses: - 200: - description: The first {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matching - /{endpoint.lower()}/{{id}}: - get: - summary: Find the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matching the given ID - tags: - - Entities - parameters: - - in: path - name: id - required: true - schema: - type: integer - description: The id matching the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - responses: - '200': - description: The matching {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - '404': - description: When no {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matches the given ID - delete: - summary: Delete the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matching the given ID - tags: - - Entities - parameters: - - in: path - name: id - required: true - schema: - type: integer - description: The id matching the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - responses: - '203': - description: Blank responses, when the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} is deleted - '404': - description: When the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} can't be found - patch: - summary: Update the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} matching the given ID - tags: - - Entities - parameters: - - in: path - name: id - required: true - schema: - type: integer - description: The id matching the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - responses: - '200': - description: The updated {SwaggerGenerator.pascal_to_normal(endpoint).lower()} - '404': - description: When the {SwaggerGenerator.pascal_to_normal(endpoint).lower()} can't be found''' - return base - swagger_gen = SwaggerGenerator()