|
| 1 | +"""Canonical Roboflow region / environment selection. |
| 2 | +
|
| 3 | +Single source of truth for resolving default Roboflow service URLs from the |
| 4 | +two public switches: |
| 5 | +
|
| 6 | +* ``ROBOFLOW_REGION`` - ``us`` (default) or ``eu`` |
| 7 | +* ``ROBOFLOW_ENVIRONMENT`` - ``prod`` (default) or ``staging`` |
| 8 | +
|
| 9 | +Explicit URL env variables (``API_BASE_URL``, ``ROBOFLOW_API_HOST``, |
| 10 | +``RF_API_BASE_URL``, ...) always take precedence over values resolved here - |
| 11 | +callers apply this module's output only as the default. The legacy |
| 12 | +``PROJECT`` variable (``roboflow-platform`` vs anything else) is honoured as |
| 13 | +an environment signal wherever it was honoured before, with |
| 14 | +``ROBOFLOW_ENVIRONMENT`` taking precedence when both are set. |
| 15 | +
|
| 16 | +This module deliberately mirrors ``inference_sdk/regions.py`` - ``inference`` |
| 17 | +core config must not import ``inference_sdk`` at module level. Keep both |
| 18 | +copies (and the inline matrix in ``inference_models/configuration.py``) in |
| 19 | +sync; a parity test guards the first two. |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import warnings |
| 24 | +from typing import Optional |
| 25 | + |
| 26 | +PROD_ENVIRONMENT_NAME = "prod" |
| 27 | +STAGING_ENVIRONMENT_NAME = "staging" |
| 28 | +US_PROD_PROJECT_NAME = "roboflow-platform" |
| 29 | + |
| 30 | +DEFAULT_REGION = "us" |
| 31 | +DEFAULT_ENVIRONMENT = PROD_ENVIRONMENT_NAME |
| 32 | + |
| 33 | +ROBOFLOW_SERVICE_URLS = { |
| 34 | + ("us", "prod"): { |
| 35 | + "api": "https://api.roboflow.com", |
| 36 | + "app": "https://app.roboflow.com", |
| 37 | + "serverless": "https://serverless.roboflow.com", |
| 38 | + }, |
| 39 | + ("us", "staging"): { |
| 40 | + "api": "https://api.roboflow.one", |
| 41 | + "app": "https://app.roboflow.one", |
| 42 | + "serverless": "https://serverless.roboflow.one", |
| 43 | + }, |
| 44 | + ("eu", "prod"): { |
| 45 | + "api": "https://api.roboflow.eu", |
| 46 | + "app": "https://app.roboflow.eu", |
| 47 | + "serverless": "https://serverless.roboflow.eu", |
| 48 | + }, |
| 49 | + ("eu", "staging"): { |
| 50 | + "api": "https://api.roboflow-eu.one", |
| 51 | + "app": "https://app.roboflow-eu.one", |
| 52 | + "serverless": "https://serverless.roboflow-eu.one", |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +SUPPORTED_REGIONS = sorted({region for region, _ in ROBOFLOW_SERVICE_URLS}) |
| 57 | + |
| 58 | + |
| 59 | +def get_roboflow_region() -> str: |
| 60 | + """Return the selected Roboflow region (``us`` or ``eu``). |
| 61 | +
|
| 62 | + Unknown values warn on stderr and fall back to ``us`` - never raise at |
| 63 | + import time. |
| 64 | + """ |
| 65 | + region = os.getenv("ROBOFLOW_REGION", DEFAULT_REGION).strip().lower() |
| 66 | + if region not in SUPPORTED_REGIONS: |
| 67 | + warnings.warn( |
| 68 | + f"Unknown ROBOFLOW_REGION {region!r} - falling back to " |
| 69 | + f"{DEFAULT_REGION!r}. Supported regions: {', '.join(SUPPORTED_REGIONS)}.", |
| 70 | + ) |
| 71 | + return DEFAULT_REGION |
| 72 | + return region |
| 73 | + |
| 74 | + |
| 75 | +def get_roboflow_environment(project: Optional[str] = None) -> str: |
| 76 | + """Return the selected Roboflow environment (``prod`` or ``staging``). |
| 77 | +
|
| 78 | + ``ROBOFLOW_ENVIRONMENT`` wins when set (any value other than ``prod`` |
| 79 | + selects staging, matching historical behaviour). Otherwise the legacy |
| 80 | + ``project`` signal decides (``roboflow-platform`` means prod), and with |
| 81 | + neither present the environment defaults to prod. |
| 82 | + """ |
| 83 | + environment = os.getenv("ROBOFLOW_ENVIRONMENT") |
| 84 | + if environment is not None: |
| 85 | + if environment.strip().lower() == PROD_ENVIRONMENT_NAME: |
| 86 | + return PROD_ENVIRONMENT_NAME |
| 87 | + return STAGING_ENVIRONMENT_NAME |
| 88 | + if project is not None and project != US_PROD_PROJECT_NAME: |
| 89 | + return STAGING_ENVIRONMENT_NAME |
| 90 | + return DEFAULT_ENVIRONMENT |
| 91 | + |
| 92 | + |
| 93 | +def resolve_roboflow_service_url( |
| 94 | + service: str, |
| 95 | + region: Optional[str] = None, |
| 96 | + environment: Optional[str] = None, |
| 97 | + project: Optional[str] = None, |
| 98 | +) -> str: |
| 99 | + """Resolve the default URL for a Roboflow service in the selected region / environment.""" |
| 100 | + if region is None: |
| 101 | + region = get_roboflow_region() |
| 102 | + if environment is None: |
| 103 | + environment = get_roboflow_environment(project=project) |
| 104 | + return ROBOFLOW_SERVICE_URLS[(region, environment)][service] |
0 commit comments