Передать Bearer token#

Используйте BearerScheme, когда приложение уже получило access token и SDK не должен его обновлять. Схема добавляет префикс Bearer сама.

→ Запрос · GET /auth/bearer

{"headers": {"Authorization": "Bearer access-1"}}

← Ответ · 200 OK

{"credential": "access-1"}
# examples/docs/auth_bearer.py
ACCESS_TOKEN = BearerScheme("mail-access")


@dataclass(frozen=True, slots=True, kw_only=True)
class GetProfile(HttpOperation[AuthResult]):
    __http__ = Http.get("/auth/bearer", security=ACCESS_TOKEN)


class ProfileApi(SyncApi):
    get_profile = op(GetProfile)


def main() -> None:
    identity = Identity(auth=(ACCESS_TOKEN.static("access-1"),))
    with request_client() as client:
        result = ProfileApi(client, identity=identity).get_profile()
    print(f"token: {result.credential}")

Операция объявляет security=ACCESS_TOKEN, а Identity предоставляет значение этой же схемы. Если источник отсутствует, план вызова завершится до HTTP-запроса.

Для входа и автоматического обновления не заменяйте значение в Identity вручную. Используйте управляемую сессию из разделов про login и refresh.

Файл целиком
"""Send a Bearer token through a declared scheme."""

from dataclasses import dataclass

from pydantic import BaseModel

from eazy_sdk import Http, HttpOperation, Identity, SyncApi, op
from eazy_sdk.auth import BearerScheme
from examples.docs.request_site import request_client


class AuthResult(BaseModel):
    credential: str


# region docs: auth-bearer
# examples/docs/auth_bearer.py
ACCESS_TOKEN = BearerScheme("mail-access")


@dataclass(frozen=True, slots=True, kw_only=True)
class GetProfile(HttpOperation[AuthResult]):
    __http__ = Http.get("/auth/bearer", security=ACCESS_TOKEN)


class ProfileApi(SyncApi):
    get_profile = op(GetProfile)


def main() -> None:
    identity = Identity(auth=(ACCESS_TOKEN.static("access-1"),))
    with request_client() as client:
        result = ProfileApi(client, identity=identity).get_profile()
    print(f"token: {result.credential}")
# endregion docs: auth-bearer


if __name__ == "__main__":
    main()
token: access-1

Дальше#