|
| 1 | +from fastapi import APIRouter, Depends, HTTPException |
| 2 | +from app.api.deps import SessionDep, get_current_active_superuser |
| 3 | +from app.crud.credentials import ( |
| 4 | + get_creds_by_org, |
| 5 | + get_key_by_org, |
| 6 | + remove_creds_for_org, |
| 7 | + set_creds_for_org, |
| 8 | + update_creds_for_org, |
| 9 | +) |
| 10 | +from app.models import CredsCreate, CredsPublic, CredsUpdate |
| 11 | +from app.utils import APIResponse |
| 12 | +from datetime import datetime |
| 13 | + |
| 14 | +router = APIRouter(prefix="/credentials", tags=["credentials"]) |
| 15 | + |
| 16 | + |
| 17 | +@router.post( |
| 18 | + "/", |
| 19 | + dependencies=[Depends(get_current_active_superuser)], |
| 20 | + response_model=APIResponse[CredsPublic], |
| 21 | +) |
| 22 | +def create_new_credential(*, session: SessionDep, creds_in: CredsCreate): |
| 23 | + new_creds = None |
| 24 | + try: |
| 25 | + existing_creds = get_creds_by_org( |
| 26 | + session=session, org_id=creds_in.organization_id |
| 27 | + ) |
| 28 | + if not existing_creds: |
| 29 | + new_creds = set_creds_for_org(session=session, creds_add=creds_in) |
| 30 | + except Exception as e: |
| 31 | + raise HTTPException( |
| 32 | + status_code=500, detail=f"An unexpected error occurred: {str(e)}" |
| 33 | + ) |
| 34 | + |
| 35 | + # Ensure inserted_at is set during creation |
| 36 | + new_creds.inserted_at = datetime.utcnow() |
| 37 | + |
| 38 | + return APIResponse.success_response(new_creds) |
| 39 | + |
| 40 | + |
| 41 | +@router.get( |
| 42 | + "/{org_id}", |
| 43 | + dependencies=[Depends(get_current_active_superuser)], |
| 44 | + response_model=APIResponse[CredsPublic], |
| 45 | +) |
| 46 | +def read_credential(*, session: SessionDep, org_id: int): |
| 47 | + try: |
| 48 | + creds = get_creds_by_org(session=session, org_id=org_id) |
| 49 | + except Exception as e: |
| 50 | + raise HTTPException( |
| 51 | + status_code=500, detail=f"An unexpected error occurred: {str(e)}" |
| 52 | + ) |
| 53 | + |
| 54 | + if creds is None: |
| 55 | + raise HTTPException(status_code=404, detail="Credentials not found") |
| 56 | + |
| 57 | + return APIResponse.success_response(creds) |
| 58 | + |
| 59 | + |
| 60 | +@router.get( |
| 61 | + "/{org_id}/api-key", |
| 62 | + dependencies=[Depends(get_current_active_superuser)], |
| 63 | + response_model=APIResponse[dict], |
| 64 | +) |
| 65 | +def read_api_key(*, session: SessionDep, org_id: int): |
| 66 | + try: |
| 67 | + api_key = get_key_by_org(session=session, org_id=org_id) |
| 68 | + except Exception as e: |
| 69 | + raise HTTPException( |
| 70 | + status_code=500, detail=f"An unexpected error occurred: {str(e)}" |
| 71 | + ) |
| 72 | + |
| 73 | + if api_key is None: |
| 74 | + raise HTTPException(status_code=404, detail="API key not found") |
| 75 | + |
| 76 | + return APIResponse.success_response({"api_key": api_key}) |
| 77 | + |
| 78 | + |
| 79 | +@router.patch( |
| 80 | + "/{org_id}", |
| 81 | + dependencies=[Depends(get_current_active_superuser)], |
| 82 | + response_model=APIResponse[CredsPublic], |
| 83 | +) |
| 84 | +def update_credential(*, session: SessionDep, org_id: int, creds_in: CredsUpdate): |
| 85 | + try: |
| 86 | + updated_creds = update_creds_for_org( |
| 87 | + session=session, org_id=org_id, creds_in=creds_in |
| 88 | + ) |
| 89 | + |
| 90 | + updated_creds.updated_at = datetime.utcnow() |
| 91 | + |
| 92 | + return APIResponse.success_response(updated_creds) |
| 93 | + except ValueError as e: |
| 94 | + raise HTTPException(status_code=404, detail=str(e)) |
| 95 | + except Exception as e: |
| 96 | + raise HTTPException( |
| 97 | + status_code=500, detail=f"An unexpected error occurred: {str(e)}" |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +from fastapi import HTTPException, Depends |
| 102 | +from app.crud.credentials import remove_creds_for_org |
| 103 | +from app.utils import APIResponse |
| 104 | +from app.api.deps import SessionDep, get_current_active_superuser |
| 105 | + |
| 106 | + |
| 107 | +@router.delete( |
| 108 | + "/{org_id}/api-key", |
| 109 | + dependencies=[Depends(get_current_active_superuser)], |
| 110 | + response_model=APIResponse[dict], |
| 111 | +) |
| 112 | +def delete_credential(*, session: SessionDep, org_id: int): |
| 113 | + try: |
| 114 | + creds = remove_creds_for_org(session=session, org_id=org_id) |
| 115 | + except Exception as e: |
| 116 | + raise HTTPException( |
| 117 | + status_code=500, detail=f"An unexpected error occurred: {str(e)}" |
| 118 | + ) |
| 119 | + |
| 120 | + if creds is None: |
| 121 | + raise HTTPException( |
| 122 | + status_code=404, detail="Credentials for organization not found" |
| 123 | + ) |
| 124 | + |
| 125 | + # No need to manually set deleted_at and is_active if it's done in remove_creds_for_org |
| 126 | + # Simply return the success response |
| 127 | + return APIResponse.success_response({"message": "Credentials deleted successfully"}) |
0 commit comments