|
| 1 | +import uuid |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from fastapi.responses import JSONResponse |
| 5 | +from starlette import status |
| 6 | + |
| 7 | +from app.core.exceptions import AppException |
| 8 | +from app.schemas.external_account import ExternalAccountCreate |
| 9 | +from app.schemas.response import ResponseSchema |
| 10 | +from app.services.integrations_service import IntegrationService |
| 11 | + |
| 12 | + |
| 13 | +class IntegrationsController: |
| 14 | + def __init__(self) -> None: |
| 15 | + self.service = IntegrationService() |
| 16 | + self.response_class: type[ResponseSchema[Any]] = ResponseSchema |
| 17 | + self.error_class = AppException |
| 18 | + |
| 19 | + def _success( |
| 20 | + self, |
| 21 | + data: Any = None, |
| 22 | + message: str = "OK", |
| 23 | + status_code: int = status.HTTP_200_OK, |
| 24 | + ) -> JSONResponse: |
| 25 | + msg = message |
| 26 | + data_payload = data |
| 27 | + |
| 28 | + if isinstance(data, dict): |
| 29 | + msg = data.get("message") or message |
| 30 | + if "user" in data: |
| 31 | + data_payload = data.get("user") |
| 32 | + elif "data" in data: |
| 33 | + data_payload = data.get("data") |
| 34 | + if isinstance(data_payload, dict) and "message" in data_payload: |
| 35 | + data_payload = { |
| 36 | + k: v for k, v in data_payload.items() if k != "message" |
| 37 | + } |
| 38 | + |
| 39 | + payload = self.response_class( |
| 40 | + success=True, |
| 41 | + message=msg, |
| 42 | + data=data_payload, |
| 43 | + errors=None, |
| 44 | + meta=None, |
| 45 | + ).model_dump(exclude_none=True) |
| 46 | + |
| 47 | + return JSONResponse(status_code=status_code, content=payload) |
| 48 | + |
| 49 | + def _error( |
| 50 | + self, message: Any = "Error", errors: Any = None, status_code: int | None = None |
| 51 | + ) -> JSONResponse: |
| 52 | + code = status_code |
| 53 | + if isinstance(message, self.error_class): |
| 54 | + exc = message |
| 55 | + fallback_status = getattr(exc, "status_code", status.HTTP_400_BAD_REQUEST) |
| 56 | + if code is None: |
| 57 | + if isinstance(fallback_status, int): |
| 58 | + code = fallback_status |
| 59 | + else: |
| 60 | + code = status.HTTP_400_BAD_REQUEST |
| 61 | + payload = self.response_class( |
| 62 | + success=False, |
| 63 | + message=getattr(exc, "message", str(exc)), |
| 64 | + errors=getattr(exc, "details", None), |
| 65 | + data=None, |
| 66 | + ).model_dump(exclude_none=True) |
| 67 | + return JSONResponse(status_code=int(code), content=payload) |
| 68 | + |
| 69 | + code = code if code is not None else status.HTTP_400_BAD_REQUEST |
| 70 | + msg = str(message) |
| 71 | + |
| 72 | + payload = self.response_class( |
| 73 | + success=False, |
| 74 | + message=msg, |
| 75 | + errors=errors, |
| 76 | + data=None, |
| 77 | + ).model_dump(exclude_none=True) |
| 78 | + |
| 79 | + return JSONResponse(status_code=int(code), content=payload) |
| 80 | + |
| 81 | + async def connect_account( |
| 82 | + self, |
| 83 | + request: ExternalAccountCreate, |
| 84 | + user_id: uuid.UUID, |
| 85 | + ) -> JSONResponse: |
| 86 | + try: |
| 87 | + account = await self.service.connect_account( |
| 88 | + user_id=user_id, |
| 89 | + provider=request.provider, |
| 90 | + provider_account_id=request.provider_account_id, |
| 91 | + access_token=request.access_token, |
| 92 | + refresh_token=request.refresh_token, |
| 93 | + extra_data=request.extra_data, |
| 94 | + ) |
| 95 | + return self._success(data=account, message="Account connected") |
| 96 | + except Exception as e: |
| 97 | + return self._error(message=e) |
| 98 | + |
| 99 | + async def get_google_drive_auth_url( |
| 100 | + self, |
| 101 | + user_id: uuid.UUID, |
| 102 | + ) -> JSONResponse: |
| 103 | + """Get Google Drive OAuth2 authorization URL""" |
| 104 | + try: |
| 105 | + auth_data = self.service.get_google_drive_auth_url(user_id=user_id) |
| 106 | + return self._success( |
| 107 | + data=auth_data, |
| 108 | + message="Google Drive authorization URL generated", |
| 109 | + ) |
| 110 | + except Exception as e: |
| 111 | + return self._error(message=e) |
| 112 | + |
| 113 | + async def google_drive_callback( |
| 114 | + self, |
| 115 | + code: str, |
| 116 | + user_id: uuid.UUID, |
| 117 | + state: str | None = None, |
| 118 | + ) -> JSONResponse: |
| 119 | + """Handle Google Drive OAuth2 callback""" |
| 120 | + try: |
| 121 | + account = await self.service.exchange_google_drive_code( |
| 122 | + code=code, |
| 123 | + user_id=user_id, |
| 124 | + ) |
| 125 | + return self._success( |
| 126 | + data=account, |
| 127 | + message="Google Drive account connected successfully", |
| 128 | + ) |
| 129 | + except Exception as e: |
| 130 | + return self._error(message=e) |
0 commit comments