|
| 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 Collection""" |
| 7 | +import asyncio |
| 8 | + |
| 9 | +from urllib.request import Request |
| 10 | +from kiota_abstractions.request_information import RequestInformation |
| 11 | + |
| 12 | +from msgraph import GraphServiceClient |
| 13 | + |
| 14 | +from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders |
| 15 | +from msgraph_core.requests.batch_request_item import BatchRequestItem |
| 16 | + |
| 17 | +from msgraph_core.requests.batch_request_content import BatchRequestContent |
| 18 | +from msgraph_core.requests.batch_request_content_collection import BatchRequestContentCollection |
| 19 | +from msgraph_core.requests.batch_request_builder import BatchRequestBuilder |
| 20 | +# Create a client |
| 21 | +# code to create graph client |
| 22 | + |
| 23 | +user_client = GraphServiceClient(credentials=token, scopes=graph_scopes) |
| 24 | + |
| 25 | +# Create a request adapter from the client |
| 26 | +request_adapter = user_client.request_adapter |
| 27 | + |
| 28 | +# Create an instance of BatchRequestBuilder |
| 29 | +batch_request_builder = BatchRequestBuilder(request_adapter) |
| 30 | + |
| 31 | +# Create some BatchRequestItems |
| 32 | + |
| 33 | +request_info1 = RequestInformation() |
| 34 | +request_info1.http_method = "GET" |
| 35 | +request_info1.url = "/me" |
| 36 | +request_info1.headers = RequestHeaders() |
| 37 | +request_info1.headers.add("Content-Type", "application/json") |
| 38 | + |
| 39 | +request_info2 = RequestInformation() |
| 40 | +request_info2.http_method = "GET" |
| 41 | +request_info2.url = "/users" |
| 42 | +request_info2.headers = RequestHeaders() |
| 43 | +request_info2.headers.add("Content-Type", "application/json") |
| 44 | + |
| 45 | +request_info3 = RequestInformation() |
| 46 | +request_info3.http_method = "GET" |
| 47 | +request_info3.url = "/me" |
| 48 | +request_info3.headers = RequestHeaders() |
| 49 | +request_info3.headers.add("Content-Type", "application/json") |
| 50 | + |
| 51 | +# Create BatchRequestItem instances |
| 52 | +batch_request_item1 = BatchRequestItem(request_information=request_info1) |
| 53 | +batch_request_item2 = BatchRequestItem(request_information=request_info2) |
| 54 | + |
| 55 | +# Create batch request content |
| 56 | +# Create a BatchRequestContent instance and add requests |
| 57 | +batch_request_content_items = [batch_request_item1, batch_request_item2] |
| 58 | +batch_request_content = BatchRequestContent(requests=batch_request_content_items) |
| 59 | +batch_request_content.add_request(batch_request_item1) |
| 60 | +batch_request_content.add_request(batch_request_item2) |
| 61 | + |
| 62 | +# Add a request using RequestInformation directly |
| 63 | +batch_request_content.add_request_information(request_info1) |
| 64 | +print( |
| 65 | + f"Number of requests after adding request using RequestInformation: {len(batch_request_content.requests)}" |
| 66 | +) |
| 67 | +print("------------------------------------------------------------------------------------") |
| 68 | +# Create an instance of BatchRequestContentCollection |
| 69 | +collection = BatchRequestContentCollection() |
| 70 | +# Add request items to the collection |
| 71 | +batch_request_item_to_add = BatchRequestItem(request_information=request_info3) |
| 72 | +batch_request_item_to_add1 = BatchRequestItem(request_information=request_info3) |
| 73 | +batch_request_item_to_add2 = BatchRequestItem(request_information=request_info3) |
| 74 | +batch_request_item_to_add3 = BatchRequestItem(request_information=request_info3) |
| 75 | +batch_request_item_to_add4 = BatchRequestItem(request_information=request_info3) |
| 76 | + |
| 77 | +collection.add_batch_request_item(batch_request_item_to_add) |
| 78 | +collection.add_batch_request_item(batch_request_item_to_add1) |
| 79 | +collection.add_batch_request_item(batch_request_item_to_add2) |
| 80 | +collection.add_batch_request_item(batch_request_item_to_add3) |
| 81 | +collection.add_batch_request_item(batch_request_item_to_add4) |
| 82 | + |
| 83 | +# Print the current batch requests |
| 84 | +print("Current Batch Requests:") |
| 85 | +for request in collection.current_batch.requests: |
| 86 | + print(f"Request ID: {request.id}, Status Code: {request.headers}") |
| 87 | + |
| 88 | +# Remove a request item from the collection |
| 89 | +collection.remove_batch_request_item(batch_request_item_to_add.id) |
| 90 | +print(f"Items left in the batch after removal: {len(collection.current_batch.requests)}") |
| 91 | + |
| 92 | + |
| 93 | +# post a collection |
| 94 | +async def main(): |
| 95 | + |
| 96 | + batch_response_content = await batch_request_builder.post(batch_request_content=collection) |
| 97 | + responses = batch_response_content.get_responses() |
| 98 | + for item in responses: |
| 99 | + for item_body in item.responses: |
| 100 | + print(f"Item: {item_body.id}, Status Code: {item_body.status}") |
| 101 | + print(f"body: {item_body.body} Item headers: {item_body.headers} ") |
| 102 | + print("-----------------------------------------------") |
| 103 | + |
| 104 | + |
| 105 | +# Run the main function |
| 106 | +asyncio.run(main()) |
0 commit comments