From 5b7a40bf72cb31e0d0e3a3e797f449def2d9b36b Mon Sep 17 00:00:00 2001 From: George Garber Date: Tue, 11 Mar 2025 10:49:55 +0000 Subject: [PATCH 1/4] fix batch creation --- .../batch_request_content_collection.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/msgraph_core/requests/batch_request_content_collection.py b/src/msgraph_core/requests/batch_request_content_collection.py index 3f5cb511..0d0632b3 100644 --- a/src/msgraph_core/requests/batch_request_content_collection.py +++ b/src/msgraph_core/requests/batch_request_content_collection.py @@ -16,8 +16,8 @@ def __init__(self) -> None: """ self.max_requests_per_batch = BatchRequestContent.MAX_REQUESTS - self.batches: list[BatchRequestContent] = [] self.current_batch: BatchRequestContent = BatchRequestContent() + self.batches: list[BatchRequestContent] = [self.current_batch] def add_batch_request_item(self, request: BatchRequestItem) -> None: """ @@ -28,8 +28,8 @@ def add_batch_request_item(self, request: BatchRequestItem) -> None: if len(self.current_batch.requests) >= self.max_requests_per_batch: self.batches.append(self.current_batch.finalize()) self.current_batch = BatchRequestContent() + self.batches.append(self.current_batch) self.current_batch.add_request(request.id, request) - self.batches.append(self.current_batch) def remove_batch_request_item(self, request_id: str) -> None: """ @@ -49,16 +49,30 @@ def new_batch_with_failed_requests(self) -> Optional[BatchRequestContent]: Optional[BatchRequestContent]: A new batch with failed requests. """ # Use IDs to get response status codes, generate new batch with failed requests - batch_with_failed_responses: Optional[BatchRequestContent] = BatchRequestContent() + batch_with_failed_responses: Optional[BatchRequestContent] = ( + BatchRequestContent() + ) for batch in self.batches: for request in batch.requests: - if request.status_code not in [ # type: ignore # Method should be deprecated - 200, 201, 202, 203, 204, 205, 206, 207, 208, 226 - ]: + if ( + request.status_code + not in [ # type: ignore # Method should be deprecated + 200, + 201, + 202, + 203, + 204, + 205, + 206, + 207, + 208, + 226, + ] + ): if batch_with_failed_responses is not None: batch_with_failed_responses.add_request( request.id, # type: ignore # Bug. Method should be deprecated - request # type: ignore + request, # type: ignore ) else: raise ValueError("batch_with_failed_responses is None") From e04db72fd1b39280e683738d14b5fbd755c408c0 Mon Sep 17 00:00:00 2001 From: George Garber Date: Tue, 11 Mar 2025 10:53:04 +0000 Subject: [PATCH 2/4] fix batch creation --- .../batch_request_content_collection.py | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/msgraph_core/requests/batch_request_content_collection.py b/src/msgraph_core/requests/batch_request_content_collection.py index 0d0632b3..bfb5c12b 100644 --- a/src/msgraph_core/requests/batch_request_content_collection.py +++ b/src/msgraph_core/requests/batch_request_content_collection.py @@ -49,30 +49,16 @@ def new_batch_with_failed_requests(self) -> Optional[BatchRequestContent]: Optional[BatchRequestContent]: A new batch with failed requests. """ # Use IDs to get response status codes, generate new batch with failed requests - batch_with_failed_responses: Optional[BatchRequestContent] = ( - BatchRequestContent() - ) + batch_with_failed_responses: Optional[BatchRequestContent] = BatchRequestContent() for batch in self.batches: for request in batch.requests: - if ( - request.status_code - not in [ # type: ignore # Method should be deprecated - 200, - 201, - 202, - 203, - 204, - 205, - 206, - 207, - 208, - 226, - ] - ): + if request.status_code not in [ # type: ignore # Method should be deprecated + 200, 201, 202, 203, 204, 205, 206, 207, 208, 226 + ]: if batch_with_failed_responses is not None: batch_with_failed_responses.add_request( request.id, # type: ignore # Bug. Method should be deprecated - request, # type: ignore + request # type: ignore ) else: raise ValueError("batch_with_failed_responses is None") From d83b294fcca3e4eea0ca5e84f03be6a8c70344c6 Mon Sep 17 00:00:00 2001 From: George Garber Date: Tue, 11 Mar 2025 13:18:23 +0000 Subject: [PATCH 3/4] fix --- src/msgraph_core/requests/batch_request_content_collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/msgraph_core/requests/batch_request_content_collection.py b/src/msgraph_core/requests/batch_request_content_collection.py index bfb5c12b..ddf0acb8 100644 --- a/src/msgraph_core/requests/batch_request_content_collection.py +++ b/src/msgraph_core/requests/batch_request_content_collection.py @@ -26,7 +26,7 @@ def add_batch_request_item(self, request: BatchRequestItem) -> None: request (BatchRequestItem): The request item to add. """ if len(self.current_batch.requests) >= self.max_requests_per_batch: - self.batches.append(self.current_batch.finalize()) + self.current_batch.finalize() self.current_batch = BatchRequestContent() self.batches.append(self.current_batch) self.current_batch.add_request(request.id, request) From 42294b11cc6e59a7f7cf1f1abe681fafaa6ce08b Mon Sep 17 00:00:00 2001 From: George Garber Date: Sat, 22 Mar 2025 16:38:42 +0000 Subject: [PATCH 4/4] tests --- .../test_batch_request_content_collection.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/requests/test_batch_request_content_collection.py diff --git a/tests/requests/test_batch_request_content_collection.py b/tests/requests/test_batch_request_content_collection.py new file mode 100644 index 00000000..da250164 --- /dev/null +++ b/tests/requests/test_batch_request_content_collection.py @@ -0,0 +1,44 @@ +import pytest +from io import BytesIO +from kiota_abstractions.request_information import RequestInformation +from msgraph_core.requests.batch_request_item import BatchRequestItem +from msgraph_core.requests.batch_request_content_collection import BatchRequestContentCollection +from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders + + +@pytest.fixture +def batch_request_content_collection(): + return BatchRequestContentCollection() + + +@pytest.fixture +def request_info(): + request_info = RequestInformation() + request_info.http_method = "GET" + request_info.url = "https://graph.microsoft.com/v1.0/me" + request_info.headers = RequestHeaders() + request_info.headers.add("Content-Type", "application/json") + request_info.content = BytesIO(b'{"key": "value"}') + return request_info + + +@pytest.fixture +def batch_request_item1(request_info): + return BatchRequestItem(request_information=request_info, id="1") + + +@pytest.fixture +def batch_request_item2(request_info): + return BatchRequestItem(request_information=request_info, id="2") + + +def test_init_batches(batch_request_content_collection): + assert len(batch_request_content_collection.batches) == 1 + assert batch_request_content_collection.current_batch is not None + + +def test_add_batch_request_item(batch_request_content_collection, batch_request_item1, batch_request_item2): + batch_request_content_collection.add_batch_request_item(batch_request_item1) + batch_request_content_collection.add_batch_request_item(batch_request_item2) + assert len(batch_request_content_collection.batches) == 1 + assert len(batch_request_content_collection.current_batch.requests) == 2