Skip to content

Commit

Permalink
⚡️ Code optimization and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
lleans committed Mar 13, 2024
1 parent 3a6aeeb commit 4dd8420
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -178,29 +170,25 @@ 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__":
Expand Down

0 comments on commit 4dd8420

Please sign in to comment.