diff --git a/codecarbon/cli/main.py b/codecarbon/cli/main.py index b6e90fe76..4d9b41cd4 100644 --- a/codecarbon/cli/main.py +++ b/codecarbon/cli/main.py @@ -114,7 +114,8 @@ def api_get(): """ ex: test-api """ - api = ApiClient(endpoint_url=API_URL) # TODO: get endpoint from config + api_endpoint = get_api_endpoint() + api = ApiClient(endpoint_url=api_endpoint) api.set_access_token(get_access_token()) organizations = api.get_list_organizations() print(organizations) @@ -123,15 +124,18 @@ def api_get(): @codecarbon.command("login", short_help="Login to CodeCarbon") def login(): authorize() - api = ApiClient(endpoint_url=API_URL) # TODO: get endpoint from config + api_endpoint = get_api_endpoint() + api = ApiClient(endpoint_url=api_endpoint) access_token = get_access_token() api.set_access_token(access_token) api.check_auth() def get_api_key(project_id: str): + api_endpoint = get_api_endpoint() + api_endpoint = api_endpoint.rstrip("/") req = requests.post( - f"{API_URL}/projects/{project_id}/api-tokens", + f"{api_endpoint}/projects/{project_id}/api-tokens", json={ "project_id": project_id, "name": "api token", @@ -318,20 +322,25 @@ def config(): def monitor( ctx: typer.Context, measure_power_secs: Annotated[ - int, typer.Option(help="Interval between two measures.") + int, + typer.Option(help="Interval between two measures."), ] = 10, api_call_interval: Annotated[ - int, typer.Option(help="Number of measures between API calls.") + int, + typer.Option(help="Number of measures between API calls."), ] = 30, api: Annotated[ - bool, typer.Option(help="Choose to call Code Carbon API or not") + bool, + typer.Option(help="Choose to call Code Carbon API or not"), ] = True, offline: Annotated[bool, typer.Option(help="Run in offline mode")] = False, country_iso_code: Annotated[ - str, typer.Option(help="3-letter country ISO code for offline mode") + str, + typer.Option(help="3-letter country ISO code for offline mode"), ] = None, region: Annotated[ - str, typer.Option(help="Region/province for offline mode") + str, + typer.Option(help="Region/province for offline mode"), ] = None, ): """Monitor your machine's carbon emissions.""" diff --git a/codecarbon/cli/monitor.py b/codecarbon/cli/monitor.py index ce606cfb5..e5925e3fe 100644 --- a/codecarbon/cli/monitor.py +++ b/codecarbon/cli/monitor.py @@ -14,7 +14,8 @@ def run_and_monitor( ctx: typer.Context, log_level: Annotated[ - str, typer.Option(help="Log level (critical, error, warning, info, debug)") + str, + typer.Option(help="Log level (critical, error, warning, info, debug)"), ] = "error", **tracker_args, ): diff --git a/tests/cli/test_cli_main.py b/tests/cli/test_cli_main.py index d1d204cf6..f4cb2fef7 100644 --- a/tests/cli/test_cli_main.py +++ b/tests/cli/test_cli_main.py @@ -42,6 +42,26 @@ def test_api_get_calls_api_and_prints(monkeypatch): assert "fake-org" in result.output +def test_api_get_uses_get_api_endpoint(monkeypatch): + call_info = {} + + class CustomApiClient(FakeApiClient): + def __init__(self, endpoint_url=None): + call_info["endpoint_url"] = endpoint_url + super().__init__(endpoint_url=endpoint_url) + + runner = CliRunner() + monkeypatch.setattr(cli_main, "ApiClient", CustomApiClient) + monkeypatch.setattr( + cli_main, "get_api_endpoint", lambda: "https://custom.codecarbon.io" + ) + monkeypatch.setattr(cli_main, "get_access_token", fake_get_access_token) + + result = runner.invoke(cli_main.codecarbon, ["test-api"]) + assert result.exit_code == 0 + assert call_info["endpoint_url"] == "https://custom.codecarbon.io" + + def test_monitor_offline_requires_country_iso_code(): runner = CliRunner() result = runner.invoke(cli_main.codecarbon, ["monitor", "--offline"]) @@ -133,11 +153,11 @@ def fake_cli(): def test_login_calls_authorize_and_auth_check(monkeypatch): - calls = {"authorize": 0, "set_token": None, "check_auth": 0} + calls = {"authorize": 0, "set_token": None, "check_auth": 0, "endpoint_url": None} class FakeApiClient: def __init__(self, endpoint_url=None): - self.endpoint_url = endpoint_url + calls["endpoint_url"] = endpoint_url def set_access_token(self, token): calls["set_token"] = token @@ -151,6 +171,9 @@ def check_auth(self): "authorize", lambda: calls.__setitem__("authorize", calls["authorize"] + 1), ) + monkeypatch.setattr( + cli_main, "get_api_endpoint", lambda: "https://custom-login.codecarbon.io" + ) monkeypatch.setattr(cli_main, "get_access_token", lambda: "login-token") runner = CliRunner() @@ -159,6 +182,7 @@ def check_auth(self): assert calls["authorize"] == 1 assert calls["set_token"] == "login-token" assert calls["check_auth"] == 1 + assert calls["endpoint_url"] == "https://custom-login.codecarbon.io" def test_get_api_key_uses_bearer_token(monkeypatch):