Skip to content

Commit cf35e47

Browse files
committed
clean up post batch with collection
1 parent b871815 commit cf35e47

File tree

1 file changed

+5
-93
lines changed

1 file changed

+5
-93
lines changed

src/msgraph_core/requests/batch_request_builder.py

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -61,73 +61,18 @@ async def post(
6161
request_info, BatchResponseContent, error_map
6262
)
6363
except APIError as e:
64-
logging.error(f"API Error: {e}")
64+
logging.error("API Error: %s", e)
6565
raise e
6666
if response is None:
6767
raise ValueError("Failed to get a valid response from the API.")
6868
return response
69-
if isinstance(batch_request_content, BatchRequestContentCollection):
70-
request_info = await self.to_post_request_information_from_collection(
71-
batch_request_content
72-
)
73-
74-
content = json.loads(request_info.content.decode("utf-8"))
75-
json_body = json.dumps(content)
76-
request_info.content = json_body
77-
error_map = error_map or self.error_map
78-
response = None
79-
try:
80-
response = await self._request_adapter.send_async(
81-
request_info, BatchResponseContent, error_map
82-
)
83-
except APIError as e:
84-
logging.error(f"API Error: {e}")
85-
raise e
86-
if response is None:
87-
raise ValueError("Failed to get a valid response from the API.")
88-
return response
89-
90-
async def post_content(
91-
self,
92-
batch_request_content: BatchRequestContentCollection,
93-
error_map: Optional[Dict[str, int]] = None,
94-
) -> BatchResponseContentCollection:
95-
if batch_request_content is None:
96-
raise ValueError("batch_request_content cannot be Null.")
97-
98-
# Determine the type of batch_request_content and call the appropriate method
99-
if isinstance(batch_request_content, BatchRequestContent):
100-
request_info = await self.to_post_request_information(batch_request_content)
10169
elif isinstance(batch_request_content, BatchRequestContentCollection):
102-
request_info = await self.to_post_request_information_from_collection(
103-
batch_request_content
104-
)
105-
batch_responses = await self.post_batch_collection(batch_request_content)
70+
batch_responses = await self.post_batch_collection(batch_request_content, error_map)
10671
return batch_responses
72+
10773
else:
10874
raise ValueError("Invalid type for batch_request_content.")
10975

110-
# Decode and re-encode the content to ensure it is in the correct format
111-
content = json.loads(request_info.content.decode("utf-8"))
112-
json_body = json.dumps(content)
113-
request_info.content = json_body.encode("utf-8")
114-
115-
error_map = error_map or self.error_map
116-
response = None
117-
118-
try:
119-
response = await self._request_adapter.send_async(
120-
request_info, BatchResponseContent, error_map
121-
)
122-
except APIError as e:
123-
logging.error(f"API Error: {e}")
124-
raise e
125-
126-
if response is None:
127-
raise ValueError("Failed to get a valid response from the API.")
128-
129-
return response
130-
13176
async def post_batch_collection(
13277
self,
13378
batch_request_content_collection: BatchRequestContentCollection,
@@ -137,7 +82,8 @@ async def post_batch_collection(
13782
Sends a collection of batch requests and returns a collection of batch response contents.
13883
13984
Args:
140-
batch_request_content_collection (BatchRequestContentCollection): The collection of batch request contents.
85+
batch_request_content_collection (BatchRequestContentCollection): The
86+
collection of batch request contents.
14187
error_map: Dict[str, int] = {}:
14288
Error mappings for response handling.
14389
@@ -189,37 +135,3 @@ async def to_post_request_information(
189135
)
190136

191137
return request_info
192-
193-
async def to_post_request_information_from_collection(
194-
self, batch_request_content: BatchRequestContentCollection
195-
) -> RequestInformation:
196-
"""
197-
Creates request information for a batch POST request from a collection.
198-
199-
Args:
200-
batch_request_content (BatchRequestContentCollection): The batch request content collection.
201-
202-
Returns:
203-
RequestInformation: The request information.
204-
"""
205-
if batch_request_content is None:
206-
raise ValueError("batch_request_content cannot be Null.")
207-
208-
request_info = RequestInformation()
209-
request_info.http_method = Method.POST
210-
request_info.url_template = self.url_template
211-
212-
all_requests = []
213-
for batch_content in batch_request_content.batches:
214-
requests_dict = [item.get_field_deserializers() for item in batch_content.requests]
215-
all_requests.extend(requests_dict)
216-
217-
request_info.content = json.dumps({"requests": all_requests}).encode("utf-8")
218-
219-
request_info.headers = HeadersCollection()
220-
request_info.headers.try_add("Content-Type", APPLICATION_JSON)
221-
request_info.set_content_from_parsable(
222-
self._request_adapter, APPLICATION_JSON, batch_request_content
223-
)
224-
225-
return request_info

0 commit comments

Comments
 (0)