Skip to content

Commit

Permalink
#50: Use pyyaml instead of f strings
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Sep 17, 2019
1 parent bb2f913 commit abe4252
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 180 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
182 changes: 2 additions & 180 deletions src/swagger/swagger_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from pathlib import Path

import yaml

from common.config import config


Expand Down Expand Up @@ -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()

0 comments on commit abe4252

Please sign in to comment.