From 282d1bcd93a53075febada86b502322e41251bf8 Mon Sep 17 00:00:00 2001 From: YuehuCao Date: Thu, 11 Sep 2025 22:28:03 +0800 Subject: [PATCH] feat(integrate api): integrate external auth introspect api --- .../infra/api_key_introspect_handler.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 apps/notification/backend/infra/api_key_introspect_handler.py diff --git a/apps/notification/backend/infra/api_key_introspect_handler.py b/apps/notification/backend/infra/api_key_introspect_handler.py new file mode 100644 index 0000000..f040cda --- /dev/null +++ b/apps/notification/backend/infra/api_key_introspect_handler.py @@ -0,0 +1,45 @@ +from typing import Dict, Any +import httpx +from fastapi import HTTPException +from common.config.app_settings import app_settings +from common.log.log_utils import log_entry_exit_async +from common.log.module_logger import ModuleLogger + + +class ApiKeyIntrospectHandler: + """ + Freeleaps Auth Service API Key Introspect Handle + """ + + def __init__(self) -> None: + self.module_logger = ModuleLogger(sender_id=ApiKeyIntrospectHandler.__name__) + self.auth_service_base = app_settings.AUTH_SERVICE_ENDPOINT + + + @log_entry_exit_async + async def api_key_introspect(self, api_key: str) -> Dict[str, Any]: + """ + Introspect API key by calling external auth service + + Args: + api_key: The API key to introspect + + Returns: + Dictionary containing the API key details + + Raises: + HTTPException: If the external service call fails + """ + api_url = self.auth_service_base + "introspect_api_key" + async with httpx.AsyncClient() as client: + response = await client.post( + api_url, + json={"api_key": api_key} + ) + if response.status_code != 200: + error_detail = response.json() if response.content else {"error": "Unknown error"} + raise HTTPException( + status_code=response.status_code, + detail=error_detail + ) + return response.json()