Skip to content

Commit 3c8dc8f

Browse files
committed
fix batch request builder for updated requests in dictionaries
1 parent e62565d commit 3c8dc8f

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

src/msgraph_core/requests/batch_request_builder.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ async def post(
6161

6262
if isinstance(batch_request_content, BatchRequestContent):
6363
request_info = await self.to_post_request_information(batch_request_content)
64+
request_info.content = request_info.content.strip(b'[]')
65+
print(f"Request info: {request_info.content}")
6466
error_map = error_map or self.error_map
6567
response = None
6668
try:
@@ -69,9 +71,9 @@ async def post(
6971
)
7072
print(f"Response type returned for content : {type(response)}")
7173
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}")
74+
# for key in response.responses:
75+
# print(f"Response key -id: {key}")
76+
# print(f"Response value: {response.response(key).body}")
7577
except APIError as e:
7678
logging.error("API Error: %s", e)
7779
raise e
@@ -129,16 +131,18 @@ async def to_post_request_information(
129131
Returns:
130132
RequestInformation: The request information.
131133
"""
134+
132135
if batch_request_content is None:
133136
raise ValueError("batch_request_content cannot be Null.")
134-
if isinstance(batch_request_content, BatchRequestContent):
135-
request_info = RequestInformation()
136-
request_info.http_method = Method.POST
137-
request_info.url_template = self.url_template
138-
request_info.headers = HeadersCollection()
139-
request_info.headers.try_add("Content-Type", APPLICATION_JSON)
140-
request_info.set_content_from_parsable(
141-
self._request_adapter, APPLICATION_JSON, batch_request_content
142-
)
143-
144-
return request_info
137+
batch_request_items = list(batch_request_content.requests.values())
138+
139+
request_info = RequestInformation()
140+
request_info.http_method = Method.POST
141+
request_info.url_template = self.url_template
142+
request_info.headers = HeadersCollection()
143+
request_info.headers.try_add("Content-Type", APPLICATION_JSON)
144+
request_info.set_content_from_parsable(
145+
self._request_adapter, APPLICATION_JSON, batch_request_items
146+
)
147+
148+
return request_info

src/msgraph_core/requests/batch_request_content.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ def __init__(self, requests: Dict[str, Union['BatchRequestItem', 'RequestInforma
1919
"""
2020
Initializes a new instance of the BatchRequestContent class.
2121
"""
22-
self._requests: Dict[str, Union[BatchRequestItem, 'RequestInformation']] = {}
22+
self._requests: Dict[str, Union[BatchRequestItem, 'RequestInformation']] = requests or {}
2323

2424
self.is_finalized = False
2525
for request_id, request in requests.items():
2626
self.add_request(request_id, request)
2727

2828
@property
29-
def requests(self) -> List:
29+
def requests(self) -> Dict:
3030
"""
3131
Gets the requests.
3232
"""
33-
return list(self._requests.values())
33+
return self._requests
3434

3535
@requests.setter
3636
def requests(self, requests: List[BatchRequestItem]) -> None:
@@ -104,7 +104,7 @@ def finalize(self):
104104
Finalizes the batch request content.
105105
"""
106106
self.is_finalized = True
107-
return list(self._requests.values())
107+
return self._requests
108108

109109
def _request_by_id(self, request_id: str) -> Optional[BatchRequestItem]:
110110
"""

src/msgraph_core/requests/batch_response_content.py

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

43+
def get_response_by_id(
44+
self,
45+
request_id: str,
46+
response_type: Optional[Type[T]] = None,
47+
) -> 'BatchResponseItem':
48+
"""
49+
Get a response by its request ID from the collection
50+
:param request_id: The request ID of the response to get
51+
:type request_id: str
52+
:return: The response with the specified request ID as a BatchResponseItem
53+
:rtype: BatchResponseItem
54+
"""
55+
return self._responses.get(request_id)
56+
4357
def response(
4458
self,
4559
request_id: str,
@@ -105,22 +119,27 @@ def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]:
105119
:return: The deserialization information for this object
106120
:rtype: Dict[str, Callable[[ParseNode], None]]
107121
"""
108-
return {
109-
'responses':
110-
lambda n: setattr(
111-
self, 'responses',
112-
{item.id: item
113-
for item in n.get_collection_of_object_values(BatchResponseItem)}
114-
)
122+
fields = {
123+
"responses":
124+
lambda n:
125+
setattr(self, 'responses', n.get_collection_of_object_values(BatchResponseItem))
115126
}
127+
return fields
128+
# return {
129+
# 'responses':
130+
# lambda n: setattr(
131+
# self, '_responses',
132+
# {item.id: item
133+
# for item in n.get_collection_of_object_values(BatchResponseItem)}
134+
# )
135+
# }
116136

117137
def serialize(self, writer: SerializationWriter) -> None:
118138
"""
119139
Writes the objects properties to the current writer.
120140
:param writer: The writer to write to
121141
"""
122-
if self._responses:
123-
writer.write_collection_of_object_values('responses', self.responses())
142+
writer.write_collection_of_object_values('responses', self._responses)
124143

125144
@staticmethod
126145
def create_from_discriminator_value(

0 commit comments

Comments
 (0)