import requests from agentm import DIAMBRA_CREDENTIALS_PATH from agentm.utils.logger import log_with_caller LOGIN_API = "https://api.diambra.ai/api/auth/token/" def login_to_diambra(email: str, password: str) -> str: """Log in to DIAMBRA and retrieve API token from /v1alpha1/token.""" payload = { "username": email, "password": password } response = requests.post("https://api.diambra.ai/api/v1alpha1/token", json=payload) response.raise_for_status() token = response.json().get("token") if not token: raise Exception("Login succeeded but no token returned.") log_with_caller("info", f"Successfully retrieved DIAMBRA token: {token[:6]}...") return token def save_diambra_token(token: str): """Writes the token to the .diambra/credentials file with no trailing newline.""" DIAMBRA_CREDENTIALS_PATH.write_bytes(token.encode("utf-8")) log_with_caller("info", f"Saved DIAMBRA token to: {DIAMBRA_CREDENTIALS_PATH}")