Skip to content

Commit

Permalink
feat(backend): follow proper Rest API spec and add responses to OpenA…
Browse files Browse the repository at this point in the history
…PI spec

Signed-off-by: Jordan Shatford <jordanshatford@live.com>
  • Loading branch information
jordanshatford committed Aug 21, 2023
1 parent acadbaa commit f130ecb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 60 deletions.
57 changes: 27 additions & 30 deletions backend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
}
}
},
"404": {
"description": "No results for search"
},
"422": {
"description": "Validation Error",
"content": {
Expand Down Expand Up @@ -120,12 +123,12 @@
}
},
"responses": {
"200": {
"201": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Message"
"$ref": "#/components/schemas/Video-Output"
}
}
}
Expand Down Expand Up @@ -181,6 +184,9 @@
}
}
},
"404": {
"description": "Download not found."
},
"422": {
"description": "Validation Error",
"content": {
Expand Down Expand Up @@ -220,15 +226,11 @@
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Message"
}
}
}
"204": {
"description": "Successful Response"
},
"404": {
"description": "Download not found."
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -272,7 +274,17 @@
],
"responses": {
"200": {
"description": "Successful Response"
"description": "Successful Response",
"content": {
"audio/*": {
"schema": {
"type": "file"
}
}
}
},
"404": {
"description": "File not found."
},
"422": {
"description": "Validation Error",
Expand Down Expand Up @@ -325,6 +337,9 @@
}
}
},
"404": {
"description": "Download not found."
},
"422": {
"description": "Validation Error",
"content": {
Expand Down Expand Up @@ -462,24 +477,6 @@
"type": "object",
"title": "HTTPValidationError"
},
"Message": {
"properties": {
"title": {
"type": "string",
"title": "Title"
},
"message": {
"type": "string",
"title": "Message"
}
},
"type": "object",
"required": [
"title",
"message"
],
"title": "Message"
},
"Session": {
"properties": {
"id": {
Expand Down
67 changes: 44 additions & 23 deletions backend/routers/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
from fastapi import APIRouter
from fastapi import HTTPException
from fastapi import Request
from fastapi import Response
from fastapi import status
from fastapi.responses import FileResponse
from sse_starlette.sse import EventSourceResponse
from utils.managers import session_manager
from utils.models import Message
from utils.models import StatusUpdate
from utils.models import Video

router = APIRouter()


@router.post('/downloads', tags=['downloads'])
def post_download(video: Video, session_id: str) -> Message:
@router.post('/downloads', tags=['downloads'], status_code=status.HTTP_201_CREATED) # noqa E501
def post_download(video: Video, session_id: str) -> Video:
download_manager = session_manager.get_download_manager(session_id)
download_manager.add(video)
return Message(
title='Video Added',
message='Video added to be downloaded.',
)
return video


async def status_stream(request: Request, session_id: str):
Expand All @@ -50,41 +45,67 @@ async def get_downloads_status(request: Request, session_id: str):
return EventSourceResponse(event_source)


@router.get('/downloads/{video_id}', tags=['downloads'])
def get_download(video_id: str, session_id: str) -> Video: # noqa E501
@router.get(
'/downloads/{video_id}', tags=['downloads'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'Download not found.',
},
},
)
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)
if download is None:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
else:
return download.video


@router.get('/downloads/{video_id}/file', tags=['downloads'], response_class=FileResponse) # noqa E501
@router.get(
'/downloads/{video_id}/file', tags=['downloads'], response_class=FileResponse, responses={ # noqa E501
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):
download_manager = session_manager.get_download_manager(session_id)
download = download_manager.get(video_id)
if download is not None and os.path.exists(download.get_file_location()):
return FileResponse(download.get_file_location())
else:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)


@router.get('/downloads/{video_id}/status', tags=['downloads'])
def get_download_status(video_id: str, response: Response, session_id: str) -> StatusUpdate: # noqa E501
@router.get(
'/downloads/{video_id}/status', tags=['downloads'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'Download not found.',
},
},
)
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)
if download is None:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
else:
return StatusUpdate(id=download.video.id, status=download.status)


@router.delete('/downloads/{video_id}', tags=['downloads'])
def delete_download(video_id: str, session_id: str) -> Message:
@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.',
},
},
)
def delete_download(video_id: str, session_id: str) -> None:
download_manager = session_manager.get_download_manager(session_id)
download_manager.remove(video_id)
return Message(
title='File Removed',
message='The requested file has been removed from the server.',
)
if video_id in download_manager:
download_manager.remove(video_id)
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
16 changes: 14 additions & 2 deletions backend/routers/search.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from fastapi import APIRouter
from fastapi import HTTPException
from fastapi import status
from utils.models import Video
from utils.youtube import search_youtube

router = APIRouter()


@router.get('/search', tags=['search'])
@router.get(
'/search', tags=['search'], responses={
status.HTTP_404_NOT_FOUND: {
'description': 'No results for search',
},
},
)
def get_search(term: str, results: int = 12) -> list[Video]:
return search_youtube(term, results)
videos = search_youtube(term, results)
if len(videos) == 0:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
else:
return videos
5 changes: 0 additions & 5 deletions backend/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ class Session(BaseModel):
id: str


class Message(BaseModel):
title: str
message: str


class StatusUpdate(BaseModel):
id: str
status: Status

0 comments on commit f130ecb

Please sign in to comment.