Using new openapi.definition to show validation in docs.

This commit is contained in:
Roland 2022-02-03 11:36:26 -07:00
parent f1f0ebecb0
commit b4c1565fbb
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
8 changed files with 60 additions and 18 deletions

View file

@ -46,7 +46,10 @@ archive_offset_limiter = api_param_limiter(100, 0)
@bp.post('/search')
@openapi.description('Search archive contents and titles')
@openapi.definition(
summary='Search archive contents and titles',
body=ArchiveSearchRequest,
)
@openapi.response(HTTPStatus.OK, ArchiveSearchResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(ArchiveSearchRequest)

View file

@ -43,7 +43,10 @@ def paths_to_files(paths: List[pathlib.Path]):
@bp.post('/')
@openapi.description('List files in a directory')
@openapi.definition(
summary='List files in a directory',
body=schema.FilesRequest,
)
@validate(schema.FilesRequest)
async def get_files(_: Request, body: schema.FilesRequest):
directories = body.directories or []
@ -54,7 +57,10 @@ async def get_files(_: Request, body: schema.FilesRequest):
@bp.post('/delete')
@openapi.description('Delete a single file. Returns an error if WROL Mode is enabled.')
@openapi.definition(
summary='Delete a single file. Returns an error if WROL Mode is enabled.',
body=schema.DeleteRequest,
)
@validate(schema.DeleteRequest)
async def delete_file(_: Request, body: schema.DeleteRequest):
if not body.file:
@ -75,6 +81,10 @@ async def refresh(_: Request):
@bp.post('/search')
@openapi.definition(
summary='Search Files',
body=schema.FilesSearchRequest,
)
@validate(schema.FilesSearchRequest)
async def search_files(_: Request, body: schema.FilesSearchRequest):
files, total = lib.search(body.search_str, body.limit, body.offset)

View file

@ -46,7 +46,10 @@ def inventory_get(_: Request, inventory_id: int):
@bp.post('/')
@openapi.description('Save a new inventory')
@openapi.definition(
summary='Save a new inventory',
body=InventoryPostRequest,
)
@validate(InventoryPostRequest)
@run_after(save_inventories_file)
def post_inventory(_: Request, data: dict):
@ -58,7 +61,10 @@ def post_inventory(_: Request, data: dict):
@bp.put('/<inventory_id:int>')
@openapi.description('Update an inventory')
@openapi.definition(
summary='Update an inventory',
body=InventoryPutRequest,
)
@validate(InventoryPutRequest)
@run_after(save_inventories_file)
def put_inventory(_: Request, inventory_id: int, data: dict):
@ -85,7 +91,10 @@ def items_get(_: Request, inventory_id: int):
@bp.post('/<inventory_id:int>/item')
@openapi.description("Save an item into it's inventory.")
@openapi.definition(
summary="Save an item into it's inventory.",
body=ItemPostRequest,
)
@validate(ItemPostRequest)
@run_after(save_inventories_file)
def post_item(_: Request, inventory_id: int, data: dict):
@ -97,7 +106,10 @@ def post_item(_: Request, inventory_id: int, data: dict):
@bp.put('/item/<item_id:int>')
@openapi.description("Update an item.")
@openapi.definition(
summary='Update an item.',
body=ItemPutRequest,
)
@validate(ItemPutRequest)
@run_after(save_inventories_file)
def put_item(_: Request, item_id: int, data: dict):

View file

@ -15,7 +15,10 @@ bp = get_blueprint('OTP', '/api/otp')
@bp.post('/encrypt_otp')
@openapi.description('Encrypt a message with OTP.')
@openapi.definition(
summary='Encrypt a message with OTP.',
body=EncryptOTPRequest,
)
@validate(EncryptOTPRequest)
async def post_encrypt_otp(_: Request, data: dict):
data = encrypt_otp(data['otp'], data['plaintext'])
@ -23,7 +26,10 @@ async def post_encrypt_otp(_: Request, data: dict):
@bp.post('/decrypt_otp')
@openapi.description('Decrypt a message with OTP.')
@openapi.definition(
summary='Decrypt a message with OTP.',
body=DecryptOTPRequest,
)
@validate(DecryptOTPRequest)
async def post_decrypt_otp(_: Request, data: dict):
data = decrypt_otp(data['otp'], data['ciphertext'])

View file

@ -99,7 +99,10 @@ async def refresh_videos(channel_links: list = None):
@content_bp.post('/favorite')
@openapi.description('Toggle the favorite flag on a video')
@openapi.definition(
description='Toggle the favorite flag on a video',
body=FavoriteRequest,
)
@validate(FavoriteRequest)
@openapi.response(HTTPStatus.OK, FavoriteResponse)
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)

View file

@ -8,7 +8,6 @@ from sanic_ext.extensions.openapi import openapi
from wrolpi.common import logger, wrol_mode_check, make_media_directory, \
get_media_directory
from wrolpi.errors import UnknownDirectory
from wrolpi.root_api import json_response
from wrolpi.schema import JSONErrorResponse
from .lib import get_minimal_channels, delete_channel, update_channel, get_channel, create_channel
@ -38,7 +37,10 @@ def channel_get(_: Request, link: str):
@channel_bp.post('/')
@openapi.description('Insert a Channel')
@openapi.definition(
description='Insert a Channel',
body=ChannelPostRequest,
)
@openapi.response(HTTPStatus.OK, ChannelPostResponse)
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
@validate(ChannelPostRequest)
@ -60,7 +62,10 @@ def channel_post(_: Request, body: ChannelPostRequest):
@channel_bp.put('/<link:str>')
@openapi.description('Update a Channel')
@openapi.definition(
description='Update a Channel',
body=ChannelPutRequest,
)
@openapi.response(HTTPStatus.NO_CONTENT)
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
@validate(ChannelPutRequest)

View file

@ -32,7 +32,10 @@ def video_get(_: Request, video_id: int):
@video_bp.post('/search')
@openapi.description('Search Video titles and captions')
@openapi.definition(
summary='Search Video titles and captions',
body=VideoSearchRequest,
)
@openapi.response(HTTPStatus.OK, VideoSearchResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(VideoSearchRequest)
@ -54,7 +57,10 @@ async def search(_: Request, body: VideoSearchRequest):
@video_bp.post('/directories')
@openapi.description('Get all directories that match the search_str, prefixed by the media directory.')
@openapi.definition(
summary='Get all directories that match the search_str, prefixed by the media directory.',
body=DirectoriesRequest,
)
@openapi.response(HTTPStatus.OK, DirectoriesResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(DirectoriesRequest)

View file

@ -97,9 +97,6 @@ def index(_):
@openapi.description('Echo whatever is sent to this.')
@openapi.response(HTTPStatus.OK, EchoResponse)
async def echo(request: Request):
"""
Returns a JSON object containing details about the request sent to me.
"""
ret = dict(
form=request.form,
headers=dict(request.headers),