From be9a881613087b68aeb1fcecf50f0e4a5e9b3149 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Thu, 17 Oct 2024 16:00:45 +0300 Subject: [PATCH 1/2] Update samples for post requests --- .../batch_body_with_bytes_content.py | 98 +++++++++++++++++++ .../batch_request_with_depends_on.py | 76 ++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 samples/batch_requests/batch_body_with_bytes_content.py create mode 100644 samples/batch_requests/batch_request_with_depends_on.py diff --git a/samples/batch_requests/batch_body_with_bytes_content.py b/samples/batch_requests/batch_body_with_bytes_content.py new file mode 100644 index 00000000..ae72fec7 --- /dev/null +++ b/samples/batch_requests/batch_body_with_bytes_content.py @@ -0,0 +1,98 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ +#pylint: disable=undefined-variable +""" Demonstrate doing a batch request with a dependency on another request """ + +graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes) +# Use the request builder to generate a regular +# request to /me +user_request = graph_client.me.to_get_request_information() + +today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) +tomorrow = today + timedelta(days=1) + +new_event = Event( + subject="File end-of-day report", + start=DateTimeTimeZone( + date_time=(today + timedelta(hours=17)).isoformat(timespec='seconds'), + time_zone='Pacific Standard Time' + ), + end=DateTimeTimeZone( + date_time=(today + timedelta(hours=17, minutes=30)).isoformat(timespec='seconds'), + time_zone='Pacific Standard Time' + ) +) + +# Use the request builder to generate a regular +add_event_request = graph_client.me.events.to_post_request_information(new_event) + +# Use the request builder to generate a regular +# request to /me/calendarview?startDateTime="start"&endDateTime="end" +query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters( + start_date_time=today.isoformat(timespec='seconds'), + end_date_time=tomorrow.isoformat(timespec='seconds') +) + +config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration( + query_parameters=query_params +) +events_request = graph_client.me.calendar_view.to_get_request_information(config) + +# Build the batch +add_event_batch_item = BatchRequestItem(request_information=add_event_request) +add_event_batch_item.body = { + "@odata.type": "#microsoft.graph.event", + "end": { + "dateTime": "2024-10-14T17:30:00", + "timeZone": "Pacific Standard Time" + }, + "start": { + "dateTime": "2024-10-14T17:00:00", + "timeZone": "Pacific Standard Time" + }, + "subject": "File end-of-day report" +} +add_event_batch_item.body = json.dumps(add_event_batch_item.body) + +print(f"Event to be added {type(add_event_batch_item.body)}") + +events_batch_item = BatchRequestItem(request_information=events_request) + +update_profile_pic_request = RequestInformation() +update_profile_pic_request.http_method = "PUT" +update_profile_pic_request.url = "/me/photo/$value" +update_profile_pic_request.headers = RequestHeaders() +update_profile_pic_request.headers.add("Content-Type", "image/jpeg") +current_directory = os.path.dirname(os.path.abspath(__file__)) +image_file_path = os.path.join(current_directory, "app_headshot.jpeg") + +with open(image_file_path, 'rb') as image_file: + # base64_image = base64.b64encode(image_file.read()).decode('utf-8') + base64_image = base64.b64encode(image_file.read()).decode('ascii') + +update_profile_pic_request.content = base64_image +# output_file_path = os.path.join(current_directory, "app_image_bytes.txt") + +print(f"Image content {type(update_profile_pic_request.content)}") +# body has a bytes array +update_profile_photo_batch_item = BatchRequestItem(request_information=update_profile_pic_request) +print(f"batch with image {type(update_profile_photo_batch_item.body)}") + +# Build the batch collection +batch_request_content_collection = BatchRequestContentCollection() +batch_request_content_collection.add_batch_request_item(update_profile_photo_batch_item) + + +async def get_batch_response(): + batch_response = await graph_client.batch.post( + batch_request_content=batch_request_content_collection + ) + for response_content in batch_response.get_responses(): + for request_id, response in response_content.responses.items(): + print(f"Batch response content collection {response.status}") + print(f"Batch response content collection {response.body}") + + +asyncio.run(get_batch_response()) diff --git a/samples/batch_requests/batch_request_with_depends_on.py b/samples/batch_requests/batch_request_with_depends_on.py new file mode 100644 index 00000000..1467f381 --- /dev/null +++ b/samples/batch_requests/batch_request_with_depends_on.py @@ -0,0 +1,76 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ +#pylint: disable=undefined-variable +""" Demonstrate doing a batch request with binary content """ +user_request = graph_client.me.to_get_request_information() + +today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) +tomorrow = today + timedelta(days=1) + +new_event = Event( + subject="File end-of-day report", + start=DateTimeTimeZone( + date_time=(today + timedelta(hours=17)).isoformat(timespec='seconds'), + time_zone='Pacific Standard Time' + ), + end=DateTimeTimeZone( + date_time=(today + timedelta(hours=17, minutes=30)).isoformat(timespec='seconds'), + time_zone='Pacific Standard Time' + ) +) + +# Use the request builder to generate a regular +add_event_request = graph_client.me.events.to_post_request_information(new_event) + +# set query parameters for the calendar view +query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters( + start_date_time=today.isoformat(timespec='seconds'), + end_date_time=tomorrow.isoformat(timespec='seconds') +) + +config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration( + query_parameters=query_params +) +events_request = graph_client.me.calendar_view.to_get_request_information(config) + +# Build the batch +add_event_batch_item = BatchRequestItem(request_information=add_event_request) +add_event_batch_item.body = { + "@odata.type": "#microsoft.graph.event", + "end": { + "dateTime": "2024-10-14T17:30:00", + "timeZone": "Pacific Standard Time" + }, + "start": { + "dateTime": "2024-10-14T17:00:00", + "timeZone": "Pacific Standard Time" + }, + "subject": "File end-of-day report" +} +add_event_batch_item.body = json.dumps(add_event_batch_item.body) + +print(f"Event to be added {type(add_event_batch_item.body)}") + +events_batch_item = BatchRequestItem( + request_information=events_request, depends_on=[add_event_batch_item] +) + +# Build the batch collection +batch_request_content_collection = BatchRequestContentCollection() +batch_request_content_collection.add_batch_request_item(add_event_batch_item) +batch_request_content_collection.add_batch_request_item(events_batch_item) + + +async def get_batch_response(): + batch_response = await graph_client.batch.post( + batch_request_content=batch_request_content_collection + ) + for response_content in batch_response.get_responses(): + for request_id, response in response_content.responses.items(): + print(f"Batch response content collection {response.status}") + print(f"Batch response content collection {response.body}") + + +asyncio.run(get_batch_response()) From 1e852a1cd13155f03e1cb7baab2a5e1307e87109 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Thu, 17 Oct 2024 16:17:01 +0300 Subject: [PATCH 2/2] update samples --- .../batch_body_with_bytes_content.py | 60 +------------------ 1 file changed, 1 insertion(+), 59 deletions(-) diff --git a/samples/batch_requests/batch_body_with_bytes_content.py b/samples/batch_requests/batch_body_with_bytes_content.py index ae72fec7..6f094e7d 100644 --- a/samples/batch_requests/batch_body_with_bytes_content.py +++ b/samples/batch_requests/batch_body_with_bytes_content.py @@ -5,77 +5,19 @@ #pylint: disable=undefined-variable """ Demonstrate doing a batch request with a dependency on another request """ -graph_client = GraphServiceClient(credentials=token, scopes=graph_scopes) -# Use the request builder to generate a regular -# request to /me -user_request = graph_client.me.to_get_request_information() - -today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) -tomorrow = today + timedelta(days=1) - -new_event = Event( - subject="File end-of-day report", - start=DateTimeTimeZone( - date_time=(today + timedelta(hours=17)).isoformat(timespec='seconds'), - time_zone='Pacific Standard Time' - ), - end=DateTimeTimeZone( - date_time=(today + timedelta(hours=17, minutes=30)).isoformat(timespec='seconds'), - time_zone='Pacific Standard Time' - ) -) - -# Use the request builder to generate a regular -add_event_request = graph_client.me.events.to_post_request_information(new_event) - -# Use the request builder to generate a regular -# request to /me/calendarview?startDateTime="start"&endDateTime="end" -query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters( - start_date_time=today.isoformat(timespec='seconds'), - end_date_time=tomorrow.isoformat(timespec='seconds') -) - -config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration( - query_parameters=query_params -) -events_request = graph_client.me.calendar_view.to_get_request_information(config) - -# Build the batch -add_event_batch_item = BatchRequestItem(request_information=add_event_request) -add_event_batch_item.body = { - "@odata.type": "#microsoft.graph.event", - "end": { - "dateTime": "2024-10-14T17:30:00", - "timeZone": "Pacific Standard Time" - }, - "start": { - "dateTime": "2024-10-14T17:00:00", - "timeZone": "Pacific Standard Time" - }, - "subject": "File end-of-day report" -} -add_event_batch_item.body = json.dumps(add_event_batch_item.body) - -print(f"Event to be added {type(add_event_batch_item.body)}") - -events_batch_item = BatchRequestItem(request_information=events_request) - update_profile_pic_request = RequestInformation() update_profile_pic_request.http_method = "PUT" update_profile_pic_request.url = "/me/photo/$value" update_profile_pic_request.headers = RequestHeaders() update_profile_pic_request.headers.add("Content-Type", "image/jpeg") current_directory = os.path.dirname(os.path.abspath(__file__)) -image_file_path = os.path.join(current_directory, "app_headshot.jpeg") +image_file_path = os.path.join(current_directory, "my_cool_pic.jpeg") with open(image_file_path, 'rb') as image_file: - # base64_image = base64.b64encode(image_file.read()).decode('utf-8') base64_image = base64.b64encode(image_file.read()).decode('ascii') update_profile_pic_request.content = base64_image -# output_file_path = os.path.join(current_directory, "app_image_bytes.txt") -print(f"Image content {type(update_profile_pic_request.content)}") # body has a bytes array update_profile_photo_batch_item = BatchRequestItem(request_information=update_profile_pic_request) print(f"batch with image {type(update_profile_photo_batch_item.body)}")