Skip to content

Commit d7218c5

Browse files
committed
refactor: fastapi response syntax moves to python typing REFS #57
as of fastapi 0.89 it supports using python typing to define response models, this unifies the syntax with sqlalchemy 2.0 using python typing to define models and makes our code look uniform across pydantic, sqlalchemy and fastapi
1 parent 7c26a74 commit d7218c5

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

src/labs/routers/auth/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
@router.post(
3333
"/token",
3434
summary="Provides an endpoint for login via email and password",
35-
response_model=Token,
3635
)
3736
async def login_for_auth_token(
3837
form_data: OAuth2PasswordRequestForm = Depends(),
3938
session: AsyncSession = Depends(get_async_session)
40-
):
39+
) -> Token:
4140
""" Attempt to authenticate a user and issue JWT token
4241
4342
"""
@@ -63,10 +62,10 @@ async def login_for_auth_token(
6362
@router.post(
6463
"/refresh",
6564
summary=""" Provides an endpoint for refreshing the JWT token""",
66-
response_model=Token,
6765
)
6866
async def refresh_jwt_token(request: Request,
69-
session: AsyncSession = Depends(get_async_session)):
67+
session: AsyncSession = Depends(get_async_session)
68+
) -> Token:
7069
""" Provides a refresh token for the JWT session.
7170
"""
7271
return {}
@@ -76,19 +75,20 @@ async def refresh_jwt_token(request: Request,
7675
"/logout",
7776
summary=""" Provides an endpoint for logging out the user""",
7877
)
79-
async def logout_user(session: AsyncSession = Depends(get_async_session)):
78+
async def logout_user(
79+
session: AsyncSession = Depends(get_async_session)
80+
):
8081
""" Ends a users session
8182
8283
"""
8384
return {}
8485

8586
@router.get(
8687
"/me",
87-
response_model=UserResponse,
8888
)
8989
async def get_me(
9090
current_user: User = Depends(get_current_user)
91-
):
91+
) -> UserResponse:
9292
"""Get the currently logged in user or myself
9393
9494
This endpoint will return the currently logged in user or raise

src/labs/routers/auth/otp.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
router = APIRouter(tags=["otp"])
1616

17-
@router.post("/initiate/email",
18-
response_model=OTPTriggerResponse,
17+
@router.post(
18+
"/initiate/email",
1919
)
20-
async def initiate_otp_email(request: OTPTriggerEmailRequest,
20+
async def initiate_otp_email(
21+
request: OTPTriggerEmailRequest,
2122
session: AsyncSession = Depends(get_async_session)
22-
):
23+
) -> OTPTriggerResponse:
2324
""" Attempt to authenticate a user and issue JWT token
2425
2526
The user has provided us their email address and we will
@@ -40,12 +41,12 @@ async def initiate_otp_email(request: OTPTriggerEmailRequest,
4041
return OTPTriggerResponse(success=True)
4142

4243

43-
@router.post("/initiate/sms",
44-
response_model=OTPTriggerResponse,
44+
@router.post(
45+
"/initiate/sms",
4546
)
4647
async def initiate_otp_sms(request: OTPTriggerSMSRequest,
4748
session: AsyncSession = Depends(get_async_session)
48-
):
49+
) -> OTPTriggerResponse:
4950
""" Attempt to authenticate a user and issue JWT token
5051
5152
The user has provided a mobile number and we will text them
@@ -65,7 +66,8 @@ async def initiate_otp_sms(request: OTPTriggerSMSRequest,
6566
return OTPTriggerResponse(success=True)
6667

6768
@router.post("/verify")
68-
async def verify_otp(request: OTPVerifyRequest,
69+
async def verify_otp(
70+
request: OTPVerifyRequest,
6971
session: AsyncSession = Depends(get_async_session)
7072
):
7173
""" Attempt to authenticate a user and issue JWT token

src/labs/routers/ext/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from ...db import get_async_session
88
from ...schema import HealthCheckResponse
9-
from ...config import config
109

1110
router = APIRouter(tags=["ext"])
1211

@@ -21,11 +20,10 @@ async def echo():
2120

2221
@router.get(
2322
"/healthcheck",
24-
response_model=HealthCheckResponse
2523
)
2624
async def get_health(
2725
session: AsyncSession = Depends(get_async_session)
28-
):
26+
) -> HealthCheckResponse:
2927
"""Check the health of the server.
3028
3129
Purpose of this endpoint is to check the health of the server.

src/labs/routers/users/__init__.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
used to manage user accounts. For the template application this
55
was built to test out the initial CRUD features.
66
"""
7-
from datetime import datetime
87
from uuid import UUID
9-
from typing import List
108

119
from fastapi import APIRouter, Depends,\
1210
HTTPException, Query, status
@@ -22,15 +20,14 @@
2220
@router.get(
2321
"",
2422
summary="Query users between limits",
25-
response_model=List[UserResponse],
2623
status_code=status.HTTP_200_OK
2724
)
2825
async def get_users_with_limits(
2926
offset: int = Query(0, ge=0),
3027
limit: int = Query(100, ge=1, le=100),
3128
session: AsyncSession = Depends(get_async_session),
3229
current_user: User = Depends(get_admin_user),
33-
):
30+
) -> list[UserResponse]:
3431
users = await User.get_all_in_range(
3532
session,
3633
offset=offset,
@@ -41,29 +38,27 @@ async def get_users_with_limits(
4138
@router.get(
4239
"/infinite",
4340
summary="Get all users",
44-
response_model=List[UserResponse],
4541
status_code=status.HTTP_200_OK
4642
)
4743
async def get_users(
4844
next_id: UUID = None,
4945
limit: int = 10,
5046
session: AsyncSession = Depends(get_async_session),
5147
current_user: User = Depends(get_admin_user),
52-
):
48+
) -> list[UserResponse]:
5349
pass
5450

5551

5652
@router.get(
5753
"/{id}",
5854
summary="Get a particular user",
59-
response_model=UserResponse,
6055
status_code=status.HTTP_200_OK
6156
)
6257
async def get_user_by_id(
6358
id: UUID,
6459
session: AsyncSession = Depends(get_async_session),
6560
current_user: User = Depends(get_admin_user),
66-
):
61+
) -> UserResponse:
6762
""" Get a user by their id
6863
6964
@@ -134,14 +129,13 @@ async def update_user(
134129
@router.post(
135130
"",
136131
summary="Create a new user",
137-
response_model=UserResponse,
138132
status_code=status.HTTP_201_CREATED,
139133
)
140134
async def create_user(
141135
user_request: UserRequest,
142136
session: AsyncSession = Depends(get_async_session),
143137
current_user: User = Depends(get_admin_user),
144-
):
138+
) -> UserResponse:
145139
""" Creates a new user based on
146140
147141
"""

0 commit comments

Comments
 (0)