diff --git a/scripts/bigquery_config.py b/scripts/bigquery_config.py index c74e87b..31685f0 100644 --- a/scripts/bigquery_config.py +++ b/scripts/bigquery_config.py @@ -33,6 +33,37 @@ CREDENTIALS_PATH = os.environ.get( PROJECT_ID = "food-security-asean-project" LOCATION = "asia-southeast2" + +# SETUP BIGQUERY CLIENT + +def get_bigquery_client() -> bigquery.Client: + """ + Create BigQuery client. + Priority: + 1. GOOGLE_CREDENTIALS_JSON env variable (Coolify/production) + 2. GOOGLE_APPLICATION_CREDENTIALS file path (lokal/development) + """ + credentials_json = os.environ.get("GOOGLE_CREDENTIALS_JSON") + + if credentials_json: + credentials_dict = json.loads(credentials_json) + credentials = service_account.Credentials.from_service_account_info( + credentials_dict, + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + else: + credentials = service_account.Credentials.from_service_account_file( + CREDENTIALS_PATH, + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + + return bigquery.Client( + credentials=credentials, + project=PROJECT_ID, + location=LOCATION + ) + + # DATASET IDs # Bronze = Raw Layer | Silver = Staging Layer | Gold = DW Layer (Kimball)