|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from kiota_abstractions.request_information import RequestInformation |
| 4 | +from kiota_abstractions.method import Method |
| 5 | +from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders |
| 6 | + |
| 7 | +from msgraph_core.requests.batch_request_item import BatchRequestItem |
| 8 | +from msgraph_core.requests.batch_request_content import BatchRequestContent |
| 9 | +from msgraph_core.requests.batch_request_content_collection import BatchRequestContentCollection |
| 10 | + |
| 11 | +from msgraph_core.requests.batch_response_content import BatchResponseContent |
| 12 | +from msgraph_core.requests.batch_response_content_collection import BatchResponseContentCollection |
| 13 | +from msgraph_core.requests.batch_request_builder import BatchRequestBuilder |
| 14 | + |
| 15 | +# Create a client |
| 16 | +# code to create a graph client |
| 17 | +user_client = GraphServiceClient(credentials=token, scopes=graph_scopes) |
| 18 | + |
| 19 | +# Create a request adapter from the clinet |
| 20 | +request_adapter = user_client.request_adapter |
| 21 | + |
| 22 | +# Create an instance of BatchRequestBuilder |
| 23 | +batch_request_builder = BatchRequestBuilder(request_adapter) |
| 24 | + |
| 25 | +# Create batch Items |
| 26 | +request_info1 = RequestInformation() |
| 27 | +request_info1.http_method = "GET" |
| 28 | +request_info1.url = "https://graph.microsoft.com/v1.0/me" |
| 29 | +request_info1.url = "/me" |
| 30 | + |
| 31 | +request_info1.headers = RequestHeaders() |
| 32 | +request_info1.headers.add("Content-Type", "application/json") |
| 33 | + |
| 34 | +request_info2 = RequestInformation() |
| 35 | +request_info2.http_method = "GET" |
| 36 | +request_info2.url = "/users" |
| 37 | +request_info2.headers = RequestHeaders() |
| 38 | +request_info2.headers.add("Content-Type", "application/json") |
| 39 | + |
| 40 | +batch_request_item1 = BatchRequestItem(request_information=request_info1) |
| 41 | +batch_request_item2 = BatchRequestItem(request_information=request_info2) |
| 42 | + |
| 43 | +# Create a batch request content |
| 44 | +batch_request_content = [batch_request_item1, batch_request_item2] |
| 45 | +batch_content = BatchRequestContent(batch_request_content) |
| 46 | + |
| 47 | + |
| 48 | +async def main(): |
| 49 | + batch_response_content = await batch_request_builder.post(batch_request_content=batch_content) |
| 50 | + |
| 51 | + # Print the batch response content |
| 52 | + print(f"Batch Response Content: {batch_response_content.responses}") |
| 53 | + for response in batch_response_content.responses: |
| 54 | + print(f"Request ID: {response.id}, Status: {response.status}") |
| 55 | + print(f"Response body: {response.body}, headers: {response.headers}") |
| 56 | + print("-------------------------------------------------------------") |
| 57 | + |
| 58 | + |
| 59 | +asyncio.run(main()) |
0 commit comments