Skip to content

Commit

Permalink
feat: standardize responses and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rorre committed Jun 12, 2024
1 parent 2232652 commit 6a6f2f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cefies/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@router.post("/login")
async def login(data: LoginData):
async def login(data: LoginData) -> Token:
user = authenticate_user(data.email, data.password)
if not user:
raise HTTPException(
Expand All @@ -24,7 +24,7 @@ async def login(data: LoginData):


@router.post("/register")
def register(data: RegisterData):
def register(data: RegisterData) -> MessageResponse:
existing_user = User.collection.filter(email=data.email).get()
if existing_user:
raise HTTPException(
Expand Down
23 changes: 18 additions & 5 deletions cefies/routes/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Annotated
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, status
from fastapi.responses import JSONResponse
from cefies.models.firebase import DecodedIdToken
from cefies.models.generate import RecipeGenerationModel
from cefies.models.generate import GenerationErrorModel, RecipeGenerationModel, RecipeListModel
from cefies.logic.recipe_generator import generate_recipe
from cefies.models.response import MessageResponse
from cefies.security import get_current_user


Expand All @@ -15,7 +16,11 @@ def hello_world():
return "Hello world!"


@router.post("/generate/recipes")
@router.post(
"/generate/recipes",
response_model=RecipeListModel,
responses={500: {"model": MessageResponse}},
)
def generate_recipes(
user: Annotated[DecodedIdToken, Depends(get_current_user)],
request_body: RecipeGenerationModel,
Expand All @@ -24,6 +29,14 @@ def generate_recipes(
topic = request_body.topic
recipe = generate_recipe(ingredients, topic)
if recipe:
return JSONResponse(content=recipe.model_dump(), status_code=201)
return recipe

if isinstance(recipe, GenerationErrorModel):
message = recipe.message
else:
return JSONResponse(content={"detail": "invalid model response"}, status_code=500)
message = "Cannot generate recipe"

return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=MessageResponse(error=True, message=message),
)
18 changes: 10 additions & 8 deletions cefies/routes/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
@router.get("/")
def get_profile(
user: Annotated[User, Depends(get_current_user)],
):
) -> ProfileData:
return ProfileData(**user.to_dict())


@router.put("/")
async def edit_profile(
user: Annotated[User, Depends(get_current_user)],
form: EditProfileForm = Depends()
):
form: EditProfileForm = Depends(),
) -> MessageResponse:
loop = asyncio.get_running_loop()

try:
data_dict = form.to_dict()
data_dict.pop("avatar", None)
Expand All @@ -36,12 +37,12 @@ async def edit_profile(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=e.errors(),
)

user.name = data.name
avatar_content = await form.avatar.read()
avatar_url = await loop.run_in_executor(
None,
lambda: bucket.upload_file(avatar_content, get_hash_sha256(avatar_content))
lambda: bucket.upload_file(avatar_content, get_hash_sha256(avatar_content)),
)
user.avatar = avatar_url
user.save()
Expand All @@ -51,11 +52,12 @@ async def edit_profile(
message="Successfully registered",
)


@router.patch("/password")
def change_password(
user: Annotated[User, Depends(get_current_user)],
data: ChangePasswordData,
):
) -> MessageResponse:
user.password = get_password_hash(data.password)
user.save()
return JSONResponse(content={"detail": "password changed"}, status_code=200)
return MessageResponse(error=False, message="Password changed")

0 comments on commit 6a6f2f7

Please sign in to comment.