Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions codecarbon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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."""
Expand Down
3 changes: 2 additions & 1 deletion codecarbon/cli/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand Down
28 changes: 26 additions & 2 deletions tests/cli/test_cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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):
Expand Down
Loading