11"""API dependencies."""
2- from typing import Annotated , AsyncGenerator
2+ from collections .abc import AsyncGenerator
3+ from typing import Annotated
34
4- from fastapi import Depends , HTTPException , status , Security
5- from fastapi .security import OAuth2PasswordBearer , APIKeyHeader
5+ from fastapi import Depends , HTTPException , Security , status
6+ from fastapi .security import APIKeyHeader , OAuth2PasswordBearer
67from jose import JWTError
78from pydantic import ValidationError
89from sqlalchemy .ext .asyncio import AsyncSession
910
10- from app .core import security , auth_client
11+ from app .core import auth_client , security
1112from app .core .config import settings
1213from app .db .session import SessionLocal
13- from app .models .user import User
1414from app .models .client import Client
15- from app .repositories .user import user_repo
15+ from app .models .user import User
1616from app .repositories .api_key import api_key_repo
17+ from app .repositories .user import user_repo
1718from app .schemas .token import TokenPayload
18- from app .schemas .user import UserResponse
1919
2020# OAuth2 scheme for JWT token in Authorization header
2121reusable_oauth2 = OAuth2PasswordBearer (tokenUrl = f"{ settings .api_v1_str } /admin/auth/login" )
@@ -52,11 +52,11 @@ async def get_current_user(session: SessionDep, token: TokenDep) -> User:
5252 headers = {"WWW-Authenticate" : "Bearer" },
5353 )
5454 token_data = TokenPayload (** payload )
55- except (JWTError , ValidationError ):
55+ except (JWTError , ValidationError ) as err :
5656 raise HTTPException (
5757 status_code = status .HTTP_403_FORBIDDEN ,
5858 detail = "Could not validate credentials" ,
59- )
59+ ) from err
6060
6161 user = await user_repo .get (session , token_data .sub ) # type: ignore # sub is UUID in string
6262 if not user :
@@ -91,10 +91,6 @@ async def get_current_client(
9191 detail = "Invalid or expired API Key" ,
9292 )
9393
94- # Update last used (fire and forget / async task in real app)
95- # db_key.last_used_at = datetime.utcnow()
96- # await session.commit()
97-
9894 if not db_key .client .is_license_valid :
9995 raise HTTPException (
10096 status_code = status .HTTP_403_FORBIDDEN ,
@@ -109,7 +105,7 @@ def get_content_service() -> "ContentService":
109105 Dependency to get content service instance.
110106 In a real app with more complex deps, this could initialize the service.
111107 """
112- from app .services .content import ContentService , content_service as _content_service
108+ from app .services .content import content_service as _content_service
113109
114110 return _content_service
115111
0 commit comments