Skip to content

App Context

Application context for managing configuration, authentication, and HTTP clients.

Application context and dependency injection.

AppContext is a framework-agnostic dependency injection container that works with CLI, web applications, and programmatic usage. It provides access to config, auth, logging, and runtime configuration.

Typical usage::

from qlam_core.common.context import AppContext

# Basic usage
ctx = AppContext(context_name="dev", verbose=True)
print(ctx.config.context_name)

# Web application usage
web_ctx = AppContext(
    context_name=f"user_{user_id}",
    output_format="json",
    verbose=False
)

# Usage with display options
cli_ctx = AppContext(
    context_name="production",
    output_format="table",
    wrap=True
)

AppContext

AppContext(context_name: str | None = None, output_format: str | None = None, verbose: bool = False, wrap: bool = False, logger: Logger | None = None, cached_config: 'Config' | None = None, visibility: Visibility | None = None, default_headers: Mapping[str, str] | None = None)

Framework-agnostic dependency injection container.

This class provides access to config, authentication, and logging for any application interface (CLI, web, programmatic).

Parameters:

Name Type Description Default
context_name str | None

Optional context name override for user/session isolation.

None
output_format str | None

Output format preference ("table" or "json").

None
verbose bool

Enable verbose logging.

False
wrap bool

Allow multiline wrapping in table output (display hint).

False
logger Logger | None

Logger instance to use; defaults to module logger.

None
cached_config 'Config' | None

Inject config (avoids reloading from disk).

None
visibility Visibility | None

API visibility mode override (None defers to config resolution).

None
default_headers Mapping[str, str] | None

Default HTTP headers to apply to SDK-created clients. Stored as a read-only mapping after construction.

None
Source code in qlam_core/common/context.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def __init__(
    self,
    context_name: str | None = None,
    output_format: str | None = None,
    verbose: bool = False,
    wrap: bool = False,
    logger: logging.Logger | None = None,
    cached_config: "Config" | None = None,
    visibility: Visibility | None = None,
    default_headers: Mapping[str, str] | None = None,
) -> None:
    self.verbose = verbose
    self.output_format = output_format
    self.wrap = wrap
    self.visibility = visibility
    self._default_headers = self._validate_default_headers(default_headers)
    self.default_headers: Mapping[str, str] = MappingProxyType(self._default_headers)
    self.logger = logger or logging.getLogger(__name__)

    # Use cached config if provided, otherwise load fresh
    if cached_config:
        self.config = cached_config
        self.logger.debug(f"Using cached config for context: {self.config.context_name}")
    else:
        # Load config (with context selection) - fallback for non-cached usage
        self.config = Config.load(context_name)
        self.logger.debug(f"Loaded config for context: {self.config.context_name}")

    # No default auth provider - will be configured via plugins as needed
    self.auth: BaseAuthProvider | None = None

    # Plugins use the new client system directly via ClientConfigResolver
    # No centralized client factory or facades needed

    self.logger.debug("AppContext initialized successfully")

effective_output_format property

effective_output_format: str

Get the effective output format, considering TTY detection.

This property provides smart defaults for output formatting based on the execution environment. Useful for CLI but can be overridden for web/programmatic usage.

Returns:

Type Description
str

"table" for TTY environments, "json" otherwise, unless explicitly set.

qpu property

qpu: str

Get the QPU from config.

Returns:

Type Description
str

Currently configured QPU identifier.

credentials_view

credentials_view(redacted: bool = True) -> list[dict]

Get a read-only view of cached credentials across all providers.

Parameters:

Name Type Description Default
redacted bool

If True, hide sensitive fields.

True

Returns:

Type Description
list[dict]

List of cache metadata dicts.

Source code in qlam_core/common/context.py
142
143
144
145
146
147
148
def credentials_view(self, redacted: bool = True) -> list[dict]:
    """Get a read-only view of cached credentials across all providers.

    :param redacted: If True, hide sensitive fields.
    :return: List of cache metadata dicts.
    """
    return auth_registry.list_cache_metadata(redacted=redacted)