diff --git a/datareservoirio/environments.py b/datareservoirio/environments.py index f6703fdb..ace6c6e0 100644 --- a/datareservoirio/environments.py +++ b/datareservoirio/environments.py @@ -43,6 +43,29 @@ def set_dev(self): _constants.APPLICATIONINSIGHTS_DEV_CONNECTIONSTRING ) + def set_api_base_url(self, base_url): + """ + Point the client at a custom DataReservoir.io API base URL. + + Intended for development/testing against a mock server. The + environment is marked as DEV, so telemetry (if explicitly enabled) + uses the DEV config and does not leak to the production resource. + + Parameters + ---------- + base_url : str + Full API base URL including the ``/api/`` suffix, e.g. + ``"http://localhost:5824/api/"``. A trailing slash is added if + missing. + """ + if not base_url.endswith("/"): + base_url = base_url + "/" + self._set_environment(_constants.ENV_DEV) + self._set_base_url(base_url) + self._set_application_insight_connectionstring( + _constants.APPLICATIONINSIGHTS_DEV_CONNECTIONSTRING + ) + def _set_environment(self, environment): self._logger.info(f"Setting environment to: {environment}") self.current_environment = environment diff --git a/tests/test_environments.py b/tests/test_environments.py index f3cce035..b38f1e2e 100644 --- a/tests/test_environments.py +++ b/tests/test_environments.py @@ -75,6 +75,19 @@ def test_set_dev(self, environment): == _constants.APPLICATIONINSIGHTS_DEV_CONNECTIONSTRING ) + def test_set_api_base_url(self, environment): + environment.set_api_base_url("http://drio-mock.gov:1337/api/") + assert environment.current_environment == "DEV" + assert environment.api_base_url == "http://drio-mock.gov:1337/api/" + assert ( + environment._application_insight_connectionstring + == _constants.APPLICATIONINSIGHTS_DEV_CONNECTIONSTRING + ) + + def test_set_api_base_url_adds_trailing_slash(self, environment): + environment.set_api_base_url("http://localhost:42/api") + assert environment.api_base_url == "http://localhost:42/api/" + def test__set_environment(self, environment): environment._set_environment("QA") assert environment.current_environment == "QA"