Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions inference_cli/lib/enterprise/inference_compiler/constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import os

from inference_cli.lib.env import ROBOFLOW_REGION

PROD_ENVIRONMENT_NAME = "prod"
ROBOFLOW_ENVIRONMENT = os.getenv("ROBOFLOW_ENVIRONMENT", PROD_ENVIRONMENT_NAME)
ROBOFLOW_API_HOST = os.getenv(
"ROBOFLOW_API_HOST",
(
"https://api.roboflow.com"
if ROBOFLOW_ENVIRONMENT == PROD_ENVIRONMENT_NAME
else "https://api.roboflow.one"
),
)
if ROBOFLOW_REGION == "eu":
_DEFAULT_ROBOFLOW_API_HOST = "https://api.roboflow.eu"
elif ROBOFLOW_ENVIRONMENT == PROD_ENVIRONMENT_NAME:
_DEFAULT_ROBOFLOW_API_HOST = "https://api.roboflow.com"
else:
_DEFAULT_ROBOFLOW_API_HOST = "https://api.roboflow.one"
ROBOFLOW_API_HOST = os.getenv("ROBOFLOW_API_HOST", _DEFAULT_ROBOFLOW_API_HOST)
ROBOFLOW_API_KEY = os.getenv("ROBOFLOW_API_KEY", None)
HTTP_CODES_TO_RETRY = {408, 429, 500, 502, 503, 504}
YOLO_MODELS_MIN_DYNAMIC_BATCH_SIZE = int(
Expand Down
26 changes: 18 additions & 8 deletions inference_cli/lib/env.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import os
import sys

CLI_LOG_LEVEL = os.getenv("CLI_LOG_LEVEL", "INFO")
ROBOFLOW_API_KEY = os.getenv("ROBOFLOW_API_KEY")
PROJECT = os.getenv("PROJECT", "roboflow-platform")
API_BASE_URL = os.getenv(
"API_BASE_URL",
(
"https://api.roboflow.com"
if PROJECT == "roboflow-platform"
else "https://api.roboflow.one"
),
)

ROBOFLOW_REGION = os.getenv("ROBOFLOW_REGION", "us").strip().lower()
if ROBOFLOW_REGION not in {"us", "eu"}:
print(
f"Warning: unknown Roboflow region {ROBOFLOW_REGION!r}; falling back to 'us'.",
file=sys.stderr,
)
ROBOFLOW_REGION = "us"

if ROBOFLOW_REGION == "eu":
_DEFAULT_API_BASE_URL = "https://api.roboflow.eu"
elif PROJECT == "roboflow-platform":
_DEFAULT_API_BASE_URL = "https://api.roboflow.com"
else:
_DEFAULT_API_BASE_URL = "https://api.roboflow.one"

API_BASE_URL = os.getenv("API_BASE_URL", _DEFAULT_API_BASE_URL)
117 changes: 117 additions & 0 deletions tests/inference_cli/unit_tests/lib/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import importlib
import os
from typing import Callable, Tuple

import pytest

import inference_cli.lib.enterprise.inference_compiler.constants
import inference_cli.lib.env

ENVIRONMENT_KEYS = [
"ROBOFLOW_REGION",
"PROJECT",
"API_BASE_URL",
"ROBOFLOW_ENVIRONMENT",
"ROBOFLOW_API_HOST",
]


@pytest.fixture
def reload_env_modules() -> Callable[..., Tuple[object, object]]:
saved_environment = {
key: os.environ.pop(key) for key in ENVIRONMENT_KEYS if key in os.environ
}

def _reload(**environment: str) -> Tuple[object, object]:
for key in ENVIRONMENT_KEYS:
os.environ.pop(key, None)
os.environ.update(environment)
env_module = importlib.reload(inference_cli.lib.env)
constants_module = importlib.reload(
inference_cli.lib.enterprise.inference_compiler.constants
)
return env_module, constants_module

try:
yield _reload
finally:
for key in ENVIRONMENT_KEYS:
os.environ.pop(key, None)
os.environ.update(saved_environment)
importlib.reload(inference_cli.lib.env)
importlib.reload(inference_cli.lib.enterprise.inference_compiler.constants)


def test_api_urls_default_to_us_production(reload_env_modules) -> None:
# when
env_module, constants_module = reload_env_modules()

# then
assert env_module.ROBOFLOW_REGION == "us"
assert env_module.API_BASE_URL == "https://api.roboflow.com"
assert constants_module.ROBOFLOW_API_HOST == "https://api.roboflow.com"


def test_api_urls_honor_eu_region(reload_env_modules) -> None:
# when
env_module, constants_module = reload_env_modules(ROBOFLOW_REGION="eu")

# then
assert env_module.ROBOFLOW_REGION == "eu"
assert env_module.API_BASE_URL == "https://api.roboflow.eu"
assert constants_module.ROBOFLOW_API_HOST == "https://api.roboflow.eu"


def test_region_value_is_normalized(reload_env_modules) -> None:
# when
env_module, _ = reload_env_modules(ROBOFLOW_REGION=" EU ")

# then
assert env_module.ROBOFLOW_REGION == "eu"
assert env_module.API_BASE_URL == "https://api.roboflow.eu"


def test_explicit_url_overrides_beat_region(reload_env_modules) -> None:
# when
env_module, constants_module = reload_env_modules(
ROBOFLOW_REGION="eu",
API_BASE_URL="https://api.example.com",
ROBOFLOW_API_HOST="https://api-host.example.com",
)

# then
assert env_module.API_BASE_URL == "https://api.example.com"
assert constants_module.ROBOFLOW_API_HOST == "https://api-host.example.com"


def test_unknown_region_warns_and_falls_back_to_us(reload_env_modules, capsys) -> None:
# when
env_module, constants_module = reload_env_modules(ROBOFLOW_REGION="mars")

# then
assert env_module.ROBOFLOW_REGION == "us"
assert env_module.API_BASE_URL == "https://api.roboflow.com"
assert constants_module.ROBOFLOW_API_HOST == "https://api.roboflow.com"
assert "unknown Roboflow region" in capsys.readouterr().err


def test_non_platform_project_still_selects_staging_api(reload_env_modules) -> None:
# when
env_module, constants_module = reload_env_modules(
PROJECT="roboflow-staging", ROBOFLOW_ENVIRONMENT="staging"
)

# then
assert env_module.API_BASE_URL == "https://api.roboflow.one"
assert constants_module.ROBOFLOW_API_HOST == "https://api.roboflow.one"


def test_eu_region_beats_project_and_environment_defaults(reload_env_modules) -> None:
# when
env_module, constants_module = reload_env_modules(
ROBOFLOW_REGION="eu", PROJECT="roboflow-staging", ROBOFLOW_ENVIRONMENT="staging"
)

# then
assert env_module.API_BASE_URL == "https://api.roboflow.eu"
assert constants_module.ROBOFLOW_API_HOST == "https://api.roboflow.eu"
Loading