From 4e4267de0d041e2e4a58908d2fb8971d02ffe406 Mon Sep 17 00:00:00 2001 From: lleans Date: Wed, 13 Mar 2024 20:46:23 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Code=20optimization=20and?= =?UTF-8?q?=20bug=20fixing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router.py | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/router.py b/router.py index a591e0a..f456618 100644 --- a/router.py +++ b/router.py @@ -4,7 +4,7 @@ from aiohttp import ClientSession -from pydantic import BaseModel, Field +from pydantic import BaseModel from fastapi import FastAPI, Request from fastapi.responses import JSONResponse, RedirectResponse @@ -47,9 +47,7 @@ async def search(query: str, request: Request) -> JSONResponse: ''' Get List of all tracks from database, based by keyword ''' - resp: ModelResponse = ModelResponse() - resp.status = 200 - resp.message = "OK" + resp: ModelResponse = ModelResponse(status=200, message="OK", data={}) if query: async with ClientSession() as sess: @@ -85,9 +83,7 @@ async def track(trackid: str, request: Request) -> JSONResponse: this method apply to other metadata, that specifiaclly refrence into the track.\n for reference on metadata, check ```Model``` class. ''' - resp: ModelResponse = ModelResponse() - resp.status = 200 - resp.message = "OK" + resp: ModelResponse = ModelResponse(status=200, message="OK", data={}) if trackid: async with ClientSession() as sess: @@ -118,9 +114,7 @@ async def lyric(lfid: str, request: Request) -> JSONResponse: ''' Get lyric from database, by given track ''' - resp: ModelResponse = ModelResponse() - resp.status = 200 - resp.message = "OK" + resp: ModelResponse = ModelResponse(status=200, message="OK", data={}) if lfid: async with ClientSession() as sess: @@ -153,9 +147,7 @@ async def translation(lfid: str, lang: str = 'en', *, request: Request): This also dynamically check, if passed language is exist or not, if doesnt, will throw exception. ''' - resp: ModelResponse = ModelResponse() - resp.status = 200 - resp.message = "OK" + resp: ModelResponse = ModelResponse(status=200, message="OK", data={}) if lfid: async with ClientSession() as sess: @@ -178,31 +170,27 @@ async def translation(lfid: str, lang: str = 'en', *, request: Request): @app.exception_handler(404) -async def error_handling_lf(_, exec: Exception) -> JSONResponse | RedirectResponse: - if isinstance(exec, LFException): - resp: ModelResponse = ModelResponse() - resp.status = exec.http_code - resp.message = exec.message - return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status) - +async def error_handling_lf(_, __) -> RedirectResponse: return RedirectResponse(url='/docs') @app.exception_handler(RequestValidationError) async def validation_handling(_, __) -> JSONResponse: - resp: ModelResponse = ModelResponse() - resp.status = 400 - resp.message = "Bad request, please check your datatypes or make sure to fill all parameter" + resp: ModelResponse = ModelResponse(status=400, message="Bad request, please check your datatypes or make sure to fill all parameter", data={}) return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status) @app.exception_handler(500) +@app.exception_handler(LFException) async def error_handling(_, exec: Exception) -> JSONResponse: - resp: ModelResponse = ModelResponse() - resp.status = 500 - resp.message = "Something went wrong!! " + str(exec) + resp: ModelResponse = ModelResponse(status=500, message="Something went wrong!! " + str(exec), data={}) + + if isinstance(exec, LFException): + resp.status = exec.http_code or 500 + resp.message = exec.message + return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status) if __name__ == "__main__": uvicorn.run("router:app", host="0.0.0.0", - port=int(environ.get('PORT')) or 8000, log_level="info", workers=3, forwarded_allow_ips="*") + port=5000, log_level="info", workers=3, forwarded_allow_ips="*")