s365b.graph

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

Exceptions

DeltaExpiredError

The delta link is no longer valid; a full resync is needed.

GraphError

An error response from Microsoft Graph.

Classes

CertificateCredential

Client-credentials flow using a certificate (no client secret).

TokenProvider

Anything that can supply a bearer token for Graph.

GraphClient

Async client for Microsoft Graph.

Package Contents

class s365b.graph.CertificateCredential(tenant_id: str, client_id: str, private_key_pem: str, certificate_pem: str, authority_host: str = 'https://login.microsoftonline.com')[source]

Client-credentials flow using a certificate (no client secret).

Parameters:
  • tenant_id – Entra tenant ID.

  • client_id – Application (client) ID of the app registration.

  • private_key_pem – PEM private key matching the uploaded certificate.

  • certificate_pem – PEM certificate uploaded to the app registration.

  • authority_host – Login host; change only for sovereign clouds.

thumbprint
not_after
async get_token(*, force_refresh: bool = False) → str[source]

Return an access token for Microsoft Graph.

Parameters:

force_refresh – Skip MSAL’s token cache.

Returns:

The access token.

Raises:

AuthError – If Entra refuses the request (wrong certificate, app deleted, consent missing…).

class s365b.graph.TokenProvider[source]

Bases: Protocol

Anything that can supply a bearer token for Graph.

async get_token(*, force_refresh: bool = False) → str[source]

Return an access token.

Parameters:

force_refresh – Ignore any cached token (used after a 401).

Returns:

The access token.

exception s365b.graph.DeltaExpiredError(status: int, code: str, message: str, request_id: str | None = None)[source]

Bases: GraphError

The delta link is no longer valid; a full resync is needed.

class s365b.graph.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)[source]

Async client for Microsoft Graph.

Parameters:
  • token_provider – Supplies access tokens.

  • base_url – API root, https://graph.microsoft.com/v1.0 by default.

  • max_concurrency – Maximum parallel requests.

  • max_retries – Retries per request for throttling and server errors.

  • max_backoff – Upper bound in seconds for any single wait.

  • http – Optional pre-built httpx.AsyncClient (tests).

base_url = ''
async aclose() → None[source]

Close the underlying HTTP connections.

async 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[source]

Send a request and return the (fully read) response.

Parameters:
  • method – HTTP method.

  • url – Absolute URL or path relative to base_url.

  • json – JSON body.

  • content – Raw body (e.g. a piece of a file upload), instead of json.

  • params – Query parameters.

  • headers – Extra headers (e.g. ConsistencyLevel: eventual).

  • authenticate – False to send no access token (pre-authenticated URLs).

  • 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.

async get_json(url: str, *, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) → dict[str, Any][source]

GET a URL and return the JSON body.

Parameters:
  • url – Absolute URL or path.

  • params – Query parameters.

  • headers – Extra headers, e.g. Prefer: IdType="ImmutableId".

Returns:

The parsed body.

async paginate(url: str, *, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) → collections.abc.AsyncIterator[dict[str, Any]][source]

Yield every item of a collection, following @odata.nextLink.

Parameters:
  • url – Collection URL or path.

  • params – Query parameters for the first page.

  • headers – Extra headers for every page.

Yields:

Items from the value arrays.

async delta(url: str, *, delta_link: str | None = None, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) → collections.abc.AsyncIterator[DeltaResult][source]

Run a delta query page by page.

Each yielded DeltaResult holds one page of items; the last one also carries delta_link. Modules can checkpoint after each page.

Parameters:
  • url – Delta URL for the first full sync, e.g. /users/{id}/mailFolders/{fid}/messages/delta.

  • delta_link – Saved delta link from the previous run; when given, url and params are ignored.

  • params – Query parameters for the initial request.

  • 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).

async batch(requests: list[BatchRequest]) → list[BatchResponse][source]

Send requests as JSON batches of up to 20, retrying throttled ones.

Parameters:

requests – Requests with unique IDs.

Returns:

Responses in the same order as requests.

async stream(url: str, *, chunk_size: int = 1024 * 1024, headers: dict[str, str] | None = None) → collections.abc.AsyncIterator[bytes][source]

Download content as a stream of byte chunks.

Parameters:
  • url – Content URL, e.g. /users/{id}/messages/{mid}/$value.

  • chunk_size – Preferred piece size.

  • headers – Extra headers.

Yields:

Pieces of the body.

exception s365b.graph.GraphError(status: int, code: str, message: str, request_id: str | None = None)[source]

Bases: Exception

An error response from Microsoft Graph.

Variables:
  • status – HTTP status code.

  • code – Graph error code, e.g. "ErrorItemNotFound".

  • message – Graph error message.

  • request_id – request-id header, useful for Microsoft support.

status
code
message
request_id = None
classmethod from_response(resp: httpx.Response) → GraphError[source]

Build an error from an HTTP response.

Parameters:

resp – The failed response.

Returns:

The error (a DeltaExpiredError for expired delta links).