1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Autogenerate swagger docs for ApiStars

This commit is contained in:
Steffo 2020-03-08 21:42:10 +01:00
parent 7c60046cb7
commit c95bd59c96
7 changed files with 95 additions and 0 deletions

View file

@ -11,6 +11,13 @@ class ApiLoginRoyalnetStar(ApiStar):
methods = ["POST"] methods = ["POST"]
summary = "Login as a Royalnet user, creating a 7-day login token."
parameters = {
"username": "The name of the user you are logging in as.",
"password": "The password of the user you are logging in as."
}
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
TokenT = self.alchemy.get(Token) TokenT = self.alchemy.get(Token)
UserT = self.alchemy.get(User) UserT = self.alchemy.get(User)

View file

@ -6,6 +6,8 @@ import royalnet.utils as ru
class ApiRoyalnetVersionStar(ApiStar): class ApiRoyalnetVersionStar(ApiStar):
path = "/api/royalnet/version/v1" path = "/api/royalnet/version/v1"
summary = "Get the current Royalnet version."
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
return { return {
"semantic": rv.semantic "semantic": rv.semantic

View file

@ -10,6 +10,13 @@ class ApiTokenCreateStar(ApiStar):
methods = ["POST"] methods = ["POST"]
summary = "Create a new login token of any duration."
parameters = {
"token": "Your current login token.",
"duration": "The duration in seconds of the new token."
}
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
user = await data.user() user = await data.user()
try: try:

View file

@ -5,6 +5,12 @@ from royalnet.constellation.api import *
class ApiTokenInfoStar(ApiStar): class ApiTokenInfoStar(ApiStar):
path = "/api/token/info/v1" path = "/api/token/info/v1"
summary = "Get info about a login token."
parameters = {
"token": "The login token to get info about.",
}
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
token = await data.token() token = await data.token()
return token.json() return token.json()

View file

@ -11,6 +11,13 @@ class ApiTokenPasswdStar(ApiStar):
methods = ["POST"] methods = ["POST"]
summary = "Change Royalnet password for an user."
parameters = {
"token": "Your current login token.",
"new_password": "The password you want to set."
}
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
TokenT = self.alchemy.get(Token) TokenT = self.alchemy.get(Token)
token = await data.token() token = await data.token()

View file

@ -11,6 +11,12 @@ import royalnet.utils as ru
class ApiStar(PageStar, ABC): class ApiStar(PageStar, ABC):
summary: str = ""
description: str = ""
parameters: Dict[str, str] = {}
async def page(self, request: Request) -> JSONResponse: async def page(self, request: Request) -> JSONResponse:
if request.query_params: if request.query_params:
data = request.query_params data = request.query_params
@ -40,3 +46,39 @@ class ApiStar(PageStar, ABC):
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
raise NotImplementedError() raise NotImplementedError()
@classmethod
def swagger(cls) -> str:
"""Generate one or more swagger paths for this ApiStar."""
string = [f'{cls.path}:\n']
for method in cls.methods:
string.append(
f' {method.lower()}:\n'
f' summary: "{cls.summary}"\n'
f' description: "{cls.description}"\n'
f' produces:\n'
f' - "application/json"\n'
f' responses:\n'
f' 200:\n'
f' description: "Success"\n'
f' 400:\n'
f' description: "Bad request"\n'
f' 403:\n'
f' description: "Forbidden"\n'
f' 404:\n'
f' description: "Not found"\n'
f' 500:\n'
f' description: "Serverside unhandled exception"\n'
f' 501:\n'
f' description: "Not yet implemented"\n'
)
if len(cls.parameters) > 0:
string.append(f' parameters:\n')
for parameter in cls.parameters:
string.append(
f' - name: "{parameter}"\n'
f' in: "query"\n'
f' description: "{cls.parameters[parameter]}"\n'
f' type: "string"\n'
)
return "".join(string).rstrip("\n")

View file

@ -1,6 +1,7 @@
import toml import toml
import importlib import importlib
import click import click
from .version import semantic
p = click.echo p = click.echo
@ -46,6 +47,29 @@ def run(config_filename, file_format):
for line in lines: for line in lines:
p(line) p(line)
elif file_format == "swagger":
p(
f'swagger: "2.0"\n'
f'info:\n'
f' description: "This is autogenerated Royalnet API documentation."\n'
f' title: "Royalnet"\n'
f' version: "{semantic}"\n'
f'paths:'
)
for pack_name in packs:
pack = packs[pack_name]
try:
page_stars = pack["stars"].available_page_stars
except AttributeError:
p(f"Pack `{pack}` does not have the `available_page_stars` attribute.", err=True)
continue
for page_star in page_stars:
try:
p(f' {page_star.swagger()}')
except AttributeError:
pass
else: else:
raise click.ClickException("Unknown format") raise click.ClickException("Unknown format")