Skip to content

Commit

Permalink
use PEP-593 typing.Annotated for specifying dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Mar 28, 2023
1 parent d48f38f commit fd04706
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions mount/app/api/rest/v1/accounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Annotated
from uuid import UUID

from app.api.rest.context import RequestContext
Expand All @@ -21,8 +22,8 @@
status_code=status.HTTP_201_CREATED,
)
async def create(
ctx: Annotated[RequestContext, Depends()],
args: SignupForm,
ctx: RequestContext = Depends(),
) -> Success[Account]:
data = await accounts.create(
ctx,
Expand All @@ -46,8 +47,8 @@ async def create(

@router.get("/v1/accounts/{account_id}")
async def fetch_one(
ctx: Annotated[RequestContext, Depends()],
account_id: UUID,
ctx: RequestContext = Depends(),
) -> Success[Account]:
data = await accounts.fetch_one(ctx, account_id=account_id)
if isinstance(data, ServiceError):
Expand All @@ -59,9 +60,9 @@ async def fetch_one(

@router.get("/v1/accounts")
async def fetch_many(
ctx: Annotated[RequestContext, Depends()],
page: int = Query(1, ge=1),
page_size: int = Query(50, ge=1, le=1000),
ctx: RequestContext = Depends(),
) -> Success[list[Account]]:
data = await accounts.fetch_many(ctx, page=page, page_size=page_size)
if isinstance(data, ServiceError):
Expand All @@ -80,9 +81,9 @@ async def fetch_many(

@router.patch("/v1/accounts/{account_id}")
async def partial_update(
ctx: Annotated[RequestContext, Depends()],
account_id: UUID,
args: AccountUpdate,
ctx: RequestContext = Depends(),
) -> Success[Account]:
data = await accounts.partial_update(
ctx,
Expand All @@ -103,8 +104,8 @@ async def partial_update(
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete(
ctx: Annotated[RequestContext, Depends()],
account_id: UUID,
ctx: RequestContext = Depends(),
) -> None:
data = await accounts.delete(ctx, account_id)
if isinstance(data, ServiceError):
Expand Down
5 changes: 3 additions & 2 deletions mount/app/api/rest/v1/login_attempts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Annotated
from uuid import UUID

from app.api.rest.context import RequestContext
Expand All @@ -15,8 +16,8 @@

@router.get("/v1/login-attempts/{login_attempt_id}")
async def fetch_one(
ctx: Annotated[RequestContext, Depends()],
login_attempt_id: UUID,
ctx: RequestContext = Depends(),
) -> Success[LoginAttempt]:
data = await login_attempts.fetch_one(ctx, login_attempt_id=login_attempt_id)
if isinstance(data, ServiceError):
Expand All @@ -28,11 +29,11 @@ async def fetch_one(

@router.get("/v1/login-attempts")
async def fetch_many(
ctx: Annotated[RequestContext, Depends()],
phone_number: str | None = None,
ip_address: str | None = None,
page: int = Query(1, ge=1),
page_size: int = Query(50, ge=1, le=1000),
ctx: RequestContext = Depends(),
) -> Success[list[LoginAttempt]]:
data = await login_attempts.fetch_many(
ctx,
Expand Down
11 changes: 6 additions & 5 deletions mount/app/api/rest/v1/sessions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Annotated
from uuid import UUID

from app.api.rest.context import RequestContext
Expand All @@ -22,10 +23,10 @@
status_code=status.HTTP_201_CREATED,
)
async def create(
ctx: Annotated[RequestContext, Depends()],
args: LoginForm,
cf_connecting_ip: str = Header("CF-Connecting-IP"),
user_agent: str = Header("User-Agent"),
ctx: RequestContext = Depends(),
) -> Success[Session]:
data = await sessions.create(
ctx,
Expand All @@ -49,8 +50,8 @@ async def create(

@router.get("/v1/sessions/{session_id}")
async def fetch_one(
ctx: Annotated[RequestContext, Depends()],
session_id: UUID,
ctx: RequestContext = Depends(),
) -> Success[Session]:
data = await sessions.fetch_one(ctx, session_id=session_id)
if isinstance(data, ServiceError):
Expand All @@ -62,9 +63,9 @@ async def fetch_one(

@router.get("/v1/sessions")
async def fetch_many(
ctx: Annotated[RequestContext, Depends()],
page: int = Query(1, ge=1),
page_size: int = Query(50, ge=1, le=1000),
ctx: RequestContext = Depends(),
) -> Success[list[Session]]:
data = await sessions.fetch_many(ctx, page=page, page_size=page_size)
if isinstance(data, ServiceError):
Expand All @@ -83,9 +84,9 @@ async def fetch_many(

@router.patch("/v1/sessions/{session_id}")
async def partial_update(
ctx: Annotated[RequestContext, Depends()],
session_id: UUID,
args: SessionUpdate,
ctx: RequestContext = Depends(),
) -> Success[Session]:
data = await sessions.partial_update(
ctx,
Expand All @@ -104,8 +105,8 @@ async def partial_update(
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete(
ctx: Annotated[RequestContext, Depends()],
session_id: UUID,
ctx: RequestContext = Depends(),
) -> None:
data = await sessions.delete(ctx, session_id)
if isinstance(data, ServiceError):
Expand Down

0 comments on commit fd04706

Please sign in to comment.