91e75e620b
CI: cua-driver distro-compat matrix / debian:12 (glibc 2.36) (push) Has been cancelled
CI: SPDX Headers / Check SPDX headers (warn-only) (push) Has been cancelled
CD: Docs MCP Server / build (linux/amd64) (push) Has been cancelled
CD: Docs MCP Server / build (linux/arm64) (push) Has been cancelled
CD: Docs MCP Server / merge (push) Has been cancelled
CI: cua-driver distro-compat matrix / Resolve release version (push) Has been cancelled
CI: cua-driver distro-compat matrix / fedora:41 (glibc 2.40) (push) Has been cancelled
CI: cua-driver distro-compat matrix / rockylinux:9 (glibc 2.34) (push) Has been cancelled
CI: cua-driver distro-compat matrix / ubuntu:22.04 (glibc 2.35) (push) Has been cancelled
CI: cua-driver distro-compat matrix / ubuntu:24.04 (glibc 2.39) (push) Has been cancelled
CI: cua-driver distro-compat matrix / Distro compat summary (push) Has been cancelled
CI: Rust Linux unit / Rust Linux unit and compile (push) Has been cancelled
CI: Rust Windows unit / Rust Windows unit and compile (push) Has been cancelled
CI: Nix Linux Rust source / Nix / compositor build (push) Has been cancelled
CI: Nix Linux Rust source / Nix / driver package (push) Has been cancelled
CI: Nix Linux Rust source / Nix / Rust unit tests (push) Has been cancelled
70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
"""Global configuration — configure(api_key, base_url).
|
|
|
|
Auth priority: per-call > configure() > ~/.cua/credentials > CUA_API_KEY env var.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass
|
|
class _Config:
|
|
api_key: Optional[str] = None
|
|
base_url: str = "https://api.cua.ai"
|
|
|
|
|
|
_global_config = _Config()
|
|
|
|
|
|
def configure(
|
|
*,
|
|
api_key: Optional[str] = None,
|
|
base_url: Optional[str] = None,
|
|
) -> None:
|
|
"""Set global configuration for the CUA SDK.
|
|
|
|
Args:
|
|
api_key: API key for cloud sandboxes.
|
|
base_url: Base URL for the CUA cloud API.
|
|
"""
|
|
if api_key is not None:
|
|
_global_config.api_key = api_key
|
|
if base_url is not None:
|
|
_global_config.base_url = base_url
|
|
|
|
|
|
def get_api_key(override: Optional[str] = None) -> Optional[str]:
|
|
"""Resolve API key with priority: override > configure() > credentials file > env."""
|
|
if override:
|
|
return override
|
|
if _global_config.api_key:
|
|
return _global_config.api_key
|
|
# Try credentials file
|
|
cred = _read_credentials_key()
|
|
if cred:
|
|
return cred
|
|
return os.environ.get("CUA_API_KEY")
|
|
|
|
|
|
def get_base_url() -> str:
|
|
return os.environ.get("CUA_BASE_URL") or _global_config.base_url
|
|
|
|
|
|
def _read_credentials_key() -> Optional[str]:
|
|
"""Read API key from ~/.cua/credentials if it exists."""
|
|
cred_path = os.path.join(os.path.expanduser("~"), ".cua", "credentials")
|
|
try:
|
|
with open(cred_path) as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line.startswith("api_key="):
|
|
return line[len("api_key=") :]
|
|
if line.startswith("api_key ="):
|
|
return line[len("api_key =") :].strip()
|
|
except FileNotFoundError:
|
|
pass
|
|
return None
|