From 8740dbdb5f32d3351969748a77a7b6a12fc5e04d Mon Sep 17 00:00:00 2001 From: Brian Helba Date: Fri, 30 Jan 2026 01:22:38 -0500 Subject: [PATCH] Consistantly name `DJANGO_BATAI_NABAT_API_URL` and make it optional --- DEPLOYMENT.md | 3 ++- bats_ai/core/tasks/nabat/nabat_data_retrieval.py | 5 ++--- bats_ai/core/tasks/nabat/nabat_update_species.py | 5 ++--- bats_ai/core/views/nabat/nabat_recording.py | 15 ++++++++++----- bats_ai/settings/base.py | 4 ++++ dev/.env.docker-compose | 1 - dev/.env.docker-compose-native | 1 - prod/.env.kitware-production.template | 1 - prod/.env.nabat-production.template | 1 - 9 files changed, 20 insertions(+), 16 deletions(-) diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 34f3ca5c..0d2d2979 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -120,7 +120,8 @@ for a list of environment variables that you'll need to populate for your deploy - `DJANGO_CELERY_BROKER_URL` is used to make sure django can send tasks to the `celery` service. For example, if using [RabbitMQ](https://www.rabbitmq.com/), it might look like this: `amqp://rabbitmq:5672` - `AWS_*` and `DJANGO_STORAGE_BUCKET_NAME` are used to make sure the application can connect to your S3 bucket -- `NABAT_API_URL`: the location of the NABat GraphQL endpoint used to retrieve information about files in NABat. +- `DJANGO_BATAI_NABAT_API_URL` (optional): the location of the NABat GraphQL endpoint used to + retrieve information about files in NABat. - `VITE_APP_API_ROUTE`: this tells the Vue application where the backend (Django) API can be found. - `DJANGO_BATAI_URL_PATH`: this allows the Django application to be mounted at a subpath in a URL. It is used by the Django application itself and the nginx configuration at nginx.subpath.template diff --git a/bats_ai/core/tasks/nabat/nabat_data_retrieval.py b/bats_ai/core/tasks/nabat/nabat_data_retrieval.py index 4d2cc9de..59c3720c 100644 --- a/bats_ai/core/tasks/nabat/nabat_data_retrieval.py +++ b/bats_ai/core/tasks/nabat/nabat_data_retrieval.py @@ -1,7 +1,7 @@ import json import logging -import os +from django.conf import settings from django.contrib.gis.geos import Point from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.db import transaction @@ -18,7 +18,6 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger('NABatDataRetrieval') -BASE_URL = os.environ.get('NABAT_API_URL', 'https://api.sciencebase.gov/nabat-graphql/graphql') SOFTWARE_ID = 81 QUERY = """ query fetchAcousticAndSurveyEventInfo { @@ -124,7 +123,7 @@ def nabat_recording_initialize(self, recording_id: int, survey_event_id: int, ap ) try: response = requests.post( - BASE_URL, + settings.BATAI_NABAT_API_URL, json={'query': batch_query}, headers=headers, ) diff --git a/bats_ai/core/tasks/nabat/nabat_update_species.py b/bats_ai/core/tasks/nabat/nabat_update_species.py index f302e1e7..b6fb1c85 100644 --- a/bats_ai/core/tasks/nabat/nabat_update_species.py +++ b/bats_ai/core/tasks/nabat/nabat_update_species.py @@ -1,6 +1,6 @@ import logging -import os +from django.conf import settings from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist import requests @@ -11,7 +11,6 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger('NABatGetSpecies') -BASE_URL = os.environ.get('NABAT_API_URL', 'https://api.sciencebase.gov/nabat-graphql/graphql') QUERY = """ query GetAllSpeciesOptions { allSpecies { @@ -75,7 +74,7 @@ def update_nabat_species(self): processing_task.save() try: - response = requests.post(BASE_URL, json={'query': QUERY}) + response = requests.post(settings.BATAI_NABAT_API_URL, json={'query': QUERY}) response.raise_for_status() except Exception as e: processing_task.status = ProcessingTask.Status.ERROR diff --git a/bats_ai/core/views/nabat/nabat_recording.py b/bats_ai/core/views/nabat/nabat_recording.py index 9ce57349..8f2e7067 100644 --- a/bats_ai/core/views/nabat/nabat_recording.py +++ b/bats_ai/core/views/nabat/nabat_recording.py @@ -1,8 +1,8 @@ import base64 import json import logging -import os +from django.conf import settings from django.db import transaction from django.db.models import Q from django.http import HttpRequest, JsonResponse @@ -39,7 +39,6 @@ def admin_auth(request): SOFTWARE_ID = 81 -BASE_URL = os.environ.get('NABAT_API_URL', 'https://api.sciencebase.gov/nabat-graphql/graphql') QUERY = """ query fetchAcousticAndSurveyEventInfo { presignedUrlFromAcousticFile(acousticFileId: "%(acoustic_file_id)s") { @@ -128,7 +127,9 @@ def get_email_if_authorized( headers = {'Authorization': f'Bearer {api_token}', 'Content-Type': 'application/json'} query = QUERY % {'acoustic_file_id': recording_id} try: - response = requests.post(BASE_URL, json={'query': query}, headers=headers) + response = requests.post( + settings.BATAI_NABAT_API_URL, json={'query': query}, headers=headers + ) except Exception as e: logger.error(f'API request error: {e}') return JsonResponse({'error': 'Failed to connect to NABat API'}, status=500) @@ -184,7 +185,9 @@ def update_nabat_species(species_id: int, api_token: str, recording_id: int, sur 'species_id': species_id, } try: - response = requests.post(BASE_URL, json={'query': batch_query}, headers=headers) + response = requests.post( + settings.BATAI_NABAT_API_URL, json={'query': batch_query}, headers=headers + ) json_response = response.json() if json_response.get('errors'): logger.error(f'API Error: {json_response["errors"]}') @@ -241,7 +244,9 @@ def generate_nabat_recording( 'acoustic_file_id': recording_id, } try: - response = requests.post(BASE_URL, json={'query': batch_query}, headers=headers) + response = requests.post( + settings.BATAI_NABAT_API_URL, json={'query': batch_query}, headers=headers + ) logger.info(response.json()) except Exception: return JsonResponse(response.json(), status=500) diff --git a/bats_ai/settings/base.py b/bats_ai/settings/base.py index 2bec548b..39d68ae7 100644 --- a/bats_ai/settings/base.py +++ b/bats_ai/settings/base.py @@ -101,6 +101,10 @@ # Can be set to mount all URLs at a subpath BATAI_URL_PATH: str = env.str('DJANGO_BATAI_URL_PATH', default='').strip('/') +BATAI_NABAT_API_URL: str = env.str( + 'DJANGO_BATAI_NABAT_API_URL', default='https://api.sciencebase.gov/nabat-graphql/graphql' +) + # Django's docs suggest that STATIC_URL should be a relative path, # for convenience serving a site on a subpath. STATIC_URL = f'/{BATAI_URL_PATH}/static/' if BATAI_URL_PATH else '/static/' diff --git a/dev/.env.docker-compose b/dev/.env.docker-compose index d75d2092..f68e105e 100644 --- a/dev/.env.docker-compose +++ b/dev/.env.docker-compose @@ -12,6 +12,5 @@ DJANGO_INTERNAL_IPS=0.0.0.0/0 SERVERHOSTNAME=localhost -NABAT_API_URL=https://api.sciencebase.gov/nabat-graphql/graphql VITE_APP_API_ROOT=http://localhost:8000 VITE_APP_LOGIN_REDIRECT=http://localhost:3000 diff --git a/dev/.env.docker-compose-native b/dev/.env.docker-compose-native index 972b4d6c..1f16d840 100644 --- a/dev/.env.docker-compose-native +++ b/dev/.env.docker-compose-native @@ -5,6 +5,5 @@ DJANGO_MINIO_STORAGE_URL=http://minioAccessKey:minioSecretKey@localhost:9000/dja SERVERHOSTNAME=localhost -NABAT_API_URL=https://api.sciencebase.gov/nabat-graphql/graphql VITE_APP_API_ROOT=http://localhost:8000 VITE_APP_LOGIN_REDIRECT=http://localhost:3000 diff --git a/prod/.env.kitware-production.template b/prod/.env.kitware-production.template index 012770d4..169daf5e 100644 --- a/prod/.env.kitware-production.template +++ b/prod/.env.kitware-production.template @@ -29,7 +29,6 @@ DJANGO_DEFAULT_FROM_EMAIL SERVERHOSTNAME=batdetectai.kitware.com # Client -NABAT_API_URL=https://api.sciencebase.gov/nabat-graphql/graphql VITE_APP_API_ROOT=https://batdetectai.kitware.com VITE_APP_LOGIN_REDIRECT=https://batdetectai.kitware.com diff --git a/prod/.env.nabat-production.template b/prod/.env.nabat-production.template index b5f77b88..92d7fa3d 100644 --- a/prod/.env.nabat-production.template +++ b/prod/.env.nabat-production.template @@ -33,6 +33,5 @@ DJANGO_BATAI_URL_PATH= SERVERHOSTNAME=batdetectai.kitware.com # Client Environment Variables -NABAT_API_URL=https://api.sciencebase.gov/nabat-graphql/graphql VITE_APP_API_ROOT=https://batdetectai.kitware.com VITE_APP_LOGIN_REDIRECT=https://batdetectai.kitware.com