Skip to content

Commit

Permalink
removed all HTTPException try/catch blocks and replaced with FastAPI …
Browse files Browse the repository at this point in the history
…exception handler, moved auth module into core/security module, expanded initial API endpoints/models/schemas with no security layers implemented yet, added custom CSRF module, updated project dependencies
  • Loading branch information
joeygrable94 committed Oct 9, 2023
1 parent 028eb4f commit a5fdc4f
Show file tree
Hide file tree
Showing 73 changed files with 1,726 additions and 918 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: ["ubuntu-latest"]
python-version: ["3.11", "3.10"]
python-version: ["3.11"]
redis-version: ["7"]
env:
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }}
Expand Down
184 changes: 63 additions & 121 deletions app/api/deps/get_db_items.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from typing import Annotated, Any
from uuid import UUID

from fastapi import Depends, HTTPException, status
from fastapi import Depends

from app.api.deps.get_db import AsyncDatabaseSession
from app.api.errors import ErrorCode
from app.api.exceptions import (
ClientNotExists,
EntityIdNotProvided,
InvalidID,
NoteNotExists,
UserNotExists,
WebsiteMapNotExists,
Expand Down Expand Up @@ -45,22 +43,14 @@ async def get_user_or_404(
user_id: Any | None = None,
) -> User | None:
"""Parses uuid/int and fetches user by id."""
try:
if user_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(user_id)
user_repo: UserRepository = UserRepository(session=db)
user: User | None = await user_repo.read(entry_id=parsed_id)
if user is None:
raise UserNotExists()
return user
except (UserNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.USER_NOT_FOUND,
)
except EntityIdNotProvided:
return None
if user_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(user_id)
user_repo: UserRepository = UserRepository(session=db)
user: User | None = await user_repo.read(entry_id=parsed_id)
if user is None:
raise UserNotExists()
return user


FetchUserOr404 = Annotated[User, Depends(get_user_or_404)]
Expand All @@ -71,22 +61,14 @@ async def get_client_or_404(
client_id: Any | None = None,
) -> Client | None:
"""Parses uuid/int and fetches client by id."""
try:
if client_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(client_id)
client_repo: ClientRepository = ClientRepository(session=db)
client: Client | None = await client_repo.read(entry_id=parsed_id)
if client is None:
raise ClientNotExists()
return client
except (ClientNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.CLIENT_NOT_FOUND,
)
except EntityIdNotProvided:
return None
if client_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(client_id)
client_repo: ClientRepository = ClientRepository(session=db)
client: Client | None = await client_repo.read(entry_id=parsed_id)
if client is None:
raise ClientNotExists()
return client


FetchClientOr404 = Annotated[Client, Depends(get_client_or_404)]
Expand All @@ -97,22 +79,14 @@ async def get_note_or_404(
note_id: Any | None = None,
) -> Note | None:
"""Parses uuid/int and fetches note by id."""
try:
if note_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(note_id)
note_repo: NoteRepository = NoteRepository(session=db)
note: Note | None = await note_repo.read(entry_id=parsed_id)
if note is None:
raise NoteNotExists()
return note
except (NoteNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.NOTE_NOT_FOUND,
)
except EntityIdNotProvided:
return None
if note_id is None:
raise EntityIdNotProvided()
parsed_id: UUID = parse_id(note_id)
note_repo: NoteRepository = NoteRepository(session=db)
note: Note | None = await note_repo.read(entry_id=parsed_id)
if note is None:
raise NoteNotExists()
return note


FetchNoteOr404 = Annotated[Note, Depends(get_note_or_404)]
Expand All @@ -123,18 +97,12 @@ async def get_website_or_404(
website_id: Any,
) -> Website | None:
"""Parses uuid/int and fetches website by id."""
try:
parsed_id: UUID = parse_id(website_id)
website_repo: WebsiteRepository = WebsiteRepository(session=db)
website: Website | None = await website_repo.read(entry_id=parsed_id)
if website is None:
raise WebsiteNotExists()
return website
except (WebsiteNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.WEBSITE_NOT_FOUND,
)
parsed_id: UUID = parse_id(website_id)
website_repo: WebsiteRepository = WebsiteRepository(session=db)
website: Website | None = await website_repo.read(entry_id=parsed_id)
if website is None:
raise WebsiteNotExists()
return website


FetchWebsiteOr404 = Annotated[Website, Depends(get_website_or_404)]
Expand All @@ -145,18 +113,12 @@ async def get_website_map_or_404(
sitemap_id: Any,
) -> WebsiteMap:
"""Parses uuid/int and fetches website map by id."""
try:
parsed_id: UUID = parse_id(sitemap_id)
sitemap_repo: WebsiteMapRepository = WebsiteMapRepository(session=db)
sitemap: WebsiteMap | None = await sitemap_repo.read(entry_id=parsed_id)
if sitemap is None:
raise WebsiteMapNotExists()
return sitemap
except (WebsiteMapNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.WEBSITE_MAP_NOT_FOUND,
)
parsed_id: UUID = parse_id(sitemap_id)
sitemap_repo: WebsiteMapRepository = WebsiteMapRepository(session=db)
sitemap: WebsiteMap | None = await sitemap_repo.read(entry_id=parsed_id)
if sitemap is None:
raise WebsiteMapNotExists()
return sitemap


FetchSitemapOr404 = Annotated[WebsiteMap, Depends(get_website_map_or_404)]
Expand All @@ -167,20 +129,12 @@ async def get_website_page_or_404(
page_id: Any,
) -> WebsitePage | None:
"""Parses uuid/int and fetches website page by id."""
try:
parsed_id: UUID = parse_id(page_id)
website_page_repo: WebsitePageRepository = WebsitePageRepository(session=db)
website_page: WebsitePage | None = await website_page_repo.read(
entry_id=parsed_id
)
if website_page is None:
raise WebsitePageNotExists()
return website_page
except (WebsitePageNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.WEBSITE_PAGE_NOT_FOUND,
)
parsed_id: UUID = parse_id(page_id)
website_page_repo: WebsitePageRepository = WebsitePageRepository(session=db)
website_page: WebsitePage | None = await website_page_repo.read(entry_id=parsed_id)
if website_page is None:
raise WebsitePageNotExists()
return website_page


FetchWebPageOr404 = Annotated[WebsitePage, Depends(get_website_page_or_404)]
Expand All @@ -191,22 +145,16 @@ async def get_website_page_psi_or_404(
psi_id: Any,
) -> WebsitePageSpeedInsights | None:
"""Parses uuid/int and fetches website page speed insights by id."""
try:
parsed_id: UUID = parse_id(psi_id)
website_page_psi_repo: WebsitePageSpeedInsightsRepository = (
WebsitePageSpeedInsightsRepository(session=db)
)
website_page_speed_insights: WebsitePageSpeedInsights | None = (
await website_page_psi_repo.read(parsed_id)
)
if website_page_speed_insights is None:
raise WebsitePageSpeedInsightsNotExists()
return website_page_speed_insights
except (WebsitePageSpeedInsightsNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.WEBSITE_PAGE_SPEED_INSIGHTS_NOT_FOUND,
)
parsed_id: UUID = parse_id(psi_id)
website_page_psi_repo: WebsitePageSpeedInsightsRepository = (
WebsitePageSpeedInsightsRepository(session=db)
)
website_page_speed_insights: WebsitePageSpeedInsights | None = (
await website_page_psi_repo.read(parsed_id)
)
if website_page_speed_insights is None:
raise WebsitePageSpeedInsightsNotExists()
return website_page_speed_insights


FetchWebPageSpeedInsightOr404 = Annotated[
Expand All @@ -219,22 +167,16 @@ async def get_website_page_kwc_or_404(
kwc_id: Any,
) -> WebsiteKeywordCorpus | None:
"""Parses uuid/int and fetches website keyword corpus by id."""
try:
parsed_id: UUID = parse_id(kwc_id)
website_page_kwc_repo: WebsiteKeywordCorpusRepository = (
WebsiteKeywordCorpusRepository(session=db)
)
website_keyword_corpus: WebsiteKeywordCorpus | None = (
await website_page_kwc_repo.read(parsed_id)
)
if website_keyword_corpus is None:
raise WebsitePageKeywordCorpusNotExists()
return website_keyword_corpus
except (WebsitePageKeywordCorpusNotExists, InvalidID):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorCode.WEBSITE_PAGE_SPEED_INSIGHTS_NOT_FOUND,
)
parsed_id: UUID = parse_id(kwc_id)
website_page_kwc_repo: WebsiteKeywordCorpusRepository = (
WebsiteKeywordCorpusRepository(session=db)
)
website_keyword_corpus: WebsiteKeywordCorpus | None = (
await website_page_kwc_repo.read(parsed_id)
)
if website_keyword_corpus is None:
raise WebsitePageKeywordCorpusNotExists()
return website_keyword_corpus


FetchWebsiteKeywordCorpusOr404 = Annotated[
Expand Down
3 changes: 1 addition & 2 deletions app/api/deps/get_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def __init__(self, strategy: List[str] | None = None):


class CommonQueryParams(PageQueryParams):
def __init__(self, page: int = Query(1), speak: str | None = Query(None)):
PageQueryParams.__init__(self, page)
def __init__(self, speak: str | None = Query(None)):
self.speak = speak


Expand Down
4 changes: 2 additions & 2 deletions app/api/deps/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from fastapi_permissions import Authenticated # type: ignore # noqa: E501
from fastapi_permissions import Everyone, configure_permissions, has_permission

from app.api.errors import ErrorCode
from app.core.auth import Auth0User, auth
from app.api.exceptions import ErrorCode
from app.core.security import Auth0User, auth


def get_current_user(user: Auth0User | None = Security(auth.get_user)) -> Auth0User:
Expand Down
117 changes: 0 additions & 117 deletions app/api/exceptions.py

This file was deleted.

Loading

0 comments on commit a5fdc4f

Please sign in to comment.