Skip to content

Commit

Permalink
👔 Add query checking
Browse files Browse the repository at this point in the history
  • Loading branch information
lleans committed Mar 14, 2024
1 parent 5446d76 commit 40a76ac
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ async def search(query: Annotated[str, Query(title="Track keyword", description=
if len(data) > 0:
resp.data = data
else:
raise LFException(message="Query not found", was_generic=True, http_code=404)
raise LFException(message="Query not found",
was_generic=True, http_code=404)

return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status)

Expand Down Expand Up @@ -258,7 +259,7 @@ async def search(query: Annotated[str, Query(title="Track keyword", description=
}}
})
@cache(expire=86400)
async def track(trackid: Annotated[str, Query(title="Track id", description="Pass your track id into here")], request: Request) -> JSONResponse:
async def track(trackid: Annotated[str, Query(title="Track id", description="Pass your track id into here", pattern="^(lfid|rovi|apple|deezer|isrc):([^,\s]+)$")], request: Request) -> JSONResponse:
'''
Get a track from database, specifically by using metadata/trackid.
Expand All @@ -283,7 +284,8 @@ async def track(trackid: Annotated[str, Query(title="Track id", description="Pas
if data:
resp.data = data
else:
raise LFException(message="Track not found", was_generic=True, http_code=404)
raise LFException(message="Track not found",
was_generic=True, http_code=404)

return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status)

Expand Down Expand Up @@ -382,7 +384,7 @@ async def track(trackid: Annotated[str, Query(title="Track id", description="Pas
}}
})
@cache(expire=86400)
async def lyric(lfid: Annotated[str, Query(title="Lfid", description="Pass your lfid into here(get it from the request before ```/track``` and ```/search```)")], request: Request) -> JSONResponse:
async def lyric(lfid: Annotated[str, Query(title="Lfid", description="Pass your lfid into here(get it from the request before ```/track``` and ```/search```)", pattern="^\d{3}-\d+$")], request: Request) -> JSONResponse:
'''
Get lyric from database, by given track
'''
Expand All @@ -402,7 +404,8 @@ async def lyric(lfid: Annotated[str, Query(title="Lfid", description="Pass your
if data:
resp.data = data
else:
raise LFException(message="Lyric not found", was_generic=True, http_code=404)
raise LFException(message="Lyric not found",
was_generic=True, http_code=404)

return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status)

Expand Down Expand Up @@ -690,7 +693,7 @@ async def lyric(lfid: Annotated[str, Query(title="Lfid", description="Pass your
}}
})
@cache(expire=86400)
async def translation(lfid: Annotated[str, Query(title="Lfid", description="Pass your lfid into here(get it from the request before ```/track``` and ```/search``` and ```/lyric```)")],
async def translation(lfid: Annotated[str, Query(title="Lfid", description="Pass your lfid into here(get it from the request before ```/track``` and ```/search``` and ```/lyric```)", pattern="^\d{3}-\d+$")],
lang: Annotated[str, Query(title="Selected language", description="Pass your target language to translate(make sure the target language exist in track data)")] = 'en', *, request: Request):
'''
Get translated lyric from database, by given track and language.
Expand All @@ -714,7 +717,8 @@ async def translation(lfid: Annotated[str, Query(title="Lfid", description="Pass
if data:
resp.data = data
else:
raise LFException(message="Translation not found", was_generic=True, http_code=404)
raise LFException(message="Translation not found",
was_generic=True, http_code=404)

return JSONResponse(content=jsonable_encoder(resp), status_code=resp.status)

Expand Down

0 comments on commit 40a76ac

Please sign in to comment.