search a ads
This commit is contained in:
+22
-18
@@ -10,8 +10,9 @@ from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Path, Query
|
||||
|
||||
from ..clients.ga_client import GoogleAnalyticsClient
|
||||
from ..credentials import GaCredentials, get_ga_credentials
|
||||
from .. import config
|
||||
from ..clients.google import GoogleApiClient
|
||||
from ..credentials import GoogleCredentials, get_ga_credentials
|
||||
|
||||
router = APIRouter(prefix="/ga/admin", tags=["google-analytics: admin"])
|
||||
|
||||
@@ -21,15 +22,18 @@ def _property(property_id: str) -> str:
|
||||
return pid if pid.startswith("properties/") else f"properties/{pid}"
|
||||
|
||||
|
||||
def _client(creds: GoogleCredentials) -> GoogleApiClient:
|
||||
return GoogleApiClient(creds, service_name="Google Analytics")
|
||||
|
||||
|
||||
@router.get("/accounts", summary="List accessible GA4 accounts")
|
||||
async def list_accounts(
|
||||
page_size: int | None = Query(None, ge=1, le=200, alias="pageSize"),
|
||||
page_token: str | None = Query(None, alias="pageToken"),
|
||||
creds: GaCredentials = Depends(get_ga_credentials),
|
||||
creds: GoogleCredentials = Depends(get_ga_credentials),
|
||||
) -> Any:
|
||||
params = _paging(page_size, page_token)
|
||||
client = GoogleAnalyticsClient(creds)
|
||||
return await client.admin_get("/accounts", params=params)
|
||||
return await _client(creds).get(f"{config.GA_ADMIN_BASE_URL}/accounts", params)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -39,11 +43,12 @@ async def list_accounts(
|
||||
async def list_account_summaries(
|
||||
page_size: int | None = Query(None, ge=1, le=200, alias="pageSize"),
|
||||
page_token: str | None = Query(None, alias="pageToken"),
|
||||
creds: GaCredentials = Depends(get_ga_credentials),
|
||||
creds: GoogleCredentials = Depends(get_ga_credentials),
|
||||
) -> Any:
|
||||
params = _paging(page_size, page_token)
|
||||
client = GoogleAnalyticsClient(creds)
|
||||
return await client.admin_get("/accountSummaries", params=params)
|
||||
return await _client(creds).get(
|
||||
f"{config.GA_ADMIN_BASE_URL}/accountSummaries", params
|
||||
)
|
||||
|
||||
|
||||
@router.get("/properties", summary="List properties under an account")
|
||||
@@ -56,14 +61,13 @@ async def list_properties(
|
||||
page_size: int | None = Query(None, ge=1, le=200, alias="pageSize"),
|
||||
page_token: str | None = Query(None, alias="pageToken"),
|
||||
show_deleted: bool | None = Query(None, alias="showDeleted"),
|
||||
creds: GaCredentials = Depends(get_ga_credentials),
|
||||
creds: GoogleCredentials = Depends(get_ga_credentials),
|
||||
) -> Any:
|
||||
params: dict[str, Any] = {"filter": f"parent:accounts/{account_id.strip()}"}
|
||||
params.update(_paging(page_size, page_token))
|
||||
if show_deleted is not None:
|
||||
params["showDeleted"] = show_deleted
|
||||
client = GoogleAnalyticsClient(creds)
|
||||
return await client.admin_get("/properties", params=params)
|
||||
return await _client(creds).get(f"{config.GA_ADMIN_BASE_URL}/properties", params)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -72,10 +76,11 @@ async def list_properties(
|
||||
)
|
||||
async def get_property(
|
||||
property_id: str = Path(..., description="GA4 property id"),
|
||||
creds: GaCredentials = Depends(get_ga_credentials),
|
||||
creds: GoogleCredentials = Depends(get_ga_credentials),
|
||||
) -> Any:
|
||||
client = GoogleAnalyticsClient(creds)
|
||||
return await client.admin_get(f"/{_property(property_id)}")
|
||||
return await _client(creds).get(
|
||||
f"{config.GA_ADMIN_BASE_URL}/{_property(property_id)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -86,12 +91,11 @@ async def list_data_streams(
|
||||
property_id: str = Path(..., description="GA4 property id"),
|
||||
page_size: int | None = Query(None, ge=1, le=200, alias="pageSize"),
|
||||
page_token: str | None = Query(None, alias="pageToken"),
|
||||
creds: GaCredentials = Depends(get_ga_credentials),
|
||||
creds: GoogleCredentials = Depends(get_ga_credentials),
|
||||
) -> Any:
|
||||
params = _paging(page_size, page_token)
|
||||
client = GoogleAnalyticsClient(creds)
|
||||
return await client.admin_get(
|
||||
f"/{_property(property_id)}/dataStreams", params=params
|
||||
return await _client(creds).get(
|
||||
f"{config.GA_ADMIN_BASE_URL}/{_property(property_id)}/dataStreams", params
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user