From b4c1565fbb1057b40a925da8ca6468b5494d9511 Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 3 Feb 2022 11:36:26 -0700 Subject: [PATCH] Using new openapi.definition to show validation in docs. --- modules/archive/api.py | 5 ++++- modules/files/api.py | 14 ++++++++++++-- modules/inventory/api.py | 20 ++++++++++++++++---- modules/otp/api.py | 10 ++++++++-- modules/videos/api.py | 5 ++++- modules/videos/channel/api.py | 11 ++++++++--- modules/videos/video/api.py | 10 ++++++++-- wrolpi/root_api.py | 3 --- 8 files changed, 60 insertions(+), 18 deletions(-) diff --git a/modules/archive/api.py b/modules/archive/api.py index 086afb3f..98e861a9 100644 --- a/modules/archive/api.py +++ b/modules/archive/api.py @@ -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) diff --git a/modules/files/api.py b/modules/files/api.py index 45c78f92..3ab8521d 100644 --- a/modules/files/api.py +++ b/modules/files/api.py @@ -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) diff --git a/modules/inventory/api.py b/modules/inventory/api.py index a4923113..f9f455ad 100644 --- a/modules/inventory/api.py +++ b/modules/inventory/api.py @@ -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('/') -@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('//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/') -@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): diff --git a/modules/otp/api.py b/modules/otp/api.py index 4726eddd..fa03aea3 100644 --- a/modules/otp/api.py +++ b/modules/otp/api.py @@ -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']) diff --git a/modules/videos/api.py b/modules/videos/api.py index 79c71aa2..f779f061 100644 --- a/modules/videos/api.py +++ b/modules/videos/api.py @@ -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) diff --git a/modules/videos/channel/api.py b/modules/videos/channel/api.py index eff63a74..013f0e5a 100644 --- a/modules/videos/channel/api.py +++ b/modules/videos/channel/api.py @@ -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('/') -@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) diff --git a/modules/videos/video/api.py b/modules/videos/video/api.py index 75b51040..60d83a14 100644 --- a/modules/videos/video/api.py +++ b/modules/videos/video/api.py @@ -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) diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 9905a477..2f94f392 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -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),