|
| 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()) |
0 commit comments