Skip to content

Skaha Client

The skaha.client module provides a comprehensive HTTP client for interacting with CANFAR Science Platform services. Built on the powerful httpx library, it offers both synchronous and asynchronous interfaces with advanced authentication capabilities.

Features

Key Capabilities

  • Multiple Authentication Methods: X.509 certificates, OIDC tokens, and bearer tokens
  • Automatic SSL Configuration: Seamless certificate-based authentication
  • Async/Sync Support: Both synchronous and asynchronous HTTP clients
  • Connection Pooling: Optimized for concurrent requests
  • Debug Logging: Comprehensive logging for troubleshooting
  • Context Managers: Proper resource management

This is a low-level client that is used by all other API clients in Skaha. It is not intended to be used directly by users, but rather as a building block for other clients and contributors.

Authentication Modes

The client supports multiple authentication modes that can be configured through the authentication system:

Debug Logging

Python
import logging

# Enable debug logging to see client creation details
client = SkahaClient(loglevel=logging.DEBUG)

# This will log:
# - Authentication mode selection
# - SSL context creation
# - Header generation
# - Client configuration

Configuration

The client inherits from the Configuration class and supports all configuration options:

Python
from skaha.client import SkahaClient

client = SkahaClient(
    timeout=60,           # Request timeout in seconds
    concurrency=64,       # Max concurrent connections
    loglevel=20,         # Logging level (INFO)
)

Authentication Expiry

The client provides an expiry property that returns the expiry time for the current authentication method:

Python
import time

client = SkahaClient()

if client.expiry:
    time_left = client.expiry - time.time()
    print(f"Authentication expires in {time_left:.0f} seconds")
else:
    print("No expiry tracking (user-provided credentials)")

Expiry Tracking

The expiry property returns None for user-provided certificates or tokens since the client cannot track their expiry automatically.

Error Handling

The client includes built-in error handling for HTTP responses:

Python
from httpx import HTTPStatusError

try:
    response = client.client.get("/invalid-endpoint")
    response.raise_for_status()
except HTTPStatusError as e:
    print(f"HTTP error: {e.response.status_code}")

API Reference

skaha.client.SkahaClient

Bases: BaseSettings

Skaha Client for interacting with CANFAR Science Platform services (V2).

This client uses a composition-based approach and inherits from Pydantic's BaseSettings to allow for flexible configuration via arguments, environment variables, or a configuration file.

The client prioritizes credentials in the following order: 1. Runtime Arguments/Environment Variables: A token or certificate provided at instantiation (e.g., SKAHA_TOKEN="..."). 2. Active Configuration Context: The context specified by active_context in the loaded configuration file.

Raises:

Type Description
ValueError

If configuration is invalid.

client: Client property

Get the synchronous HTTPx Client.

Returns:

Name Type Description
Client Client

The synchronous HTTPx client.

asynclient: AsyncClient property

Get the asynchronous HTTPx Async Client.

_get_ssl_context(source: Path) -> ssl.SSLContext

Get SSL context from certificate file.

Parameters:

Name Type Description Default
source Path

Path to the certificate file.

required

Returns:

Type Description
SSLContext

ssl.SSLContext: SSL context.