Client setup¶
Synchronous client¶
Use a context manager to close pooled connections deterministically:
from hubuum_client import Client, Credentials
with Client("https://hubuum.example.com") as client:
client.login_session(Credentials("alice", "secret"))
me = client.request("GET", "/api/v1/iam/me")
login_session() authenticates the current client and returns it for optional chaining.
Access and refresh credentials stay in memory. Requests and streams refresh
expired access credentials under a shared lock; uncertain rotation clears the
credentials and requires a new login. The legacy login() method remains
available only for older servers that still expose /api/v0/auth.
The resulting client.token.expires_at contains the authoritative expiry
persisted by Hubuum v0.0.22. A token supplied directly at construction has no
known expiry unless its AccessToken value includes one.
An existing bearer token can be supplied at construction:
logout() invalidates the current token on the server and always clears the
local token, including when the server reports an error.
Application lifetime and connection reuse¶
Each Client owns an HTTPX connection pool. A context-managed client is already
persistent for the entire with block; it is not recreated for each request.
Keep that block around the application's work when the application's lifetime
fits naturally into one scope:
from hubuum_client import Client, Credentials
with Client("https://hubuum.example.com") as client:
client.login_session(Credentials("alice", "secret"))
run_application(client)
Frameworks, workers, and dependency-injection containers can instead create the client during application startup and close it during shutdown. The Server class below comes from the Atlas example inventory:
from hubuum_client import Client, Credentials
client = Client("https://hubuum.example.com")
try:
client.login_session(Credentials("alice", "secret"))
servers = client.classes.by_name("Server").objects
for server in servers.all():
print(server.name)
finally:
client.close()
The asynchronous client follows the same ownership pattern:
from hubuum_client import AsyncClient, Credentials
client = AsyncClient("https://hubuum.example.com")
try:
await client.login_session(Credentials("alice", "secret"))
servers = client.classes.by_name("Server").objects
for server in await servers.all():
print(server.name)
finally:
await client.close()
Reuse a client for a meaningful application lifetime rather than constructing
one for every API call. HTTPX reuses eligible TCP/TLS connections from the
client's pool and transparently opens replacements when a connection is no
longer usable. close() releases local network resources; logout() also
invalidates the current bearer token on the server.
Asynchronous client¶
The async client mirrors the resource surface and uses HTTPX's async connection pool:
from hubuum_client import AsyncClient, Credentials
async with AsyncClient("https://hubuum.example.com") as client:
await client.login_session(Credentials("alice", "secret"))
collections = await client.collections.list()
Only I/O methods are awaited. Query construction and Pydantic models are the same in both modes.
Create and close an AsyncClient within the same application and event-loop
lifetime.
Resource-service accessors such as client.classes are created lazily and
cached for the lifetime of each client. Class-scoped services such as
client.classes.by_id(class_id).objects remain created on demand for the
requested class.
Identity scopes¶
Provider-scoped authentication uses the optional identity_scope value:
credentials = Credentials(
name="alice",
password="secret",
identity_scope="company-directory",
)
client.login_session(credentials)
HTTP configuration¶
Both clients accept a shared ClientOptions value:
from hubuum_client import Client, ClientOptions
client = Client(
"https://hubuum.example.com",
options=ClientOptions(timeout=15.0, user_agent="inventory-service/1.0"),
)
ClientOptions contains:
timeout: a float or HTTPX timeout object;verify: certificate validation settings, enabled by default;user_agent: an application-specific user agent.
The optional transport constructor argument accepts an HTTPX transport. It is
primarily useful for explicit proxy policies and deterministic tests.
The base URL must be an absolute HTTP or HTTPS URL without credentials, a query
string, or a fragment. A deployment prefix such as
https://example.test/hubuum/ is preserved.
Warning
Setting verify=False disables server certificate authentication. Use it
only for a disposable local system whose network you control.
Public probes and configuration¶
Authentication is not required for:
health = client.healthz()
readiness = client.readyz()
config = client.config()
prometheus_text = client.metrics()
Metrics are fetched without bearer authentication. If Hubuum is configured
with a non-default metrics route, use metrics_at("/custom/metrics"); the path
is subject to the same origin and traversal protections as every request.
The typed client configuration contains the server's effective pagination defaults and the default PAT lifetime applied when token minting omits an explicit expiry:
default_hours = config.authentication.default_token_lifetime_hours
default_page_size = config.pagination.default_page_limit
maximum_page_size = config.pagination.max_page_limit
Rotating human sessions¶
On the supported Server v0.0.22, use the versioned session API:
from hubuum_client import Client, Credentials
with Client("https://hubuum.example.org") as client:
client.login_session(Credentials("alice", "secret"), client_id="my-cli")
print(client.me().principal.name)
client.logout()
The asynchronous equivalent is await client.login_session(...). Client IDs
contain 1–128 ASCII letters, digits, -, ., _, or :. They identify the
client binding and are not embedded client secrets. The default is hubuum-python.
Each client serializes refresh across its concurrent requests and streams, checks again after acquiring the lock, and atomically installs the new pair. Refresh starts before access expiry using a bounded skew allowance, capped at 30 seconds or one tenth of the received access lifetime. Idle and absolute expiry remain server-authoritative. The original absolute deadline and family identity must be preserved in every rotation response.
Neither credential is persisted. client.token exposes the current redacted
access-token wrapper. SessionResponse secret fields redact representations and
JSON dumps; explicit get_secret_value() is required to extract them. The SDK
does not retain the password or offer automatic refresh-token disk storage.
Use one client instance for concurrent work; separate clients do not coordinate
refresh. Application-owned secure storage needs its own coordination strategy.
An uncertain refresh may already have committed on the server. Transport errors,
invalid/rejected responses and async cancellation therefore clear the local
session without retry. Expiry raises SessionExpiredError; HTTP and transport
failures use their existing exception types. Authenticate again. Ordinary
resource 401/403 responses are never retried as a refresh side effect.
logout() uses the versioned endpoint for interactive sessions and clears both
credentials even if revocation fails. Closing the HTTP client is not server-side
logout. Fresh authentication approvals remain required for PAT/service-token
creation; use the existing credential workflow.
The legacy login() method still uses /api/v0/auth/login. Upgraded servers that
remove those routes require login_session(). Explicit PAT injection remains
available for automation and does not trigger refresh.
Injected PATs can also be logged out against the versioned server: logout falls
back to /api/v1/auth/logout only if the legacy route returns HTTP 404.