Skip to content

Commit 8ec2aee

Browse files
committed
feat: fixed sync/async situation; made email a BackgroundTask
1 parent 767dd01 commit 8ec2aee

5 files changed

Lines changed: 25 additions & 9 deletions

File tree

backend/api/audio/controllers/audio_controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def analyse_audio(
6161

6262

6363
@audio.post("/api/v1/audio/apply", response_model=list[EventBase])
64-
async def apply_recommendations( # noqa: PLR0913
64+
def apply_recommendations( # noqa: PLR0913
6565
analysis_output: AudioAnalysisOutput,
6666
apply_recommendations_use_case: Annotated[
6767
ApplyRecommendationsUseCase,
@@ -81,7 +81,7 @@ async def apply_recommendations( # noqa: PLR0913
8181
],
8282
current_user: Annotated[str, Depends(get_current_user)],
8383
):
84-
return await apply_recommendations_use_case.execute(
84+
return apply_recommendations_use_case.execute(
8585
current_user,
8686
analysis_output.llm_output,
8787
create_event_use_case,

backend/api/audio/use_cases/apply_recommendations_use_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(
2323
) -> None:
2424
self.user_repository = user_repository
2525

26-
async def execute(
26+
def execute(
2727
self,
2828
current_user: str,
2929
recommendations: str,

backend/api/services/email_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self) -> None:
1010
self.url = PLUNK_URL
1111
self.api_key = config.PLUNK_API_KEY
1212

13-
def send_welcome_email(self, recipient: str) -> None:
13+
async def send_welcome_email(self, recipient: str) -> None:
1414
self.body = """
1515
<!DOCTYPE html>
1616
<html lang="en">
@@ -70,4 +70,5 @@ def send_welcome_email(self, recipient: str) -> None:
7070
"Authorization": f"Bearer {self.api_key}",
7171
}
7272

73-
httpx.request("POST", self.url, json=payload, headers=headers)
73+
async with httpx.AsyncClient() as client:
74+
await client.request("POST", self.url, json=payload, headers=headers)

backend/api/users/controllers/users_controller.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
from typing import Annotated
22

3-
from fastapi import APIRouter, Depends, HTTPException, Request, Response
3+
from fastapi import (
4+
APIRouter,
5+
BackgroundTasks,
6+
Depends,
7+
HTTPException,
8+
Request,
9+
Response,
10+
)
411
from fastapi.security import OAuth2PasswordRequestForm
512

613
from api.config import config
@@ -33,6 +40,7 @@
3340
@users.post("/api/v1/users")
3441
def register_user(
3542
request: UserDetails,
43+
background_tasks: BackgroundTasks,
3644
register_user_use_case: Annotated[
3745
RegisterUserUseCase,
3846
Depends(register_user_use_case),
@@ -62,7 +70,7 @@ def register_user(
6270
detail=validation_password_errors,
6371
)
6472

65-
return register_user_use_case.execute(request)
73+
return register_user_use_case.execute(request, background_tasks)
6674

6775

6876
@users.post("/api/v1/users/login")

backend/api/users/use_cases/register_user_use_case.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from fastapi import BackgroundTasks
2+
13
from api.services.email_service import EmailService
24
from api.system.interfaces.use_cases import UseCase
35
from api.system.models.models import User
@@ -19,7 +21,9 @@ def __init__(
1921
self.bcrypt_hasher = bcrypt_hasher
2022
self.email_service = email_service
2123

22-
def execute(self, request: UserDetails) -> UserSchema:
24+
def execute(
25+
self, request: UserDetails, background_tasks: BackgroundTasks
26+
) -> UserSchema:
2327
if self.user_repository.find_by_email(request.email_address):
2428
msg = "User already exists."
2529
raise UserAlreadyExistsError(msg)
@@ -34,6 +38,9 @@ def execute(self, request: UserDetails) -> UserSchema:
3438
)
3539

3640
self.user_repository.add(user)
37-
self.email_service.send_welcome_email(str(user.email_address))
41+
background_tasks.add_task(
42+
self.email_service.send_welcome_email,
43+
str(user.email_address),
44+
)
3845

3946
return user

0 commit comments

Comments
 (0)