Skip to content

Commit 9fa9444

Browse files
committed
add samples for using custom error class on batch requests
1 parent 17bc625 commit 9fa9444

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

samples/batch_requests/batch_request_with_content.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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"""
17
import asyncio
28

39
from kiota_abstractions.request_information import RequestInformation
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+
from msgraph_core.requests.batch_request_builder import BatchRequestBuilder
23+
# create client
24+
# code to create client
25+
user_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
26+
27+
28+
# Create an Error map Parsable or import it from wherever you have it
29+
class CustomError(Parsable):
30+
31+
def __init__(self) -> None:
32+
self.error_code: str = None
33+
self.message: str = None
34+
35+
@staticmethod
36+
def not_found() -> 'CustomError':
37+
error = CustomError()
38+
error.error_code = "404"
39+
error.message = "Resource not found"
40+
return error
41+
42+
43+
# Create a request adapter from client
44+
request_adapter = user_client.request_adapter
45+
46+
# Create an instance of BatchRequestBuilder
47+
batch_request_builder = BatchRequestBuilder(request_adapter)
48+
49+
# Create batch Items
50+
request_info1 = RequestInformation()
51+
request_info1.http_method = "GET"
52+
request_info1.url = "https://graph.microsoft.com/v1.0/me"
53+
request_info1.url = "/me"
54+
55+
request_info1.headers = RequestHeaders()
56+
request_info1.headers.add("Content-Type", "application/json")
57+
58+
request_info2 = RequestInformation()
59+
request_info2.http_method = "GET"
60+
request_info2.url = "/users"
61+
request_info2.headers = RequestHeaders()
62+
request_info2.headers.add("Content-Type", "application/json")
63+
64+
# get user who does not exist to test 404 in error map
65+
request_info3 = RequestInformation()
66+
request_info3.http_method = "GET"
67+
request_info3.url = "/users/random-id"
68+
request_info3.headers = RequestHeaders()
69+
request_info3.headers.add("Content-Type", "application/json")
70+
71+
# bacth request items to be added to content
72+
batch_request_item1 = BatchRequestItem(request_information=request_info1)
73+
batch_request_item2 = BatchRequestItem(request_information=request_info2)
74+
batch_request_item3 = BatchRequestItem(request_information=request_info3)
75+
76+
# Create a BatchRequestContent
77+
batch_request_content = [batch_request_item1, batch_request_item2, batch_request_item3]
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 batch_request_builder.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())

0 commit comments

Comments
 (0)