Skip to content

Commit ac6b985

Browse files
shemogumbebaywet
authored andcommitted
Update samples for post requests
1 parent 97e6d58 commit ac6b985

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
""" Demonstrate doing a batch request with a dependency on another request """
7+
8+
graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes)
9+
# Use the request builder to generate a regular
10+
# request to /me
11+
user_request = graph_client.me.to_get_request_information()
12+
13+
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
14+
tomorrow = today + timedelta(days=1)
15+
16+
new_event = Event(
17+
subject="File end-of-day report",
18+
start=DateTimeTimeZone(
19+
date_time=(today + timedelta(hours=17)).isoformat(timespec='seconds'),
20+
time_zone='Pacific Standard Time'
21+
),
22+
end=DateTimeTimeZone(
23+
date_time=(today + timedelta(hours=17, minutes=30)).isoformat(timespec='seconds'),
24+
time_zone='Pacific Standard Time'
25+
)
26+
)
27+
28+
# Use the request builder to generate a regular
29+
add_event_request = graph_client.me.events.to_post_request_information(new_event)
30+
31+
# Use the request builder to generate a regular
32+
# request to /me/calendarview?startDateTime="start"&endDateTime="end"
33+
query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters(
34+
start_date_time=today.isoformat(timespec='seconds'),
35+
end_date_time=tomorrow.isoformat(timespec='seconds')
36+
)
37+
38+
config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration(
39+
query_parameters=query_params
40+
)
41+
events_request = graph_client.me.calendar_view.to_get_request_information(config)
42+
43+
# Build the batch
44+
add_event_batch_item = BatchRequestItem(request_information=add_event_request)
45+
add_event_batch_item.body = {
46+
"@odata.type": "#microsoft.graph.event",
47+
"end": {
48+
"dateTime": "2024-10-14T17:30:00",
49+
"timeZone": "Pacific Standard Time"
50+
},
51+
"start": {
52+
"dateTime": "2024-10-14T17:00:00",
53+
"timeZone": "Pacific Standard Time"
54+
},
55+
"subject": "File end-of-day report"
56+
}
57+
add_event_batch_item.body = json.dumps(add_event_batch_item.body)
58+
59+
print(f"Event to be added {type(add_event_batch_item.body)}")
60+
61+
events_batch_item = BatchRequestItem(request_information=events_request)
62+
63+
update_profile_pic_request = RequestInformation()
64+
update_profile_pic_request.http_method = "PUT"
65+
update_profile_pic_request.url = "/me/photo/$value"
66+
update_profile_pic_request.headers = RequestHeaders()
67+
update_profile_pic_request.headers.add("Content-Type", "image/jpeg")
68+
current_directory = os.path.dirname(os.path.abspath(__file__))
69+
image_file_path = os.path.join(current_directory, "app_headshot.jpeg")
70+
71+
with open(image_file_path, 'rb') as image_file:
72+
# base64_image = base64.b64encode(image_file.read()).decode('utf-8')
73+
base64_image = base64.b64encode(image_file.read()).decode('ascii')
74+
75+
update_profile_pic_request.content = base64_image
76+
# output_file_path = os.path.join(current_directory, "app_image_bytes.txt")
77+
78+
print(f"Image content {type(update_profile_pic_request.content)}")
79+
# body has a bytes array
80+
update_profile_photo_batch_item = BatchRequestItem(request_information=update_profile_pic_request)
81+
print(f"batch with image {type(update_profile_photo_batch_item.body)}")
82+
83+
# Build the batch collection
84+
batch_request_content_collection = BatchRequestContentCollection()
85+
batch_request_content_collection.add_batch_request_item(update_profile_photo_batch_item)
86+
87+
88+
async def get_batch_response():
89+
batch_response = await graph_client.batch.post(
90+
batch_request_content=batch_request_content_collection
91+
)
92+
for response_content in batch_response.get_responses():
93+
for request_id, response in response_content.responses.items():
94+
print(f"Batch response content collection {response.status}")
95+
print(f"Batch response content collection {response.body}")
96+
97+
98+
asyncio.run(get_batch_response())
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
""" Demonstrate doing a batch request with binary content """
7+
user_request = graph_client.me.to_get_request_information()
8+
9+
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
10+
tomorrow = today + timedelta(days=1)
11+
12+
new_event = Event(
13+
subject="File end-of-day report",
14+
start=DateTimeTimeZone(
15+
date_time=(today + timedelta(hours=17)).isoformat(timespec='seconds'),
16+
time_zone='Pacific Standard Time'
17+
),
18+
end=DateTimeTimeZone(
19+
date_time=(today + timedelta(hours=17, minutes=30)).isoformat(timespec='seconds'),
20+
time_zone='Pacific Standard Time'
21+
)
22+
)
23+
24+
# Use the request builder to generate a regular
25+
add_event_request = graph_client.me.events.to_post_request_information(new_event)
26+
27+
# set query parameters for the calendar view
28+
query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters(
29+
start_date_time=today.isoformat(timespec='seconds'),
30+
end_date_time=tomorrow.isoformat(timespec='seconds')
31+
)
32+
33+
config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration(
34+
query_parameters=query_params
35+
)
36+
events_request = graph_client.me.calendar_view.to_get_request_information(config)
37+
38+
# Build the batch
39+
add_event_batch_item = BatchRequestItem(request_information=add_event_request)
40+
add_event_batch_item.body = {
41+
"@odata.type": "#microsoft.graph.event",
42+
"end": {
43+
"dateTime": "2024-10-14T17:30:00",
44+
"timeZone": "Pacific Standard Time"
45+
},
46+
"start": {
47+
"dateTime": "2024-10-14T17:00:00",
48+
"timeZone": "Pacific Standard Time"
49+
},
50+
"subject": "File end-of-day report"
51+
}
52+
add_event_batch_item.body = json.dumps(add_event_batch_item.body)
53+
54+
print(f"Event to be added {type(add_event_batch_item.body)}")
55+
56+
events_batch_item = BatchRequestItem(
57+
request_information=events_request, depends_on=[add_event_batch_item]
58+
)
59+
60+
# Build the batch collection
61+
batch_request_content_collection = BatchRequestContentCollection()
62+
batch_request_content_collection.add_batch_request_item(add_event_batch_item)
63+
batch_request_content_collection.add_batch_request_item(events_batch_item)
64+
65+
66+
async def get_batch_response():
67+
batch_response = await graph_client.batch.post(
68+
batch_request_content=batch_request_content_collection
69+
)
70+
for response_content in batch_response.get_responses():
71+
for request_id, response in response_content.responses.items():
72+
print(f"Batch response content collection {response.status}")
73+
print(f"Batch response content collection {response.body}")
74+
75+
76+
asyncio.run(get_batch_response())

0 commit comments

Comments
 (0)