search a ads

This commit is contained in:
JiriUhlir
2026-06-18 12:43:42 +02:00
parent 9cd5c8011b
commit 125b112967
12 changed files with 748 additions and 191 deletions
+26 -32
View File
@@ -13,8 +13,9 @@ from typing import Any
from fastapi import APIRouter, Body, Depends, Path
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/data", tags=["google-analytics: data"])
@@ -32,6 +33,14 @@ 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")
def _url(property_id: str, suffix: str) -> str:
return f"{config.GA_DATA_BASE_URL}/{_property(property_id)}{suffix}"
@router.post(
"/properties/{property_id}/runReport",
summary="Run a GA4 report",
@@ -39,10 +48,9 @@ def _property(property_id: str) -> str:
async def run_report(
property_id: str = Path(..., description="GA4 property id, e.g. 123456789"),
body: dict[str, Any] = Body(..., examples=[_RUN_REPORT_EXAMPLE]),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(f"/{_property(property_id)}:runReport", body)
return await _client(creds).post(_url(property_id, ":runReport"), body)
@router.post(
@@ -52,12 +60,9 @@ async def run_report(
async def run_pivot_report(
property_id: str = Path(..., description="GA4 property id"),
body: dict[str, Any] = Body(...),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(
f"/{_property(property_id)}:runPivotReport", body
)
return await _client(creds).post(_url(property_id, ":runPivotReport"), body)
@router.post(
@@ -67,12 +72,9 @@ async def run_pivot_report(
async def batch_run_reports(
property_id: str = Path(..., description="GA4 property id"),
body: dict[str, Any] = Body(...),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(
f"/{_property(property_id)}:batchRunReports", body
)
return await _client(creds).post(_url(property_id, ":batchRunReports"), body)
@router.post(
@@ -82,11 +84,10 @@ async def batch_run_reports(
async def batch_run_pivot_reports(
property_id: str = Path(..., description="GA4 property id"),
body: dict[str, Any] = Body(...),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(
f"/{_property(property_id)}:batchRunPivotReports", body
return await _client(creds).post(
_url(property_id, ":batchRunPivotReports"), body
)
@@ -97,12 +98,9 @@ async def batch_run_pivot_reports(
async def run_realtime_report(
property_id: str = Path(..., description="GA4 property id"),
body: dict[str, Any] = Body(...),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(
f"/{_property(property_id)}:runRealtimeReport", body
)
return await _client(creds).post(_url(property_id, ":runRealtimeReport"), body)
@router.post(
@@ -112,12 +110,9 @@ async def run_realtime_report(
async def check_compatibility(
property_id: str = Path(..., description="GA4 property id"),
body: dict[str, Any] = Body(...),
creds: GaCredentials = Depends(get_ga_credentials),
creds: GoogleCredentials = Depends(get_ga_credentials),
) -> Any:
client = GoogleAnalyticsClient(creds)
return await client.data_post(
f"/{_property(property_id)}:checkCompatibility", body
)
return await _client(creds).post(_url(property_id, ":checkCompatibility"), body)
@router.get(
@@ -126,7 +121,6 @@ async def check_compatibility(
)
async def get_metadata(
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.data_get(f"/{_property(property_id)}/metadata")
return await _client(creds).get(_url(property_id, "/metadata"))