Skip to content

Commit 5defee3

Browse files
fix(cli): use configured API endpoint and declare option defaults (#1145)
* fix(cli): use configured API endpoint and declare option defaults * fix(cli): use configured API endpoint in CLI commands * fix(cli): use configured API endpoint and restore typer compatibility --------- Co-authored-by: inimaz <49730431+inimaz@users.noreply.github.com>
1 parent 46e234b commit 5defee3

3 files changed

Lines changed: 45 additions & 11 deletions

File tree

codecarbon/cli/main.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def api_get():
114114
"""
115115
ex: test-api
116116
"""
117-
api = ApiClient(endpoint_url=API_URL) # TODO: get endpoint from config
117+
api_endpoint = get_api_endpoint()
118+
api = ApiClient(endpoint_url=api_endpoint)
118119
api.set_access_token(get_access_token())
119120
organizations = api.get_list_organizations()
120121
print(organizations)
@@ -123,15 +124,18 @@ def api_get():
123124
@codecarbon.command("login", short_help="Login to CodeCarbon")
124125
def login():
125126
authorize()
126-
api = ApiClient(endpoint_url=API_URL) # TODO: get endpoint from config
127+
api_endpoint = get_api_endpoint()
128+
api = ApiClient(endpoint_url=api_endpoint)
127129
access_token = get_access_token()
128130
api.set_access_token(access_token)
129131
api.check_auth()
130132

131133

132134
def get_api_key(project_id: str):
135+
api_endpoint = get_api_endpoint()
136+
api_endpoint = api_endpoint.rstrip("/")
133137
req = requests.post(
134-
f"{API_URL}/projects/{project_id}/api-tokens",
138+
f"{api_endpoint}/projects/{project_id}/api-tokens",
135139
json={
136140
"project_id": project_id,
137141
"name": "api token",
@@ -318,20 +322,25 @@ def config():
318322
def monitor(
319323
ctx: typer.Context,
320324
measure_power_secs: Annotated[
321-
int, typer.Option(help="Interval between two measures.")
325+
int,
326+
typer.Option(help="Interval between two measures."),
322327
] = 10,
323328
api_call_interval: Annotated[
324-
int, typer.Option(help="Number of measures between API calls.")
329+
int,
330+
typer.Option(help="Number of measures between API calls."),
325331
] = 30,
326332
api: Annotated[
327-
bool, typer.Option(help="Choose to call Code Carbon API or not")
333+
bool,
334+
typer.Option(help="Choose to call Code Carbon API or not"),
328335
] = True,
329336
offline: Annotated[bool, typer.Option(help="Run in offline mode")] = False,
330337
country_iso_code: Annotated[
331-
str, typer.Option(help="3-letter country ISO code for offline mode")
338+
str,
339+
typer.Option(help="3-letter country ISO code for offline mode"),
332340
] = None,
333341
region: Annotated[
334-
str, typer.Option(help="Region/province for offline mode")
342+
str,
343+
typer.Option(help="Region/province for offline mode"),
335344
] = None,
336345
):
337346
"""Monitor your machine's carbon emissions."""

codecarbon/cli/monitor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
def run_and_monitor(
1515
ctx: typer.Context,
1616
log_level: Annotated[
17-
str, typer.Option(help="Log level (critical, error, warning, info, debug)")
17+
str,
18+
typer.Option(help="Log level (critical, error, warning, info, debug)"),
1819
] = "error",
1920
**tracker_args,
2021
):

tests/cli/test_cli_main.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ def test_api_get_calls_api_and_prints(monkeypatch):
4242
assert "fake-org" in result.output
4343

4444

45+
def test_api_get_uses_get_api_endpoint(monkeypatch):
46+
call_info = {}
47+
48+
class CustomApiClient(FakeApiClient):
49+
def __init__(self, endpoint_url=None):
50+
call_info["endpoint_url"] = endpoint_url
51+
super().__init__(endpoint_url=endpoint_url)
52+
53+
runner = CliRunner()
54+
monkeypatch.setattr(cli_main, "ApiClient", CustomApiClient)
55+
monkeypatch.setattr(
56+
cli_main, "get_api_endpoint", lambda: "https://custom.codecarbon.io"
57+
)
58+
monkeypatch.setattr(cli_main, "get_access_token", fake_get_access_token)
59+
60+
result = runner.invoke(cli_main.codecarbon, ["test-api"])
61+
assert result.exit_code == 0
62+
assert call_info["endpoint_url"] == "https://custom.codecarbon.io"
63+
64+
4565
def test_monitor_offline_requires_country_iso_code():
4666
runner = CliRunner()
4767
result = runner.invoke(cli_main.codecarbon, ["monitor", "--offline"])
@@ -133,11 +153,11 @@ def fake_cli():
133153

134154

135155
def test_login_calls_authorize_and_auth_check(monkeypatch):
136-
calls = {"authorize": 0, "set_token": None, "check_auth": 0}
156+
calls = {"authorize": 0, "set_token": None, "check_auth": 0, "endpoint_url": None}
137157

138158
class FakeApiClient:
139159
def __init__(self, endpoint_url=None):
140-
self.endpoint_url = endpoint_url
160+
calls["endpoint_url"] = endpoint_url
141161

142162
def set_access_token(self, token):
143163
calls["set_token"] = token
@@ -151,6 +171,9 @@ def check_auth(self):
151171
"authorize",
152172
lambda: calls.__setitem__("authorize", calls["authorize"] + 1),
153173
)
174+
monkeypatch.setattr(
175+
cli_main, "get_api_endpoint", lambda: "https://custom-login.codecarbon.io"
176+
)
154177
monkeypatch.setattr(cli_main, "get_access_token", lambda: "login-token")
155178

156179
runner = CliRunner()
@@ -159,6 +182,7 @@ def check_auth(self):
159182
assert calls["authorize"] == 1
160183
assert calls["set_token"] == "login-token"
161184
assert calls["check_auth"] == 1
185+
assert calls["endpoint_url"] == "https://custom-login.codecarbon.io"
162186

163187

164188
def test_get_api_key_uses_bearer_token(monkeypatch):

0 commit comments

Comments
 (0)