diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 5946b57..a08ba66 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -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: @@ -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' }} diff --git a/.github/workflows/manual.yml b/.github/workflows/manual.yml index 2ec9ca8..b4bc926 100644 --- a/.github/workflows/manual.yml +++ b/.github/workflows/manual.yml @@ -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: @@ -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' }} diff --git a/Readme.md b/Readme.md index 752145a..23944ec 100644 --- a/Readme.md +++ b/Readme.md @@ -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. diff --git a/tm_extractor.py b/tm_extractor.py index 862e64d..a1979f1 100644 --- a/tm_extractor.py +++ b/tm_extractor.py @@ -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): @@ -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() @@ -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: @@ -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