s365b.graph =========== .. py:module:: s365b.graph .. autoapi-nested-parse:: Microsoft Graph access shared by all modules. Throttling, retries, paging, delta queries, batching and authentication are handled here once, so modules only describe *what* to fetch. Submodules ---------- .. toctree:: :maxdepth: 1 /reference/api/s365b/graph/auth/index /reference/api/s365b/graph/client/index Exceptions ---------- .. autoapisummary:: s365b.graph.DeltaExpiredError s365b.graph.GraphError Classes ------- .. autoapisummary:: s365b.graph.CertificateCredential s365b.graph.TokenProvider s365b.graph.GraphClient Package Contents ---------------- .. py:class:: CertificateCredential(tenant_id: str, client_id: str, private_key_pem: str, certificate_pem: str, authority_host: str = 'https://login.microsoftonline.com') Client-credentials flow using a certificate (no client secret). :param tenant_id: Entra tenant ID. :param client_id: Application (client) ID of the app registration. :param private_key_pem: PEM private key matching the uploaded certificate. :param certificate_pem: PEM certificate uploaded to the app registration. :param authority_host: Login host; change only for sovereign clouds. .. py:attribute:: thumbprint .. py:attribute:: not_after .. py:method:: get_token(*, force_refresh: bool = False) -> str :async: Return an access token for Microsoft Graph. :param force_refresh: Skip MSAL's token cache. :returns: The access token. :raises AuthError: If Entra refuses the request (wrong certificate, app deleted, consent missing…). .. py:class:: TokenProvider Bases: :py:obj:`Protocol` Anything that can supply a bearer token for Graph. .. py:method:: get_token(*, force_refresh: bool = False) -> str :async: Return an access token. :param force_refresh: Ignore any cached token (used after a 401). :returns: The access token. .. py:exception:: DeltaExpiredError(status: int, code: str, message: str, request_id: str | None = None) Bases: :py:obj:`GraphError` The delta link is no longer valid; a full resync is needed. .. py:class:: GraphClient(token_provider: s365b.graph.auth.TokenProvider, *, base_url: str = GRAPH_V1, max_concurrency: int = 4, max_retries: int = 8, max_backoff: float = 120.0, http: httpx.AsyncClient | None = None) Async client for Microsoft Graph. :param token_provider: Supplies access tokens. :param base_url: API root, ``https://graph.microsoft.com/v1.0`` by default. :param max_concurrency: Maximum parallel requests. :param max_retries: Retries per request for throttling and server errors. :param max_backoff: Upper bound in seconds for any single wait. :param http: Optional pre-built ``httpx.AsyncClient`` (tests). .. py:attribute:: base_url :value: '' .. py:method:: aclose() -> None :async: Close the underlying HTTP connections. .. py:method:: request(method: str, url: str, *, json: Any = None, content: bytes | None = None, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None, authenticate: bool = True, idempotent: bool = True) -> httpx.Response :async: Send a request and return the (fully read) response. :param method: HTTP method. :param url: Absolute URL or path relative to :attr:`base_url`. :param json: JSON body. :param content: Raw body (e.g. a piece of a file upload), instead of ``json``. :param params: Query parameters. :param headers: Extra headers (e.g. ``ConsistencyLevel: eventual``). :param authenticate: False to send no access token (pre-authenticated URLs). :param idempotent: False for a request that creates something; it is not retried when it may already have been carried out. :returns: The successful response. :raises GraphError: For error responses after retries. .. py:method:: get_json(url: str, *, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> dict[str, Any] :async: GET a URL and return the JSON body. :param url: Absolute URL or path. :param params: Query parameters. :param headers: Extra headers, e.g. ``Prefer: IdType="ImmutableId"``. :returns: The parsed body. .. py:method:: paginate(url: str, *, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> collections.abc.AsyncIterator[dict[str, Any]] :async: Yield every item of a collection, following ``@odata.nextLink``. :param url: Collection URL or path. :param params: Query parameters for the first page. :param headers: Extra headers for every page. :Yields: Items from the ``value`` arrays. .. py:method:: delta(url: str, *, delta_link: str | None = None, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> collections.abc.AsyncIterator[DeltaResult] :async: Run a delta query page by page. Each yielded :class:`DeltaResult` holds one page of items; the last one also carries ``delta_link``. Modules can checkpoint after each page. :param url: Delta URL for the first full sync, e.g. ``/users/{id}/mailFolders/{fid}/messages/delta``. :param delta_link: Saved delta link from the previous run; when given, ``url`` and ``params`` are ignored. :param params: Query parameters for the initial request. :param headers: Extra headers for every page (e.g. ``Prefer`` for immutable IDs or page size). :Yields: Pages of changes. :raises DeltaExpiredError: If ``delta_link`` has expired (HTTP 410). .. py:method:: batch(requests: list[BatchRequest]) -> list[BatchResponse] :async: Send requests as JSON batches of up to 20, retrying throttled ones. :param requests: Requests with unique IDs. :returns: Responses in the same order as ``requests``. .. py:method:: stream(url: str, *, chunk_size: int = 1024 * 1024, headers: dict[str, str] | None = None) -> collections.abc.AsyncIterator[bytes] :async: Download content as a stream of byte chunks. :param url: Content URL, e.g. ``/users/{id}/messages/{mid}/$value``. :param chunk_size: Preferred piece size. :param headers: Extra headers. :Yields: Pieces of the body. .. py:exception:: GraphError(status: int, code: str, message: str, request_id: str | None = None) Bases: :py:obj:`Exception` An error response from Microsoft Graph. :ivar status: HTTP status code. :ivar code: Graph error code, e.g. ``"ErrorItemNotFound"``. :ivar message: Graph error message. :ivar request_id: ``request-id`` header, useful for Microsoft support. .. py:attribute:: status .. py:attribute:: code .. py:attribute:: message .. py:attribute:: request_id :value: None .. py:method:: from_response(resp: httpx.Response) -> GraphError :classmethod: Build an error from an HTTP response. :param resp: The failed response. :returns: The error (a :class:`DeltaExpiredError` for expired delta links).