File size: 1,477 Bytes
55ecbeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()


@router.put("", response_model=GoogleApiScopesResponse)
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