s365b.graph.client¶
Async Microsoft Graph client.
Behaviour every module gets for free:
Throttling: 429 and 503/504 responses are retried after the
Retry-Afterdelay (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_concurrencyrequests 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 raisesDeltaExpiredErrorso 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¶
An error response from Microsoft Graph. |
|
The delta link is no longer valid; a full resync is needed. |
Classes¶
Result of a delta query. |
|
One request inside a JSON batch. |
|
One response from a JSON batch. |
|
Async client for Microsoft Graph. |
Module Contents¶
- exception s365b.graph.client.GraphError(status: int, code: str, message: str, request_id: str | None = None)[source]¶
Bases:
ExceptionAn error response from Microsoft Graph.
- Variables:
status – HTTP status code.
code – Graph error code, e.g.
"ErrorItemNotFound".message – Graph error message.
request_id –
request-idheader, useful for Microsoft support.
- classmethod from_response(resp: httpx.Response) GraphError[source]¶
Build an error from an HTTP response.
- Parameters:
resp – The failed response.
- Returns:
The error (a
DeltaExpiredErrorfor expired delta links).
- exception s365b.graph.client.DeltaExpiredError(status: int, code: str, message: str, request_id: str | None = None)[source]¶
Bases:
GraphErrorThe 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
@removedkey.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.
- 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.
- 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).
- 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.0by 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).
- 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
valuearrays.
- 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
DeltaResultholds one page of items; the last one also carriesdelta_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,
urlandparamsare ignored.params – Query parameters for the initial request.
headers – Extra headers for every page (e.g.
Preferfor immutable IDs or page size).
- Yields:
Pages of changes.
- Raises:
DeltaExpiredError – If
delta_linkhas 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.