|
24 | 24 | geopolygons_as_string, |
25 | 25 | ) |
26 | 26 | from parse_request import parse_request_parameters |
27 | | -from shared.database.database import with_db_session, refresh_materialized_view |
| 27 | +from shared.database.database import with_db_session |
28 | 28 | from shared.database_gen.sqlacodegen_models import ( |
29 | 29 | Geopolygon, |
30 | 30 | Feed, |
|
38 | 38 | ) |
39 | 39 | from shared.helpers.logger import get_logger |
40 | 40 |
|
| 41 | +import google.auth |
| 42 | +from google.auth.transport.requests import Request |
| 43 | +import google.auth.transport.requests |
| 44 | + |
41 | 45 |
|
42 | 46 | @with_db_session |
43 | 47 | def get_cached_geopolygons( |
@@ -378,11 +382,28 @@ def extract_location_aggregates( |
378 | 382 | db_session.commit() |
379 | 383 |
|
380 | 384 | # Replace direct call to refresh_materialized_view with HTTP request to GCP function |
381 | | - function_url = os.getenv("FUNCTION_URL_REFRESH_MV") |
382 | | - if not function_url: |
| 385 | + refresh_url = os.getenv("FUNCTION_URL_REFRESH_MV") |
| 386 | + if not refresh_url: |
383 | 387 | raise ValueError("FUNCTION_URL_REFRESH_MV environment variable is not set") |
384 | 388 |
|
385 | | - response = requests.get(f"{function_url}") |
| 389 | + # Fetch default credentials |
| 390 | + credentials, _ = google.auth.default() |
| 391 | + |
| 392 | + # Get an ID token for the function |
| 393 | + id_token_credentials = ( |
| 394 | + google.auth.transport.requests.IDTokenCredentials.from_credentials( |
| 395 | + credentials, target_audience=refresh_url |
| 396 | + ) |
| 397 | + ) |
| 398 | + |
| 399 | + # Refresh the token |
| 400 | + id_token_credentials.refresh(Request()) |
| 401 | + |
| 402 | + # Make the authenticated request |
| 403 | + response = requests.get( |
| 404 | + refresh_url, headers={"Authorization": f"Bearer {id_token_credentials.token}"} |
| 405 | + ) |
| 406 | + |
386 | 407 | response.raise_for_status() |
387 | 408 | logger.info("Materialized view refresh event triggered successfully.") |
388 | 409 |
|
|
0 commit comments