diff --git a/.env.example b/.env.example index 5e54b56..b60b966 100644 --- a/.env.example +++ b/.env.example @@ -12,3 +12,6 @@ DATABASE_URI=postgresql://postgres:postgres@localhost:5432/atol_db JWT_SECRET_KEY= JWT_ALGORITHM=HS256 # BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8000"] + +# NCBI API key +NCBI_API_KEY= diff --git a/app/core/settings.py b/app/core/settings.py index 9f7143f..d1134ff 100644 --- a/app/core/settings.py +++ b/app/core/settings.py @@ -37,6 +37,9 @@ class Settings(BaseSettings): ENVIRONMENT: Optional[str] = None # Options: "dev", "prod" APP_VERSION: str = "dev" + # NCBI api key + NCBI_API_KEY: Optional[str] = None + # Model config model_config = SettingsConfigDict( env_file=".env", diff --git a/app/services/ncbi_taxonomy_service.py b/app/services/ncbi_taxonomy_service.py index 32e303b..d9430fb 100644 --- a/app/services/ncbi_taxonomy_service.py +++ b/app/services/ncbi_taxonomy_service.py @@ -6,6 +6,8 @@ import requests +from app.core.settings import settings + logger = logging.getLogger(__name__) # Adapted from: @@ -55,12 +57,16 @@ def fetch_reports( ) -> list[dict[str, Any]]: """Fetch taxonomy report dicts for a batch of tax_ids.""" url = build_taxonomy_url(tax_ids, endpoint) - logger.info("Fetching NCBI %s reports for tax_ids=%s", endpoint, tax_ids) + request_params = {} + if settings.NCBI_API_KEY: + request_params["api_key"] = settings.NCBI_API_KEY + logger.info("Fetching NCBI %s reports for tax_ids=%s", endpoint, tax_ids) + for attempt in range(1, max_retries + 1): try: with _ncbi_semaphore: - response = requests.get(url, timeout=timeout_seconds) + response = requests.get(url, timeout=timeout_seconds, params=request_params) response.raise_for_status() payload = response.json() reports = payload.get("reports", [])