s365b.graph.client

Async Microsoft Graph client.

Behaviour every module gets for free:

  • Throttling: 429 and 503/504 responses are retried after the Retry-After delay (or exponential backoff with jitter). While one request is throttled, all requests through the same client pause, which is what Graph expects of a well-behaved client.

  • Concurrency limit: at most max_concurrency requests in flight.

  • Auth: a 401 triggers one token refresh and retry.

  • Paging: GraphClient.paginate() follows @odata.nextLink.

  • Delta queries: GraphClient.delta() returns items plus the new @odata.deltaLink; an expired link raises DeltaExpiredError so the module can do a full resync.

  • Batching: GraphClient.batch() sends up to 20 requests per call.

  • Streaming: GraphClient.stream() downloads content without holding it in memory.

Attributes

Exceptions

GraphError

An error response from Microsoft Graph.

DeltaExpiredError

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

Classes

DeltaResult

Result of a delta query.

BatchRequest

One request inside a JSON batch.

BatchResponse

One response from a JSON batch.

GraphClient

Async client for Microsoft Graph.

Module Contents

s365b.graph.client.GRAPH_V1 = 'https://graph.microsoft.com/v1.0'[source]
s365b.graph.client.RETRY_STATUSES[source]
s365b.graph.client.RETRY_STATUSES_UNSAFE[source]
s365b.graph.client.NOT_SENT[source]
s365b.graph.client.BATCH_LIMIT = 20[source]
s365b.graph.client.log[source]
exception s365b.graph.client.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[source]
code[source]
message[source]
request_id = None[source]
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).

exception s365b.graph.client.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.client.DeltaResult[source]

Result of a delta query.

Variables:
  • items – Changed items. Deleted items carry an @removed key.

  • delta_link – Link to pass next time to get only later changes (last page only).

  • next_link – Link to the next page; saving it lets an interrupted first sync resume where it stopped.

items: list[dict[str, Any]] = [][source]
class s365b.graph.client.BatchRequest[source]

One request inside a JSON batch.

Variables:
  • id – Caller-chosen ID to match responses.

  • method – HTTP method.

  • url – URL relative to the API version, e.g. /users/{id}.

  • body – Optional JSON body.

id: str[source]
method: str[source]
url: str[source]
body: dict[str, Any] | None = None[source]
class s365b.graph.client.BatchResponse[source]

One response from a JSON batch.

Variables:
  • id – Matching BatchRequest.id.

  • status – HTTP status of this sub-request.

  • body – Parsed body (may be empty).

id: str[source]
status: int[source]
body: dict[str, Any][source]
class s365b.graph.client.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 = ''[source]
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.