Skip to content

Commit e62565d

Browse files
committed
clean up code after list to dict conversion
1 parent e1f610d commit e62565d

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/msgraph_core/requests/batch_request_builder.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def __init__(
3838
async def post(
3939
self,
4040
batch_request_content: Union[BatchRequestContent, BatchRequestContentCollection],
41-
response_type: Optional[Type[T]] = None,
4241
error_map: Optional[Dict[str, Type[Parsable]]] = None,
4342
) -> Union[T, BatchResponseContentCollection]:
4443
"""
@@ -58,20 +57,21 @@ async def post(
5857
"""
5958
if batch_request_content is None:
6059
raise ValueError("batch_request_content cannot be Null.")
61-
if response_type is None:
62-
response_type = BatchResponseContent
60+
response_type = BatchResponseContent
6361

6462
if isinstance(batch_request_content, BatchRequestContent):
6563
request_info = await self.to_post_request_information(batch_request_content)
66-
# content = json.loads(request_info.content.decode("utf-8"))
67-
# json_body = json.dumps(content)
68-
# request_info.content = json_body
6964
error_map = error_map or self.error_map
7065
response = None
7166
try:
7267
response = await self._request_adapter.send_async(
7368
request_info, response_type, error_map
7469
)
70+
print(f"Response type returned for content : {type(response)}")
71+
print(f"Batch response responses returned for content : {response.responses}")
72+
for key in response.responses:
73+
print(f"Response key -id: {key}")
74+
print(f"Response value: {response.response(key).body}")
7575
except APIError as e:
7676
logging.error("API Error: %s", e)
7777
raise e
@@ -80,6 +80,7 @@ async def post(
8080
return response
8181
if isinstance(batch_request_content, BatchRequestContentCollection):
8282
batch_responses = await self._post_batch_collection(batch_request_content, error_map)
83+
print(f"Response type returned for collection: {type(response)}")
8384
return batch_responses
8485

8586
raise ValueError("Invalid type for batch_request_content.")
@@ -111,6 +112,7 @@ async def _post_batch_collection(
111112
response = await self._request_adapter.send_async(
112113
request_info, BatchResponseContent, error_map or self.error_map
113114
)
115+
print(f"Response type for batch item: {type(response)}")
114116
batch_responses.add_response(response)
115117

116118
return batch_responses

src/msgraph_core/requests/batch_request_content.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class BatchRequestContent(Parsable):
1515

1616
MAX_REQUESTS = 20
1717

18-
def __init__(self, requests: List[Union['BatchRequestItem', 'RequestInformation']] = []):
18+
def __init__(self, requests: Dict[str, Union['BatchRequestItem', 'RequestInformation']] = {}):
1919
"""
2020
Initializes a new instance of the BatchRequestContent class.
2121
"""
2222
self._requests: Dict[str, Union[BatchRequestItem, 'RequestInformation']] = {}
2323

2424
self.is_finalized = False
25-
for request in requests:
26-
self.add_request(request)
25+
for request_id, request in requests.items():
26+
self.add_request(request_id, request)
2727

2828
@property
2929
def requests(self) -> List:
@@ -42,10 +42,13 @@ def requests(self, requests: List[BatchRequestItem]) -> None:
4242
for request in requests:
4343
self.add_request(request)
4444

45-
def add_request(self, request: BatchRequestItem) -> None:
45+
def add_request(self, request_id: Optional[str], request: BatchRequestItem) -> None:
4646
"""
4747
Adds a request to the batch request content.
4848
"""
49+
print(f"Request: {request}")
50+
print(f"Request type: {type(request)}")
51+
4952
if len(self.requests) >= BatchRequestContent.MAX_REQUESTS:
5053
raise RuntimeError(f"Maximum number of requests is {BatchRequestContent.MAX_REQUESTS}")
5154
if not request.id:

src/msgraph_core/requests/batch_response_content.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,24 @@ def responses(self, responses: Optional[Dict[str, 'BatchResponseItem']]) -> None
4040
"""
4141
self._responses = responses
4242

43-
def response(self, request_id: str) -> 'BatchResponseItem':
43+
def response(
44+
self,
45+
request_id: str,
46+
response_type: Optional[Type[T]] = None,
47+
) -> 'BatchResponseItem':
4448
"""
4549
Get a response by its request ID from the collection
4650
:param request_id: The request ID of the response to get
4751
:type request_id: str
4852
:return: The response with the specified request ID as a BatchResponseItem
4953
:rtype: BatchResponseItem
5054
"""
51-
if self._responses is None:
52-
raise ValueError("Responses list is not initialized.")
53-
if request_id in self._responses:
54-
return self._responses[request_id]
55-
raise KeyError(f"Response with request ID {request_id} not found.")
55+
return self._responses.get(request_id)
56+
# if self._responses is None:
57+
# raise ValueError("Responses list is not initialized.")
58+
# if request_id in self._responses:
59+
# return self._responses[request_id]
60+
# raise KeyError(f"Response with request ID {request_id} not found.")
5661

5762
def response_body(self, request_id: str, type: Type[T]) -> Optional[T]:
5863
"""
@@ -115,7 +120,7 @@ def serialize(self, writer: SerializationWriter) -> None:
115120
:param writer: The writer to write to
116121
"""
117122
if self._responses:
118-
writer.write_collection_of_object_values('responses', list(self._responses.values()))
123+
writer.write_collection_of_object_values('responses', self.responses())
119124

120125
@staticmethod
121126
def create_from_discriminator_value(

0 commit comments

Comments
 (0)