|
| 1 | +from fastapi import APIRouter, HTTPException, Depends, status |
| 2 | +from uuid import UUID |
| 3 | +from app.models.integration import ( |
| 4 | + IntegrationCreateRequest, |
| 5 | + IntegrationUpdateRequest, |
| 6 | + IntegrationResponse, |
| 7 | + IntegrationListResponse, |
| 8 | + IntegrationStatusResponse |
| 9 | +) |
| 10 | +from app.services.integration_service import integration_service, NotFoundError |
| 11 | +from app.core.dependencies import get_current_user |
| 12 | + |
| 13 | +router = APIRouter() |
| 14 | + |
| 15 | + |
| 16 | +@router.post("/", response_model=IntegrationResponse, status_code=status.HTTP_201_CREATED) |
| 17 | +async def create_integration( |
| 18 | + request: IntegrationCreateRequest, |
| 19 | + user_id: UUID = Depends(get_current_user) |
| 20 | +): |
| 21 | + """Create a new organization integration.""" |
| 22 | + try: |
| 23 | + return await integration_service.create_integration(user_id, request) |
| 24 | + except ValueError as e: |
| 25 | + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) |
| 26 | + except Exception as e: |
| 27 | + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e |
| 28 | + |
| 29 | + |
| 30 | +@router.get("/", response_model=IntegrationListResponse) |
| 31 | +async def list_integrations(user_id: UUID = Depends(get_current_user)): |
| 32 | + """List all integrations for the current user.""" |
| 33 | + try: |
| 34 | + integrations = await integration_service.get_integrations(user_id) |
| 35 | + return IntegrationListResponse(integrations=integrations, total=len(integrations)) |
| 36 | + except Exception as e: |
| 37 | + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e |
| 38 | + |
| 39 | + |
| 40 | +@router.get("/status/{platform}", response_model=IntegrationStatusResponse) |
| 41 | +async def get_integration_status( |
| 42 | + platform: str, |
| 43 | + user_id: UUID = Depends(get_current_user) |
| 44 | +): |
| 45 | + """Get the status of a specific platform integration.""" |
| 46 | + try: |
| 47 | + return await integration_service.get_integration_status(user_id, platform) |
| 48 | + except Exception as e: |
| 49 | + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e |
| 50 | + |
| 51 | +@router.get("/{integration_id}", response_model=IntegrationResponse) |
| 52 | +async def get_integration( |
| 53 | + integration_id: UUID, |
| 54 | + user_id: UUID = Depends(get_current_user) |
| 55 | +): |
| 56 | + """Get a specific integration.""" |
| 57 | + try: |
| 58 | + integration = await integration_service.get_integration(user_id, integration_id) |
| 59 | + |
| 60 | + if not integration: |
| 61 | + raise HTTPException( |
| 62 | + status_code=status.HTTP_404_NOT_FOUND, |
| 63 | + detail="Integration not found" |
| 64 | + ) |
| 65 | + |
| 66 | + return integration |
| 67 | + except Exception as e: |
| 68 | + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e |
| 69 | + |
| 70 | +@router.put("/{integration_id}", response_model=IntegrationResponse) |
| 71 | +async def update_integration( |
| 72 | + integration_id: UUID, |
| 73 | + request: IntegrationUpdateRequest, |
| 74 | + user_id: UUID = Depends(get_current_user) |
| 75 | +): |
| 76 | + """Update an existing integration.""" |
| 77 | + try: |
| 78 | + return await integration_service.update_integration(user_id, integration_id, request) |
| 79 | + except NotFoundError as e: |
| 80 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e |
| 81 | + except HTTPException: |
| 82 | + raise |
| 83 | + except Exception as e: |
| 84 | + raise HTTPException( |
| 85 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 86 | + detail=f"Failed to update integration: {str(e)}" |
| 87 | + ) from e |
| 88 | + |
| 89 | +@router.delete("/{integration_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 90 | +async def delete_integration( |
| 91 | + integration_id: UUID, |
| 92 | + user_id: UUID = Depends(get_current_user) |
| 93 | +): |
| 94 | + """Delete an integration.""" |
| 95 | + try: |
| 96 | + await integration_service.delete_integration(user_id, integration_id) |
| 97 | + except NotFoundError as e: |
| 98 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e |
| 99 | + except HTTPException: |
| 100 | + raise |
| 101 | + except Exception as e: |
| 102 | + raise HTTPException( |
| 103 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 104 | + detail=f"Failed to delete integration: {str(e)}" |
| 105 | + ) from e |
0 commit comments