1212from app .crud .project import validate_project
1313from app .models import APIKeyPublic , User
1414from app .utils import APIResponse
15+ from app .core .exception_handlers import HTTPException
1516
1617router = APIRouter (prefix = "/apikeys" , tags = ["API Keys" ])
1718
1819
19- # Create API Key
2020@router .post ("/" , response_model = APIResponse [APIKeyPublic ])
2121def create_key (
2222 project_id : int ,
@@ -27,31 +27,26 @@ def create_key(
2727 """
2828 Generate a new API key for the user's organization.
2929 """
30- try :
31- # Validate organization
32- project = validate_project (session , project_id )
33-
34- existing_api_key = get_api_key_by_project_user (session , project_id , user_id )
35- if existing_api_key :
36- raise HTTPException (
37- status_code = 400 ,
38- detail = "API Key already exists for this user and project." ,
39- )
40-
41- # Create and return API key
42- api_key = create_api_key (
43- session ,
44- organization_id = project .organization_id ,
45- user_id = user_id ,
46- project_id = project_id ,
30+ # Validate organization
31+ project = validate_project (session , project_id )
32+
33+ existing_api_key = get_api_key_by_project_user (session , project_id , user_id )
34+ if existing_api_key :
35+ raise HTTPException (
36+ status_code = 400 ,
37+ detail = "API Key already exists for this user and project." ,
4738 )
48- return APIResponse .success_response (api_key )
4939
50- except ValueError as e :
51- raise HTTPException (status_code = 400 , detail = str (e ))
40+ # Create and return API key
41+ api_key = create_api_key (
42+ session ,
43+ organization_id = project .organization_id ,
44+ user_id = user_id ,
45+ project_id = project_id ,
46+ )
47+ return APIResponse .success_response (api_key )
5248
5349
54- # List API Keys
5550@router .get ("/" , response_model = APIResponse [list [APIKeyPublic ]])
5651def list_keys (
5752 project_id : int ,
@@ -62,27 +57,29 @@ def list_keys(
6257 Retrieve all API keys for the given project. Superusers get all keys;
6358 regular users get only their own.
6459 """
65- try :
66- # Validate project
67- project = validate_project (session = session , project_id = project_id )
68-
69- if current_user .is_superuser :
70- # Superuser: fetch all API keys for the project
71- api_keys = get_api_keys_by_project (session = session , project_id = project_id )
72- else :
73- # Regular user: fetch only their own API key
74- user_api_key = get_api_key_by_project_user (
75- session = session , project_id = project_id , user_id = current_user .id
76- )
77- api_keys = [user_api_key ] if user_api_key else []
60+ # Validate project
61+ project = validate_project (session = session , project_id = project_id )
62+
63+ if current_user .is_superuser :
64+ # Superuser: fetch all API keys for the project
65+ api_keys = get_api_keys_by_project (session = session , project_id = project_id )
66+ else :
67+ # Regular user: fetch only their own API key
68+ user_api_key = get_api_key_by_project_user (
69+ session = session , project_id = project_id , user_id = current_user .id
70+ )
71+ api_keys = [user_api_key ] if user_api_key else []
7872
79- return APIResponse .success_response (api_keys )
73+ # Raise an exception if no API keys are found for the project
74+ if not api_keys :
75+ raise HTTPException (
76+ status_code = 404 ,
77+ detail = "No API keys found for this project." ,
78+ )
8079
81- except ValueError as e :
82- raise HTTPException (status_code = 400 , detail = str (e ))
80+ return APIResponse .success_response (api_keys )
8381
8482
85- # Get API Key by ID
8683@router .get ("/{api_key_id}" , response_model = APIResponse [APIKeyPublic ])
8784def get_key (
8885 api_key_id : int ,
@@ -94,12 +91,11 @@ def get_key(
9491 """
9592 api_key = get_api_key (session , api_key_id )
9693 if not api_key :
97- raise HTTPException (status_code = 404 , detail = "API Key does not exist" )
94+ raise HTTPException (404 , "API Key does not exist" )
9895
9996 return APIResponse .success_response (api_key )
10097
10198
102- # Revoke API Key (Soft Delete)
10399@router .delete ("/{api_key_id}" , response_model = APIResponse [dict ])
104100def revoke_key (
105101 api_key_id : int ,
@@ -109,8 +105,11 @@ def revoke_key(
109105 """
110106 Soft delete an API key (revoke access).
111107 """
112- try :
113- delete_api_key (session , api_key_id )
114- return APIResponse .success_response ({"message" : "API key revoked successfully" })
115- except ValueError as e :
116- raise HTTPException (status_code = 400 , detail = str (e ))
108+ api_key = get_api_key (session , api_key_id )
109+
110+ if not api_key :
111+ raise HTTPException (404 , "API key not found or already deleted" )
112+
113+ delete_api_key (session , api_key_id )
114+
115+ return APIResponse .success_response ({"message" : "API key revoked successfully" })
0 commit comments