Skip to content

Commit

Permalink
Manage gfd credential.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Feb 6, 2025
1 parent db570a3 commit 9761943
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
34 changes: 23 additions & 11 deletions apps/etl/extraction/sources/gfd/extract.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import base64
import hashlib
import json
import logging
import os
import tempfile
from typing import Any, Callable

import ee
import requests
from django.conf import settings

from apps.etl.extraction.sources.base.handler import BaseExtraction
from apps.etl.extraction.sources.base.utils import manage_duplicate_file_content
Expand All @@ -20,10 +22,24 @@

class GFDExtraction(BaseExtraction):

@classmethod
def decode_json(cls, encoded_str):
"""Decodes a Base64 string back to a JSON object."""
decoded_data = base64.urlsafe_b64decode(encoded_str.encode()).decode()
return json.loads(decoded_data)

@classmethod
def get_json_credentials(self, content):
with tempfile.NamedTemporaryFile(delete=False, mode="w") as temp_file:
json_string = json.dumps(content, sort_keys=True)
temp_file.write(json_string)
temp_path = temp_file.name
return temp_path

@classmethod
def hash_json_content(cls, json_data):
"""Hashes a JSON object using SHA256."""
json_string = json.dumps(json_data, sort_keys=True) # Ensure consistent ordering
json_string = json.dumps(json_data, sort_keys=True)
return hashlib.sha256(json_string.encode()).hexdigest()

@classmethod
Expand Down Expand Up @@ -138,18 +154,14 @@ def handle_extraction(cls, url: str, source: int, start_date, end_date) -> int:
@classmethod
def extract_data(cls, start_date=None, end_date=None):
# Set up authentication
# TODO Remove this autherntication and use official authentication
service_account = "[email protected]"

# Get the script's directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
service_account = settings.GFD_SERVICE_ACCOUNT

# Construct the JSON file path
# TODO Add json key file official
json_key_path = os.path.join(BASE_DIR, "gidd-1671534293682-dab333fe24ec.json")
# # Decode the earthengine credential
decoded_json = cls.decode_json(settings.GFD_CREDENTIAL)
credential_file_path = cls.get_json_credentials(decoded_json)

# Authenticate
credentials = ee.ServiceAccountCredentials(service_account, json_key_path)
credentials = ee.ServiceAccountCredentials(service_account, credential_file_path)
ee.Initialize(credentials)

# Load Global Flood Database (GFD)
Expand Down
6 changes: 6 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@
EMDAT_AUTHORIZATION_KEY=str,
IDMC_CLIENT_ID=str,
IDMC_DATA_URL=(str, "https://helix-tools-api.idmcdb.org"),
GFD_CREDENTIAL=str,
GFD_SERVICE_ACCOUNT=str,
)

GFD_SERVICE_ACCOUNT = env("GFD_SERVICE_ACCOUNT")

GFD_CREDENTIAL = env("GFD_CREDENTIAL")

EMDAT_AUTHORIZATION_KEY = env("EMDAT_AUTHORIZATION_KEY")

IDMC_CLIENT_ID = env("IDMC_CLIENT_ID")
Expand Down

0 comments on commit 9761943

Please sign in to comment.