Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples for post requests #708

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions samples/batch_requests/batch_body_with_bytes_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
#pylint: disable=undefined-variable
""" Demonstrate doing a batch request with a dependency on another 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, "my_cool_pic.jpeg")

with open(image_file_path, 'rb') as image_file:
base64_image = base64.b64encode(image_file.read()).decode('ascii')

update_profile_pic_request.content = base64_image

# 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())
76 changes: 76 additions & 0 deletions samples/batch_requests/batch_request_with_depends_on.py
Original file line number Diff line number Diff line change
@@ -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())
Loading