| 1 | from fastapi import HTTPException |
| 2 | from loguru import logger |
| 3 | |
| 4 | from app.connectors.shuffle.schema.organizations import DetailedOrganization |
| 5 | from app.connectors.shuffle.schema.organizations import Organization |
| 6 | from app.connectors.shuffle.schema.organizations import OrganizationsListResponse |
| 7 | from app.connectors.shuffle.utils.universal import send_get_request |
| 8 | |
| 9 | |
| 10 | class OrganizationsService: |
| 11 | """Service class for handling Shuffle Organizations operations.""" |
| 12 | |
| 13 | @staticmethod |
| 14 | async def list_organizations(connector_name: str = "Shuffle") -> OrganizationsListResponse: |
| 15 | """ |
| 16 | List all organizations from Shuffle. |
| 17 | |
| 18 | Args: |
| 19 | connector_name (str, optional): The name of the connector to use. Defaults to "Shuffle". |
| 20 | |
| 21 | Returns: |
| 22 | OrganizationsListResponse: Response containing list of organizations. |
| 23 | """ |
| 24 | logger.info("Fetching organizations from Shuffle") |
| 25 | |
| 26 | try: |
| 27 | # Send GET request to Shuffle API |
| 28 | response = await send_get_request(endpoint="/api/v1/orgs", connector_name=connector_name) |
| 29 | |
| 30 | if not response.get("success", False): |
| 31 | logger.error(f"Failed to fetch organizations: {response.get('message', 'Unknown error')}") |
| 32 | raise HTTPException(status_code=500, detail=f"Failed to fetch organizations: {response.get('message', 'Unknown error')}") |
| 33 | |
| 34 | # Parse the response data |
| 35 | organizations_data = response.get("data", []) |
| 36 | |
| 37 | if not isinstance(organizations_data, list): |
| 38 | logger.error("Invalid response format: expected list of organizations") |
| 39 | raise HTTPException(status_code=500, detail="Invalid response format from Shuffle API") |
| 40 | |
| 41 | # Convert to Organization models |
| 42 | organizations = [] |
| 43 | for org_data in organizations_data: |
| 44 | try: |
| 45 | organization = Organization(**org_data) |
| 46 | organizations.append(organization) |
| 47 | except Exception as e: |
| 48 | logger.warning(f"Failed to parse organization data: {org_data}. Error: {e}") |
| 49 | continue |
| 50 | |
| 51 | logger.info(f"Successfully fetched {len(organizations)} organizations") |
| 52 | |
| 53 | return OrganizationsListResponse( |
| 54 | success=True, |
| 55 | message=f"Successfully retrieved {len(organizations)} organizations", |
| 56 | data=organizations, |
| 57 | total_count=len(organizations), |
| 58 | ) |
| 59 | |
| 60 | except HTTPException: |
| 61 | # Re-raise HTTPExceptions as-is |
| 62 | raise |
| 63 | except Exception as e: |
| 64 | logger.error(f"Unexpected error while fetching organizations: {e}") |
| 65 | raise HTTPException(status_code=500, detail=f"Unexpected error while fetching organizations: {str(e)}") |
| 66 | |
| 67 | @staticmethod |
| 68 | async def get_organization_by_id(org_id: str, connector_name: str = "Shuffle") -> DetailedOrganization: |
| 69 | """ |
| 70 | Get a specific organization by ID using direct API call. |
| 71 | |
| 72 | Args: |
| 73 | org_id (str): The organization ID to retrieve. |
| 74 | connector_name (str, optional): The name of the connector to use. Defaults to "Shuffle". |
| 75 | |
| 76 | Returns: |
| 77 | DetailedOrganization: The detailed organization data. |
| 78 | """ |
| 79 | logger.info(f"Fetching organization with ID: {org_id}") |
| 80 | |
| 81 | try: |
| 82 | # Send GET request to Shuffle API for specific organization |
| 83 | response = await send_get_request(endpoint=f"/api/v1/orgs/{org_id}", connector_name=connector_name) |
| 84 | |
| 85 | if not response.get("success", False): |
| 86 | logger.error(f"Failed to fetch organization {org_id}: {response.get('message', 'Unknown error')}") |
| 87 | raise HTTPException( |
| 88 | status_code=500, |
| 89 | detail=f"Failed to fetch organization {org_id}: {response.get('message', 'Unknown error')}", |
| 90 | ) |
| 91 | |
| 92 | # Parse the response data |
| 93 | organization_data = response.get("data", {}) |
| 94 | |
| 95 | if not organization_data: |
| 96 | logger.error(f"Organization with ID {org_id} not found") |
| 97 | raise HTTPException(status_code=404, detail=f"Organization with ID {org_id} not found") |
| 98 | |
| 99 | try: |
| 100 | organization = DetailedOrganization(**organization_data) |
| 101 | logger.info(f"Found organization: {organization.name}") |
| 102 | return organization |
| 103 | except Exception as e: |
| 104 | logger.error(f"Failed to parse organization data for ID {org_id}: {e}") |
| 105 | raise HTTPException(status_code=500, detail=f"Failed to parse organization data: {str(e)}") |
| 106 | |
| 107 | except HTTPException: |
| 108 | # Re-raise HTTPExceptions as-is |
| 109 | raise |
| 110 | except Exception as e: |
| 111 | logger.error(f"Unexpected error while fetching organization {org_id}: {e}") |
| 112 | raise HTTPException(status_code=500, detail=f"Unexpected error while fetching organization {org_id}: {str(e)}") |
| 113 | |
| 114 | @staticmethod |
| 115 | async def get_organization_by_name(org_name: str, connector_name: str = "Shuffle") -> Organization: |
| 116 | """ |
| 117 | Get a specific organization by name. |
| 118 | |
| 119 | Args: |
| 120 | org_name (str): The organization name to retrieve. |
| 121 | connector_name (str, optional): The name of the connector to use. Defaults to "Shuffle". |
| 122 | |
| 123 | Returns: |
| 124 | Organization: The organization data. |
| 125 | """ |
| 126 | logger.info(f"Fetching organization with name: {org_name}") |
| 127 | |
| 128 | # Get all organizations and filter by name |
| 129 | organizations_response = await OrganizationsService.list_organizations(connector_name) |
| 130 | |
| 131 | for org in organizations_response.data: |
| 132 | if org.name.lower() == org_name.lower(): |
| 133 | logger.info(f"Found organization: {org.name} (ID: {org.id})") |
| 134 | return org |
| 135 | |
| 136 | logger.error(f"Organization with name '{org_name}' not found") |
| 137 | raise HTTPException(status_code=404, detail=f"Organization with name '{org_name}' not found") |