refactor(api): use Annotated-style dependency injection

This commit is contained in:
MingxuanGame
2025-10-03 05:41:31 +00:00
parent 37b4eadf79
commit 346c2557cf
45 changed files with 623 additions and 577 deletions

View File

@@ -1,16 +1,15 @@
from __future__ import annotations
import secrets
from typing import Annotated
from app.database.auth import OAuthClient, OAuthToken
from app.database.user import User
from app.dependencies.database import Database, get_redis
from app.dependencies.user import get_client_user
from app.dependencies.database import Database, Redis
from app.dependencies.user import ClientUser
from .router import router
from fastapi import Body, Depends, HTTPException, Security
from redis.asyncio import Redis
from fastapi import Body, HTTPException
from sqlmodel import select, text
@@ -22,10 +21,10 @@ from sqlmodel import select, text
)
async def create_oauth_app(
session: Database,
name: str = Body(..., max_length=100, description="应用程序名称"),
description: str = Body("", description="应用程序描述"),
redirect_uris: list[str] = Body(..., description="允许的重定向 URI 列表"),
current_user: User = Security(get_client_user),
name: Annotated[str, Body(..., max_length=100, description="应用程序名称")],
redirect_uris: Annotated[list[str], Body(..., description="允许的重定向 URI 列表")],
current_user: ClientUser,
description: Annotated[str, Body(description="应用程序描述")] = "",
):
result = await session.execute(
text(
@@ -64,7 +63,7 @@ async def create_oauth_app(
async def get_oauth_app(
session: Database,
client_id: int,
current_user: User = Security(get_client_user),
current_user: ClientUser,
):
oauth_app = await session.get(OAuthClient, client_id)
if not oauth_app:
@@ -85,7 +84,7 @@ async def get_oauth_app(
)
async def get_user_oauth_apps(
session: Database,
current_user: User = Security(get_client_user),
current_user: ClientUser,
):
oauth_apps = await session.exec(select(OAuthClient).where(OAuthClient.owner_id == current_user.id))
return [
@@ -109,7 +108,7 @@ async def get_user_oauth_apps(
async def delete_oauth_app(
session: Database,
client_id: int,
current_user: User = Security(get_client_user),
current_user: ClientUser,
):
oauth_client = await session.get(OAuthClient, client_id)
if not oauth_client:
@@ -134,10 +133,10 @@ async def delete_oauth_app(
async def update_oauth_app(
session: Database,
client_id: int,
name: str = Body(..., max_length=100, description="应用程序新名称"),
description: str = Body("", description="应用程序新描述"),
redirect_uris: list[str] = Body(..., description="新的重定向 URI 列表"),
current_user: User = Security(get_client_user),
name: Annotated[str, Body(..., max_length=100, description="应用程序新名称")],
redirect_uris: Annotated[list[str], Body(..., description="新的重定向 URI 列表")],
current_user: ClientUser,
description: Annotated[str, Body(description="应用程序新描述")] = "",
):
oauth_client = await session.get(OAuthClient, client_id)
if not oauth_client:
@@ -168,7 +167,7 @@ async def update_oauth_app(
async def refresh_secret(
session: Database,
client_id: int,
current_user: User = Security(get_client_user),
current_user: ClientUser,
):
oauth_client = await session.get(OAuthClient, client_id)
if not oauth_client:
@@ -200,10 +199,10 @@ async def refresh_secret(
async def generate_oauth_code(
session: Database,
client_id: int,
current_user: User = Security(get_client_user),
redirect_uri: str = Body(..., description="授权后重定向的 URI"),
scopes: list[str] = Body(..., description="请求的权限范围列表"),
redis: Redis = Depends(get_redis),
current_user: ClientUser,
redirect_uri: Annotated[str, Body(..., description="授权后重定向的 URI")],
scopes: Annotated[list[str], Body(..., description="请求的权限范围列表")],
redis: Redis,
):
client = await session.get(OAuthClient, client_id)
if not client: