|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +#pylint: disable=undefined-variable |
| 6 | +"""Demonstrates using the Batch request with Parsable Resposnse Type""" |
| 7 | +import asyncio |
| 8 | + |
| 9 | +from kiota_abstractions.request_adapter import RequestAdapter |
| 10 | +from kiota_abstractions.request_information import RequestInformation |
| 11 | +from kiota_abstractions.method import Method |
| 12 | +from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders |
| 13 | + |
| 14 | +from msgraph_core.requests.batch_request_item import BatchRequestItem |
| 15 | +from msgraph_core.requests.batch_request_content import BatchRequestContent |
| 16 | + |
| 17 | +from msgraph_core.requests.batch_request_builder import BatchRequestBuilder |
| 18 | + |
| 19 | +# import User model to serialize to |
| 20 | +from msgraph.generated.models.user import User |
| 21 | +# Create a client |
| 22 | +# code to create graph client |
| 23 | +user_client = GraphServiceClient(credentials=token, scopes=graph_scopes) |
| 24 | + |
| 25 | +print(f"Graph Scopes: {graph_scopes}") |
| 26 | + |
| 27 | +# Create a request adapter from the client |
| 28 | +request_adapter = user_client.request_adapter |
| 29 | + |
| 30 | +# Create an instance of BatchRequestBuilder |
| 31 | +batch_request_builder = BatchRequestBuilder(request_adapter) |
| 32 | + |
| 33 | +# Create batch Items |
| 34 | +request_info1 = RequestInformation() |
| 35 | +request_info1.http_method = "GET" |
| 36 | +request_info1.url = "/users/<user-id-1>" |
| 37 | + |
| 38 | +request_info1.headers = RequestHeaders() |
| 39 | +request_info1.headers.add("Content-Type", "application/json") |
| 40 | + |
| 41 | +request_info2 = RequestInformation() |
| 42 | +request_info2.http_method = "GET" |
| 43 | +request_info2.url = "/users/<user-id-2>" |
| 44 | +request_info2.headers = RequestHeaders() |
| 45 | +request_info2.headers.add("Content-Type", "application/json") |
| 46 | + |
| 47 | +# bacth request items to be added to content |
| 48 | +batch_request_item1 = BatchRequestItem(request_information=request_info1) |
| 49 | +batch_request_item2 = BatchRequestItem(request_information=request_info2) |
| 50 | + |
| 51 | +# Create a batch request content |
| 52 | +batch_request_content = [batch_request_item1, batch_request_item2] |
| 53 | +batch_content = BatchRequestContent(batch_request_content) |
| 54 | + |
| 55 | + |
| 56 | +async def main(): |
| 57 | + # Send the batch request and get the response content |
| 58 | + batch_response_content = await batch_request_builder.post( |
| 59 | + batch_request_content=batch_content, response_type=User |
| 60 | + ) |
| 61 | + |
| 62 | + # Print the batch response content - User model type |
| 63 | + print(f"Batch Response Content: {batch_response_content}") |
| 64 | + |
| 65 | + |
| 66 | +asyncio.run(main()) |
0 commit comments