Skip to content

Commit

Permalink
ci(tm_api_key): Added tm API key support for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Mar 21, 2024
1 parent 1346426 commit 3f1a972
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ on:
workflow_dispatch:
inputs:
projectHours:
description: 'Number of hours to fetch active projects (1-24)'
description: "Number of hours to fetch active projects (1-24)"
required: false
default: "24" # Default value as a string
tasking_manager_api_key:
description: "Enter the TASKING MANAGER API KEY (leave blank if not applicable)"
required: false
default: '24' # Default value as a string

jobs:
Run-Scheduled-Exports:
Expand Down Expand Up @@ -41,3 +44,4 @@ jobs:
run: python tm_extractor.py --fetch-active-projects ${{ steps.project_hours.outputs.value }}
env:
RAWDATA_API_AUTH_TOKEN: ${{ secrets.RAWDATA_API_AUTH_TOKEN }}
TASKING_MANAGER_API_KEY: ${{ github.event.inputs.tasking_manager_api_key || 'None' }}
4 changes: 4 additions & 0 deletions .github/workflows/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
project_ids:
description: 'Enter space-separated project IDs (e.g., "123 456 789")'
required: true
tasking_manager_api_key:
description: "Enter the TASKING MANAGER API KEY (leave blank if not applicable)"
required: false

jobs:
Run-Manual-TM-Projects-Report:
Expand All @@ -25,3 +28,4 @@ jobs:
run: python tm_extractor.py --projects ${{ github.event.inputs.project_ids }}
env:
RAWDATA_API_AUTH_TOKEN: ${{ secrets.RAWDATA_API_AUTH_TOKEN }}
TASKING_MANAGER_API_KEY: ${{ github.event.inputs.tasking_manager_api_key || 'None' }}
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export RAWDATA_API_AUTH_TOKEN='my_token'

- **`CONFIG_JSON`**: Path to the config JSON file. Default is `config.json`.

- **`TASKING_MANAGER_API_KEY`**: [Optional] Tasking manager API key . Example : `Token your_token_key_from_tasking_manager`. Only required to fetch projects that requires authentication.

### Config JSON

The `config.json` file contains configuration settings for the extraction process. It includes details about the dataset, categories, and geometry of the extraction area.
Expand Down
18 changes: 16 additions & 2 deletions tm_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(
"https://tasking-manager-tm4-production-api.hotosm.org/api/v2",
)
self.RAWDATA_API_AUTH_TOKEN = os.environ.get("RAWDATA_API_AUTH_TOKEN")
self.TASKING_MANAGER_API_KEY = os.environ.get("TASKING_MANAGER_API_KEY", None)

def get_mapping_list(self, input_value):
if isinstance(input_value, int):
Expand Down Expand Up @@ -192,7 +193,10 @@ def get_project_details(self, project_id):
max_retries = 3
for retry in range(max_retries):
try:
response = requests.get(project_api_url, timeout=20)
headers = {"accept": "application/json"}
if self.TASKING_MANAGER_API_KEY:
headers["Authorization"] = self.TASKING_MANAGER_API_KEY
response = requests.get(project_api_url, timeout=20, headers=headers)
response.raise_for_status()
result = response.json()

Expand All @@ -212,7 +216,12 @@ def get_active_projects(self, time_interval):
for retry in range(max_retries):
try:
active_projects_api_url = f"{self.TM_API_BASE_URL}/projects/queries/active/?interval={time_interval}"
response = requests.get(active_projects_api_url, timeout=10)
headers = {"accept": "application/json"}
if self.TASKING_MANAGER_API_KEY:
headers["Authorization"] = self.TASKING_MANAGER_API_KEY
response = requests.get(
active_projects_api_url, timeout=10, headers=headers
)
response.raise_for_status()
return response.json()["features"]
except Exception as ex:
Expand Down Expand Up @@ -304,6 +313,11 @@ def main():
if os.environ.get("RAWDATA_API_AUTH_TOKEN", None) is None:
raise ValueError("RAWDATA_API_AUTH_TOKEN environment variable not found.")

if os.environ.get("TASKING_MANAGER_API_KEY", None) is None:
print(
"Tasking manager API key is not supplied , Authenticated endpoint won't be available"
)

project_processor = ProjectProcessor(config_json)
task_ids = project_processor.init_call(
projects=args.projects, fetch_active_projects=args.fetch_active_projects
Expand Down

0 comments on commit 3f1a972

Please sign in to comment.