| 1 | from fastapi import APIRouter |
| 2 | from fastapi import Depends |
| 3 | from fastapi import HTTPException |
| 4 | from fastapi import Query |
| 5 | from loguru import logger |
| 6 | |
| 7 | from app.auth.utils import AuthHandler |
| 8 | from app.connectors.shuffle.schema.organizations import DetailedOrganizationResponse |
| 9 | from app.connectors.shuffle.schema.organizations import OrganizationResponse |
| 10 | from app.connectors.shuffle.schema.organizations import OrganizationsListResponse |
| 11 | from app.connectors.shuffle.services.organizations import OrganizationsService |
| 12 | |
| 13 | # Router Configuration |
| 14 | shuffle_organizations_router = APIRouter() |
| 15 | |
| 16 | # Auth handler |
| 17 | auth_handler = AuthHandler() |
| 18 | |
| 19 | |
| 20 | @shuffle_organizations_router.get( |
| 21 | "/organizations", |
| 22 | response_model=OrganizationsListResponse, |
| 23 | description="Retrieve all organizations from Shuffle", |
| 24 | dependencies=[Depends(auth_handler.require_any_scope("admin", "analyst"))], |
| 25 | ) |
| 26 | async def list_organizations(connector_name: str = Query("Shuffle", description="Name of the Shuffle connector to use")): |
| 27 | """ |
| 28 | Retrieve all organizations from Shuffle. |
| 29 | |
| 30 | Args: |
| 31 | connector_name (str): Name of the Shuffle connector to use. |
| 32 | |
| 33 | Returns: |
| 34 | OrganizationsListResponse: List of all organizations. |
| 35 | """ |
| 36 | logger.info(f"Request to list all organizations using connector: {connector_name}") |
| 37 | |
| 38 | try: |
| 39 | organizations = await OrganizationsService.list_organizations(connector_name) |
| 40 | logger.info(f"Successfully retrieved {organizations.total_count} organizations") |
| 41 | return organizations |
| 42 | except HTTPException: |
| 43 | raise |
| 44 | except Exception as e: |
| 45 | logger.error(f"Unexpected error in list_organizations endpoint: {e}") |
| 46 | raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") |
| 47 | |
| 48 | |
| 49 | @shuffle_organizations_router.get( |
| 50 | "/organizations/{org_id}", |
| 51 | response_model=DetailedOrganizationResponse, |
| 52 | description="Retrieve a specific organization by ID", |
| 53 | dependencies=[Depends(auth_handler.require_any_scope("admin", "analyst"))], |
| 54 | ) |
| 55 | async def get_organization_by_id(org_id: str, connector_name: str = Query("Shuffle", description="Name of the Shuffle connector to use")): |
| 56 | """ |
| 57 | Retrieve a specific organization by ID. |
| 58 | |
| 59 | Args: |
| 60 | org_id (str): The organization ID to retrieve. |
| 61 | connector_name (str): Name of the Shuffle connector to use. |
| 62 | |
| 63 | Returns: |
| 64 | DetailedOrganizationResponse: The detailed organization data. |
| 65 | """ |
| 66 | logger.info(f"Request to get organization with ID: {org_id} using connector: {connector_name}") |
| 67 | |
| 68 | try: |
| 69 | organization = await OrganizationsService.get_organization_by_id(org_id, connector_name) |
| 70 | return DetailedOrganizationResponse( |
| 71 | success=True, |
| 72 | message=f"Successfully retrieved organization: {organization.name}", |
| 73 | data=organization, |
| 74 | ) |
| 75 | except HTTPException: |
| 76 | raise |
| 77 | except Exception as e: |
| 78 | logger.error(f"Unexpected error in get_organization_by_id endpoint: {e}") |
| 79 | raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") |
| 80 | |
| 81 | |
| 82 | @shuffle_organizations_router.get( |
| 83 | "/organizations/name/{org_name}", |
| 84 | response_model=OrganizationResponse, |
| 85 | description="Retrieve a specific organization by name", |
| 86 | dependencies=[Depends(auth_handler.require_any_scope("admin", "analyst"))], |
| 87 | ) |
| 88 | async def get_organization_by_name( |
| 89 | org_name: str, |
| 90 | connector_name: str = Query("Shuffle", description="Name of the Shuffle connector to use"), |
| 91 | ): |
| 92 | """ |
| 93 | Retrieve a specific organization by name. |
| 94 | |
| 95 | Args: |
| 96 | org_name (str): The organization name to retrieve. |
| 97 | connector_name (str): Name of the Shuffle connector to use. |
| 98 | |
| 99 | Returns: |
| 100 | OrganizationResponse: The organization data. |
| 101 | """ |
| 102 | logger.info(f"Request to get organization with name: {org_name} using connector: {connector_name}") |
| 103 | |
| 104 | try: |
| 105 | organization = await OrganizationsService.get_organization_by_name(org_name, connector_name) |
| 106 | return OrganizationResponse(success=True, message=f"Successfully retrieved organization: {organization.name}", data=organization) |
| 107 | except HTTPException: |
| 108 | raise |
| 109 | except Exception as e: |
| 110 | logger.error(f"Unexpected error in get_organization_by_name endpoint: {e}") |
| 111 | raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") |