Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/library_development.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ MDC_API_URL=https://your-dev-environment.example/api
Notes:

- The override applies to the SDK API requests.
- The cutover compatibility shim rewrites the legacy production host
`https://datacollective.mozillafoundation.org/api` to
`https://mozilladatacollective.com/api`.
- Custom non-production URLs are passed through unchanged.

## Live E2E tests
Expand Down
28 changes: 1 addition & 27 deletions src/datacollective/api_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import platform
import warnings
from pathlib import Path
from typing import Any

Expand All @@ -13,15 +12,13 @@


DEFAULT_API_URL = "https://mozilladatacollective.com/api"
LEGACY_API_URL = "https://datacollective.mozillafoundation.org/api"
SCHEMA_REGISTRY_RAW_BASE_URL = (
"https://raw.githubusercontent.com/Mozilla-Data-Collective/dataset-schema-registry"
)
ENV_API_KEY = "MDC_API_KEY"
ENV_API_URL = "MDC_API_URL"
ENV_DOWNLOAD_PATH = "MDC_DOWNLOAD_PATH"
HTTP_TIMEOUT = (10, 60) # (connect, read)
_LEGACY_API_URL_NOTICE_EMITTED = False
_SENSITIVE_KEY_SUBSTRINGS = (
"url",
"token",
Expand Down Expand Up @@ -115,30 +112,7 @@ def _send_api_request(


def _get_api_url() -> str:
configured_url = os.getenv(ENV_API_URL, DEFAULT_API_URL).rstrip("/")
# Overwrite legacy URL during runtime
if configured_url == LEGACY_API_URL:
_warn_legacy_api_url_once()
return DEFAULT_API_URL
return configured_url


def _warn_legacy_api_url_once() -> None:
global _LEGACY_API_URL_NOTICE_EMITTED

if _LEGACY_API_URL_NOTICE_EMITTED:
return None

_LEGACY_API_URL_NOTICE_EMITTED = True
message = (
f"`{ENV_API_URL}` is set to the legacy API URL `{LEGACY_API_URL}`. "
f"The SDK is using `{DEFAULT_API_URL}` instead. "
f"Update the variable `{ENV_API_URL}` in your `.env` file to the new URL"
f", or completely remove `{ENV_API_URL}` to use the SDK default."
)
warnings.warn(message, FutureWarning, stacklevel=3)
logger.warning(message)
return None
return os.getenv(ENV_API_URL, DEFAULT_API_URL).rstrip("/")


def _extract_error_detail(resp: requests.Response) -> str:
Expand Down
51 changes: 4 additions & 47 deletions tests/test_api_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
import re
import warnings

import datacollective.api_utils as api_utils

Expand All @@ -19,58 +17,17 @@ def test_get_user_agent_appends_download_source_token() -> None:
assert user_agent.endswith("source function: load_dataset")


def test_get_api_url_uses_new_default_without_warning(monkeypatch) -> None:
def test_get_api_url_uses_new_default(monkeypatch) -> None:
monkeypatch.delenv(api_utils.ENV_API_URL, raising=False)
monkeypatch.setattr(api_utils, "_LEGACY_API_URL_NOTICE_EMITTED", False)

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
api_url = api_utils._get_api_url()
assert api_utils._get_api_url() == api_utils.DEFAULT_API_URL

assert api_url == api_utils.DEFAULT_API_URL
assert not caught


def test_get_api_url_rewrites_legacy_env_and_warns_once(monkeypatch, caplog) -> None:
monkeypatch.setenv(api_utils.ENV_API_URL, api_utils.LEGACY_API_URL)
monkeypatch.setattr(api_utils, "_LEGACY_API_URL_NOTICE_EMITTED", False)

with (
warnings.catch_warnings(record=True) as caught,
caplog.at_level(logging.WARNING, logger="datacollective.api_utils"),
):
warnings.simplefilter("always")
first_url = api_utils._get_api_url()
second_url = api_utils._get_api_url()

assert first_url == api_utils.DEFAULT_API_URL
assert second_url == api_utils.DEFAULT_API_URL
assert len(caught) == 1

warning_message = str(caught[0].message)
assert api_utils.LEGACY_API_URL in warning_message
assert api_utils.DEFAULT_API_URL in warning_message
assert "set to the legacy API URL" in warning_message

logged_messages = [
record.getMessage()
for record in caplog.records
if record.name == "datacollective.api_utils"
]
assert logged_messages == [warning_message]


def test_get_api_url_preserves_custom_url_without_warning(monkeypatch) -> None:
def test_get_api_url_preserves_custom_url(monkeypatch) -> None:
custom_url = "https://api.example.test/custom"
monkeypatch.setenv(api_utils.ENV_API_URL, custom_url)
monkeypatch.setattr(api_utils, "_LEGACY_API_URL_NOTICE_EMITTED", False)

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
api_url = api_utils._get_api_url()

assert api_url == "https://api.example.test/custom"
assert not caught
assert api_utils._get_api_url() == custom_url


def test_redact_sensitive_masks_urls_tokens_and_emails() -> None:
Expand Down
Loading