Skip to content

Commit 784d8d6

Browse files
authored
Merge pull request #685 from microsoftgraph/shem/batch_requests_samples
Batch Requests Samples
2 parents 92ddc32 + 1dadf66 commit 784d8d6

6 files changed

+376
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
""" Demonstrate getting response body as stream in Batch Responses"""
7+
batch_request_content = {
8+
batch_request_item1.id: batch_request_item1,
9+
batch_request_item2.id: batch_request_item2
10+
}
11+
12+
batch_content = BatchRequestContent(batch_request_content)
13+
14+
15+
async def main():
16+
batch_response_content = await client.batch.post(batch_request_content=batch_content)
17+
18+
try:
19+
stream_response = batch_response_content.get_response_stream_by_id(batch_request_item1.id)
20+
print(f"Stream Response: {stream_response}")
21+
print(f"Stream Response Content: {stream_response.read()}")
22+
except AttributeError as e:
23+
print(f"Error getting response by ID: {e}")
24+
25+
26+
asyncio.run(main())
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
# Create a client
20+
# code to create graph client
21+
22+
graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
23+
24+
# Create a request adapter from the client
25+
request_adapter = graph_client.request_adapter
26+
27+
# Create some BatchRequestItems
28+
29+
request_info1 = RequestInformation()
30+
request_info1.http_method = "GET"
31+
request_info1.url = "/me"
32+
request_info1.headers = RequestHeaders()
33+
request_info1.headers.add("Content-Type", "application/json")
34+
35+
request_info2 = RequestInformation()
36+
request_info2.http_method = "GET"
37+
request_info2.url = "/users"
38+
request_info2.headers = RequestHeaders()
39+
request_info2.headers.add("Content-Type", "application/json")
40+
41+
request_info3 = RequestInformation()
42+
request_info3.http_method = "GET"
43+
request_info3.url = "/me"
44+
request_info3.headers = RequestHeaders()
45+
request_info3.headers.add("Content-Type", "application/json")
46+
47+
# Create BatchRequestItem instances
48+
batch_request_item1 = BatchRequestItem(request_information=request_info1)
49+
batch_request_item2 = BatchRequestItem(request_information=request_info2)
50+
51+
# Add a request using RequestInformation directly
52+
batch_request_content.add_request_information(request_info1)
53+
print(
54+
f"Number of requests after adding request using RequestInformation: {len(batch_request_content.requests)}"
55+
)
56+
print("------------------------------------------------------------------------------------")
57+
# Create an instance of BatchRequestContentCollection
58+
collection = BatchRequestContentCollection()
59+
# Add request items to the collection
60+
batch_request_item_to_add = BatchRequestItem(request_information=request_info3)
61+
batch_request_item_to_add1 = BatchRequestItem(request_information=request_info3)
62+
batch_request_item_to_add2 = BatchRequestItem(request_information=request_info3)
63+
batch_request_item_to_add3 = BatchRequestItem(request_information=request_info3)
64+
batch_request_item_to_add4 = BatchRequestItem(request_information=request_info3)
65+
66+
collection.add_batch_request_item(batch_request_item_to_add)
67+
collection.add_batch_request_item(batch_request_item_to_add1)
68+
collection.add_batch_request_item(batch_request_item_to_add2)
69+
collection.add_batch_request_item(batch_request_item_to_add3)
70+
collection.add_batch_request_item(batch_request_item_to_add4)
71+
72+
# Print the current batch requests
73+
print("Current Batch Requests:")
74+
for request in collection.current_batch.requests:
75+
print(f"Request ID: {request.id}, Status Code: {request.headers}")
76+
77+
# Remove a request item from the collection
78+
collection.remove_batch_request_item(batch_request_item_to_add.id)
79+
print(f"Items left in the batch after removal: {len(collection.current_batch.requests)}")
80+
81+
82+
# post a collection
83+
async def main():
84+
85+
batch_response_content = await graph_client.batch.post(batch_request_content=collection)
86+
responses = batch_response_content.get_responses()
87+
for item in responses:
88+
for item_body in item.responses:
89+
print(f"Item: {item_body.id}, Status Code: {item_body.status}")
90+
print(f"body: {item_body.body} Item headers: {item_body.headers} ")
91+
print("-----------------------------------------------")
92+
93+
94+
# Run the main function
95+
asyncio.run(main())
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 content"""
7+
import asyncio
8+
9+
from kiota_abstractions.request_information import RequestInformation
10+
from kiota_abstractions.method import Method
11+
from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders
12+
13+
from msgraph_core.requests.batch_request_item import BatchRequestItem
14+
from msgraph_core.requests.batch_request_content import BatchRequestContent
15+
from msgraph_core.requests.batch_request_content_collection import BatchRequestContentCollection
16+
17+
from msgraph_core.requests.batch_response_content import BatchResponseContent
18+
from msgraph_core.requests.batch_response_content_collection import BatchResponseContentCollection
19+
20+
# Create a client
21+
# code to create a graph client
22+
graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
23+
24+
# Create a request adapter from the clinet
25+
request_adapter = graph_client.request_adapter
26+
27+
# Create batch Items
28+
request_info1 = RequestInformation()
29+
request_info1.http_method = "GET"
30+
request_info1.url = "https://graph.microsoft.com/v1.0/me"
31+
request_info1.url = "/me"
32+
33+
request_info1.headers = RequestHeaders()
34+
request_info1.headers.add("Content-Type", "application/json")
35+
36+
request_info2 = RequestInformation()
37+
request_info2.http_method = "GET"
38+
request_info2.url = "/users"
39+
request_info2.headers = RequestHeaders()
40+
request_info2.headers.add("Content-Type", "application/json")
41+
42+
batch_request_item1 = BatchRequestItem(request_information=request_info1)
43+
batch_request_item2 = BatchRequestItem(request_information=request_info2)
44+
45+
# Create a batch request content
46+
batch_request_content = {
47+
batch_request_item1.id: batch_request_item1,
48+
batch_request_item2.id: batch_request_item2
49+
}
50+
batch_content = BatchRequestContent(batch_request_content)
51+
52+
53+
async def main():
54+
batch_response_content = await graph_client.batch.post(batch_request_content=batch_content)
55+
56+
# Print the batch response content
57+
print(f"Batch Response Content: {batch_response_content.responses}")
58+
for response in batch_response_content.responses:
59+
print(f"Request ID: {response.id}, Status: {response.status}")
60+
print(f"Response body: {response.body}, headers: {response.headers}")
61+
print("-------------------------------------------------------------")
62+
63+
64+
asyncio.run(main())
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 Custom Error Class"""
7+
8+
import asyncio
9+
10+
from kiota_abstractions.request_adapter import RequestAdapter
11+
from kiota_abstractions.serialization import Parsable
12+
from kiota_abstractions.request_information import RequestInformation
13+
from kiota_abstractions.method import Method
14+
from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders
15+
16+
from msgraph import GraphServiceClient
17+
18+
from msgraph_core.requests.batch_request_item import BatchRequestItem
19+
from msgraph_core.requests.batch_request_content import BatchRequestContent
20+
21+
from msgraph_core.requests.batch_response_content import BatchResponseContent
22+
# create client
23+
# code to create client
24+
graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
25+
26+
27+
# Create an Error map Parsable or import it from wherever you have it
28+
class CustomError(Parsable):
29+
30+
def __init__(self) -> None:
31+
self.error_code: str = None
32+
self.message: str = None
33+
34+
@staticmethod
35+
def not_found() -> 'CustomError':
36+
error = CustomError()
37+
error.error_code = "404"
38+
error.message = "Resource not found"
39+
return error
40+
41+
42+
# Create a request adapter from client
43+
request_adapter = graph_client.request_adapter
44+
45+
# Create batch Items
46+
request_info1 = RequestInformation()
47+
request_info1.http_method = "GET"
48+
request_info1.url = "https://graph.microsoft.com/v1.0/me"
49+
request_info1.url = "/me"
50+
51+
request_info1.headers = RequestHeaders()
52+
request_info1.headers.add("Content-Type", "application/json")
53+
54+
request_info2 = RequestInformation()
55+
request_info2.http_method = "GET"
56+
request_info2.url = "/users"
57+
request_info2.headers = RequestHeaders()
58+
request_info2.headers.add("Content-Type", "application/json")
59+
60+
# get user who does not exist to test 404 in error map
61+
request_info3 = RequestInformation()
62+
request_info3.http_method = "GET"
63+
request_info3.url = "/users/random-id"
64+
request_info3.headers = RequestHeaders()
65+
request_info3.headers.add("Content-Type", "application/json")
66+
67+
# bacth request items to be added to content
68+
batch_request_item1 = BatchRequestItem(request_information=request_info1)
69+
batch_request_item2 = BatchRequestItem(request_information=request_info2)
70+
batch_request_item3 = BatchRequestItem(request_information=request_info3)
71+
72+
# Create a BatchRequestContent
73+
batch_request_content = {
74+
batch_request_item1.id: batch_request_item1,
75+
batch_request_item2.id: batch_request_item2
76+
}
77+
78+
batch_content = BatchRequestContent(batch_request_content)
79+
80+
81+
# Function to demonstrate the usage of BatchRequestBuilder
82+
async def main():
83+
error_map = {"400": CustomError, "404": CustomError.not_found}
84+
85+
batch_response_content = await graph_client.batch.post(
86+
batch_request_content=batch_content, error_map=error_map
87+
)
88+
89+
# Print the batch response content
90+
print(f"Batch Response Content: {batch_response_content.responses}")
91+
for response in batch_response_content.responses:
92+
print(f"Request ID: {response.id}, Status: {response.status}")
93+
print(f"Response body: {response.body}, headers: {response.headers}")
94+
print("-------------------------------------------------------------")
95+
96+
97+
asyncio.run(main())
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
# import User model to serialize to
18+
from msgraph.generated.models.user import User
19+
# Create a client
20+
# code to create graph client
21+
graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
22+
23+
print(f"Graph Scopes: {graph_scopes}")
24+
25+
# Create a request adapter from the client
26+
request_adapter = graph_client.request_adapter
27+
28+
# Create batch Items
29+
request_info1 = RequestInformation()
30+
request_info1.http_method = "GET"
31+
request_info1.url = "/users/<user-id-1>"
32+
33+
request_info1.headers = RequestHeaders()
34+
request_info1.headers.add("Content-Type", "application/json")
35+
36+
request_info2 = RequestInformation()
37+
request_info2.http_method = "GET"
38+
request_info2.url = "/users/<user-id-2>"
39+
request_info2.headers = RequestHeaders()
40+
request_info2.headers.add("Content-Type", "application/json")
41+
42+
# bacth request items to be added to content
43+
batch_request_item1 = BatchRequestItem(request_information=request_info1)
44+
batch_request_item2 = BatchRequestItem(request_information=request_info2)
45+
46+
# Create a batch request content
47+
batch_request_content = {
48+
batch_request_item1.id: batch_request_item1,
49+
batch_request_item2.id: batch_request_item2
50+
}
51+
52+
batch_content = BatchRequestContent(batch_request_content)
53+
54+
55+
# Function to demonstrate the usage of BatchRequestBuilder
56+
async def main():
57+
batch_response_content = await graph_client.batch.post(batch_request_content=batch_content)
58+
# response_type=User
59+
60+
try:
61+
individual_response = batch_response_content.get_response_by_id(
62+
batch_request_item1.id, User
63+
)
64+
print(f"Individual Response: {individual_response}")
65+
except AttributeError as e:
66+
print(f"Error getting response by ID: {e}")
67+
68+
69+
asyncio.run(main())
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
"""Demonstrates getting status codes in Batch Responses"""
7+
batch_request_content = {
8+
batch_request_item1.id: batch_request_item1,
9+
batch_request_item2.id: batch_request_item2
10+
}
11+
12+
batch_content = BatchRequestContent(batch_request_content)
13+
14+
15+
async def main():
16+
batch_response_content = await client.batch.post(batch_request_content=batch_content)
17+
18+
try:
19+
status_codes = batch_response_content.get_response_status_codes()
20+
print(f"Status Codes: {status_codes}")
21+
except AttributeError as e:
22+
print(f"Error getting respons status codes: {e}")
23+
24+
25+
asyncio.run(main())

0 commit comments

Comments
 (0)