Spaces:
Running
Running
| from __future__ import annotations | |
| import time | |
| from fastapi import APIRouter, HTTPException | |
| from app.models.schemas import ( | |
| GoogleApiScopesResponse, | |
| GoogleSetScopesRequest, | |
| ) | |
| from app.services.google_scope_service import GoogleScopeError | |
| from .deps import GENERATED, REFERENCE, SOURCE, get_scope_service | |
| router = APIRouter() | |
| async def set_google_scopes(body: GoogleSetScopesRequest) -> GoogleApiScopesResponse: | |
| """Set the current OAuth scopes for one or more Google integrations. | |
| The selection is validated against the catalog and stored in memory as a | |
| Python object structure (resets on restart). Pass ``reset: true`` to restore | |
| recommended defaults. | |
| """ | |
| started = time.perf_counter() | |
| service = get_scope_service() | |
| try: | |
| state = await service.set_current_scopes( | |
| updates=body.apis or {}, reset=body.reset | |
| ) | |
| plans = await service.load_all_plans() | |
| data = service.build_data(plans, state) | |
| return GoogleApiScopesResponse( | |
| success=True, | |
| time_ms=round((time.perf_counter() - started) * 1000, 3), | |
| count=len(data), | |
| total=len(data), | |
| generated=GENERATED, | |
| reference=REFERENCE, | |
| source=SOURCE, | |
| data=data, | |
| ) | |
| except GoogleScopeError as exc: | |
| raise HTTPException(status_code=exc.status_code, detail=exc.message) from exc | |