Skip to content

Mixins

AuthMixin dataclass

AuthMixin(*, context_name: str)

Mixin that provides authentication helpers for qlam API clients.

Manages an AppContext scoped to a qlam context name and ensures the client is authenticated before making API calls.

Attributes:

Name Type Description
context_name str

Name of the qlam context to use.

app_context property

app_context: AppContext

The AppContext used to authenticate and connect to the backend.

Returns:

Name Type Description
AppContext AppContext

An app context scoped to context_name.

authenticate

authenticate()

Ensure the client is authenticated, triggering a login if needed.

Returns:

Type Description

The login result when a login is performed; otherwise None.

Source code in src/bloqade/core/device/mixins.py
33
34
35
36
37
38
39
40
41
42
43
def authenticate(self):
    """Ensure the client is authenticated, triggering a login if needed.

    Returns:
        The login result when a login is performed; otherwise None.
    """
    with AuthClient(self.app_context) as client:
        if client.is_authenticated():
            return

        return client.login()

call_with_auth_refresh

call_with_auth_refresh(fn: Callable[[], T]) -> T

Run a qlam API call, refreshing credentials once on a 403.

If fn raises an APIError with status 403, a best-effort non-interactive credential refresh is attempted via AuthClient. When the refresh updates at least one provider's credentials, fn is invoked again. Any other error, or a refresh that produces no fresh credentials, propagates the original exception.

Only one retry is attempted; a second 403 is re-raised.

Parameters:

Name Type Description Default
fn Callable[[], T]

Zero-argument callable that performs the qlam API call.

required

Returns:

Name Type Description
T T

The value returned by fn.

Raises:

Type Description
APIError

When fn raises an APIError whose status is not 403, when refresh produces no fresh credentials, or when the retry also fails.

Source code in src/bloqade/core/device/mixins.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def call_with_auth_refresh(self, fn: Callable[[], T]) -> T:
    """Run a qlam API call, refreshing credentials once on a 403.

    If `fn` raises an `APIError` with status 403, a best-effort
    non-interactive credential refresh is attempted via `AuthClient`.
    When the refresh updates at least one provider's credentials, `fn`
    is invoked again. Any other error, or a refresh that produces no
    fresh credentials, propagates the original exception.

    Only one retry is attempted; a second 403 is re-raised.

    Args:
        fn (Callable[[], T]): Zero-argument callable that performs the
            qlam API call.

    Returns:
        T: The value returned by `fn`.

    Raises:
        APIError: When `fn` raises an `APIError` whose status is not 403,
            when refresh produces no fresh credentials, or when the retry
            also fails.
    """
    try:
        return fn()
    except APIError as e:
        if e.status_code != 403:
            raise
        with AuthClient(self.app_context) as client:
            refresh_results = client.refresh_credentials()
        if not any(refresh_results.values()):
            raise
        return fn()