Skip to content

Commit

Permalink
feat(backend): simplify routers with common responses and tags
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Aug 22, 2023
1 parent f332b64 commit 1a1db4d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
13 changes: 8 additions & 5 deletions backend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}
},
"404": {
"description": "No results for search"
"description": "Not Found"
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -133,6 +133,9 @@
}
}
},
"404": {
"description": "Not Found"
},
"422": {
"description": "Validation Error",
"content": {
Expand Down Expand Up @@ -185,7 +188,7 @@
}
},
"404": {
"description": "Download not found."
"description": "Not Found"
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -230,7 +233,7 @@
"description": "Successful Response"
},
"404": {
"description": "Download not found."
"description": "Not Found"
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -284,7 +287,7 @@
}
},
"404": {
"description": "File not found."
"description": "Not Found"
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -338,7 +341,7 @@
}
},
"404": {
"description": "Download not found."
"description": "Not Found"
},
"422": {
"description": "Validation Error",
Expand Down
41 changes: 13 additions & 28 deletions backend/routers/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
from utils.models import StatusUpdate
from utils.models import Video

router = APIRouter()
router = APIRouter(
prefix='/downloads',
tags=['downloads'],
responses={
status.HTTP_404_NOT_FOUND: {},
},
)


@router.post('/downloads', tags=['downloads'], status_code=status.HTTP_201_CREATED) # noqa E501
@router.post('', status_code=status.HTTP_201_CREATED)
def post_download(video: Video, session_id: str) -> Video:
download_manager = session_manager.get_download_manager(session_id)
download_manager.add(video)
Expand All @@ -39,19 +45,13 @@ async def status_stream(request: Request, session_id: str):
session_manager.remove(session_id)


@router.get('/downloads/status', tags=['downloads'], include_in_schema=False)
@router.get('/status', include_in_schema=False)
async def get_downloads_status(request: Request, session_id: str):
event_source = status_stream(request, session_id=session_id)
return EventSourceResponse(event_source)


@router.get(
'/downloads/{video_id}', tags=['downloads'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'Download not found.',
},
},
)
@router.get('/{video_id}')
def get_download(video_id: str, session_id: str) -> Video:
download_manager = session_manager.get_download_manager(session_id)
download = download_manager.get(video_id)
Expand All @@ -62,13 +62,10 @@ def get_download(video_id: str, session_id: str) -> Video:


@router.get(
'/downloads/{video_id}/file', tags=['downloads'], response_class=FileResponse, responses={ # noqa E501
'/{video_id}/file', response_class=FileResponse, responses={
status.HTTP_200_OK: {
'content': {'audio/*': {'schema': {'type': 'file'}}},
},
status.HTTP_404_NOT_FOUND: {
'description': 'File not found.',
},
},
)
def get_download_file(video_id: str, session_id: str):
Expand All @@ -80,13 +77,7 @@ def get_download_file(video_id: str, session_id: str):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)


@router.get(
'/downloads/{video_id}/status', tags=['downloads'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'Download not found.',
},
},
)
@router.get('/{video_id}/status')
def get_download_status(video_id: str, session_id: str) -> StatusUpdate:
download_manager = session_manager.get_download_manager(session_id)
download = download_manager.get(video_id)
Expand All @@ -96,13 +87,7 @@ def get_download_status(video_id: str, session_id: str) -> StatusUpdate:
return StatusUpdate(id=download.video.id, status=download.status)


@router.delete(
'/downloads/{video_id}', tags=['downloads'], status_code=status.HTTP_204_NO_CONTENT, responses={ # noqa E501
status.HTTP_404_NOT_FOUND: {
'description': 'Download not found.',
},
},
)
@router.delete('/{video_id}', status_code=status.HTTP_204_NO_CONTENT)
def delete_download(video_id: str, session_id: str) -> None:
download_manager = session_manager.get_download_manager(session_id)
if video_id in download_manager:
Expand Down
16 changes: 8 additions & 8 deletions backend/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from utils.models import Video
from utils.youtube import search_youtube

router = APIRouter()


@router.get(
'/search', tags=['search'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'No results for search',
},
router = APIRouter(
prefix='/search',
tags=['search'],
responses={
status.HTTP_404_NOT_FOUND: {},
},
)


@router.get('')
def get_search(term: str, results: int = 12) -> list[Video]:
videos = search_youtube(term, results)
if len(videos) == 0:
Expand Down
7 changes: 5 additions & 2 deletions backend/routers/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from utils.managers import session_manager
from utils.models import Session

router = APIRouter()
router = APIRouter(
prefix='/session',
tags=['session'],
)


@router.get('/session', tags=['session'])
@router.get('')
def get_session() -> Session:
session_id = session_manager.create()
return Session(id=session_id)

0 comments on commit 1a1db4d

Please sign in to comment.