feat: Make instance and secrets CLI argument

This commit is contained in:
moanos [he/him] 2025-01-26 17:59:48 +01:00
parent 885bed888d
commit 03a48da355

View File

@ -1,12 +1,40 @@
import argparse
import json
import os
import requests
OSM_DATA_FILE = "osm_data.geojson"
ENDPOINT = "https://test.notfellchen.org/api/organizations/"
HEADERS = {
"Authorization": "API_KEY",
"Content-Type": "application/json"
}
DEFAULT_OSM_DATA_FILE = "osm_data.geojson"
def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="Upload animal shelter data to the Notfellchen API.")
parser.add_argument("--api-token", type=str, help="API token for authentication.")
parser.add_argument("--instance", type=str, help="API instance URL.")
parser.add_argument("--data-file", type=str, help="Path to the GeoJSON file containing (only) animal shelters.")
return parser.parse_args()
def get_config():
"""Get configuration from environment variables or command-line arguments."""
args = parse_args()
api_token = args.api_token or os.getenv("NOTFELLCHEN_API_TOKEN")
instance = args.instance or os.getenv("NOTFELLCHEN_INSTANCE")
data_file = args.data_file or os.getenv("NOTFELLCHEN_DATA_FILE", DEFAULT_OSM_DATA_FILE)
if not api_token or not instance:
raise ValueError("API token and instance URL must be provided via environment variables or CLI arguments.")
return api_token, instance, data_file
def load_osm_data(file_path):
"""Load OSM data from a GeoJSON file."""
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
return data
def load_osm_data(file_path):
@ -33,9 +61,9 @@ def transform_osm_data(feature):
}
def send_to_api(data):
def send_to_api(data, endpoint, headers):
# Send transformed data to the Notfellchen API.
response = requests.post(ENDPOINT, headers=HEADERS, json=data)
response = requests.post(endpoint, headers=headers, json=data)
if response.status_code == 201:
print(f"Success: Shelter '{data['name']}' uploaded.")
elif response.status_code == 400:
@ -46,13 +74,24 @@ def send_to_api(data):
def main():
# Get configuration
api_token, instance, data_file = get_config()
# Set headers and endpoint
endpoint = f"{instance}/api/organizations/"
headers = {
"Authorization": f"Token {api_token}",
"Content-Type": "application/json"
}
# Step 1: Load OSM data
osm_data = load_osm_data(OSM_DATA_FILE)
osm_data = load_osm_data(data_file)
# Step 2: Process each shelter and send it to the API
for feature in osm_data.get("features", []):
shelter_data = transform_osm_data(feature)
send_to_api(shelter_data)
send_to_api(shelter_data, endpoint, headers)
if __name__ == "__main__":
main()