import json import os from typing import Dict, Tuple from owslib.enums import Environment SERVICE_URL_SPEC = "{protocol}://{environment}-{service_name}.{domain}" SERVICE_DEFAULT_DOMAIN = "theorchard.io" def get_dev_service_mapping() -> Dict[str, str]: service_map = os.environ.get("OWSREQUEST_SERVICE_MAP") if service_map: try: mapping = json.loads(service_map) except (json.JSONDecodeError, TypeError): pass else: if isinstance(mapping, dict): return mapping return {} dev_service_mapping = get_dev_service_mapping() def discover_service_url( environment: Environment, service_name: str ) -> Tuple[Environment, str]: service_name = service_name.lower() if environment in [Environment.QA, Environment.PROD]: url = SERVICE_URL_SPEC.format( environment=environment.lower(), service_name=service_name, domain=SERVICE_DEFAULT_DOMAIN, protocol="https", ) return environment, url if ( environment in [Environment.TEST, Environment.DEV] and service_name in dev_service_mapping ): try: return environment, dev_service_mapping[service_name] except KeyError: pass # Default to qa url = SERVICE_URL_SPEC.format( environment=Environment.QA.lower(), service_name=service_name, domain=SERVICE_DEFAULT_DOMAIN, protocol="https", ) return Environment.QA, url