1- from typing import TypeVar , Dict , Optional
1+ from typing import TypeVar , Dict , Optional , Union , List
22import json
33import logging
44
1010from kiota_abstractions .api_error import APIError
1111
1212from .batch_request_content import BatchRequestContent
13+ from .batch_request_content_collection import BatchRequestContentCollection
1314from .batch_response_content import BatchResponseContent
15+ from .batch_response_content_collection import BatchResponseContentCollection
1416
1517T = TypeVar ('T' , bound = 'Parsable' )
1618
@@ -31,7 +33,7 @@ def __init__(self, request_adapter: RequestAdapter, error_map: Optional[Dict[str
3133
3234 async def post (
3335 self ,
34- batch_request_content : BatchRequestContent ,
36+ batch_request_content : Union [ BatchRequestContent , BatchRequestContentCollection ] ,
3537 error_map : Optional [Dict [str , int ]] = None ,
3638 ) -> BatchResponseContent :
3739 """
@@ -47,26 +49,121 @@ async def post(
4749 """
4850 if batch_request_content is None :
4951 raise ValueError ("batch_request_content cannot be Null." )
50- request_info = await self .to_post_request_information (batch_request_content )
52+ if isinstance (batch_request_content , BatchRequestContent ):
53+ request_info = await self .to_post_request_information (batch_request_content )
54+ content = json .loads (request_info .content .decode ("utf-8" ))
55+ json_body = json .dumps (content )
56+ request_info .content = json_body
57+ error_map = error_map or self .error_map
58+ response = None
59+ try :
60+ response = await self ._request_adapter .send_async (
61+ request_info , BatchResponseContent , error_map
62+ )
63+ except APIError as e :
64+ logging .error (f"API Error: { e } " )
65+ raise e
66+ if response is None :
67+ raise ValueError ("Failed to get a valid response from the API." )
68+ 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 )
101+ 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 )
106+ return batch_responses
107+ else :
108+ raise ValueError ("Invalid type for batch_request_content." )
109+
110+ print (f"request info to be posted { request_info } " )
111+
112+ # Decode and re-encode the content to ensure it is in the correct format
51113 content = json .loads (request_info .content .decode ("utf-8" ))
52114 json_body = json .dumps (content )
53- request_info .content = json_body
115+ request_info .content = json_body .encode ("utf-8" )
116+
54117 error_map = error_map or self .error_map
55118 response = None
119+
56120 try :
57121 response = await self ._request_adapter .send_async (
58122 request_info , BatchResponseContent , error_map
59123 )
60124 except APIError as e :
61125 logging .error (f"API Error: { e } " )
62126 raise e
127+
63128 if response is None :
64129 raise ValueError ("Failed to get a valid response from the API." )
130+
65131 return response
66132
67- async def to_post_request_information (
133+ async def post_batch_collection (
68134 self ,
69- batch_request_content : BatchRequestContent ,
135+ batch_request_content_collection : BatchRequestContentCollection ,
136+ error_map : Optional [Dict [str , int ]] = None ,
137+ ) -> BatchResponseContentCollection :
138+ """
139+ Sends a collection of batch requests and returns a collection of batch response contents.
140+
141+ Args:
142+ batch_request_content_collection (BatchRequestContentCollection): The collection of batch request contents.
143+ error_map: Dict[str, int] = {}:
144+ Error mappings for response handling.
145+
146+ Returns:
147+ BatchResponseContentCollection: The collection of batch response contents.
148+ """
149+ if batch_request_content_collection is None :
150+ raise ValueError ("batch_request_content_collection cannot be Null." )
151+
152+ batch_responses = BatchResponseContentCollection ()
153+
154+ for batch_request_content in batch_request_content_collection .batches :
155+ request_info = await self .to_post_request_information (batch_request_content )
156+ print (f"request_info content before call { request_info .content } " )
157+ # response = await self._request_adapter.send_async(
158+ # request_info, BatchResponseContent, error_map
159+ # )
160+ # = await self.post_content(batch_request_content, error_map)
161+ # batch_responses.add_response(batch_request_content.requests.keys(), response)
162+
163+ return batch_responses
164+
165+ async def to_post_request_information (
166+ self , batch_request_content : BatchRequestContent
70167 ) -> RequestInformation :
71168 """
72169 Creates request information for a batch POST request.
@@ -79,12 +176,51 @@ async def to_post_request_information(
79176 """
80177 if batch_request_content is None :
81178 raise ValueError ("batch_request_content cannot be Null." )
179+ if isinstance (batch_request_content , BatchRequestContent ):
180+ request_info = RequestInformation ()
181+ request_info .http_method = Method .POST
182+ request_info .url_template = self .url_template
183+
184+ requests_dict = [
185+ item .get_field_deserializers () for item in batch_request_content .requests
186+ ]
187+ request_info .content = json .dumps ({"requests" : requests_dict }).encode ("utf-8" )
188+
189+ request_info .headers = HeadersCollection ()
190+ request_info .headers .try_add ("Content-Type" , APPLICATION_JSON )
191+ request_info .set_content_from_parsable (
192+ self ._request_adapter , APPLICATION_JSON , batch_request_content
193+ )
194+
195+ return request_info
196+
197+ async def to_post_request_information_from_collection (
198+ self , batch_request_content : BatchRequestContentCollection
199+ ) -> RequestInformation :
200+ """
201+ Creates request information for a batch POST request from a collection.
202+
203+ Args:
204+ batch_request_content (BatchRequestContentCollection): The batch request content collection.
205+
206+ Returns:
207+ RequestInformation: The request information.
208+ """
209+ if batch_request_content is None :
210+ raise ValueError ("batch_request_content cannot be Null." )
211+
82212 request_info = RequestInformation ()
83213 request_info .http_method = Method .POST
84214 request_info .url_template = self .url_template
85215
86- requests_dict = [item .get_field_deserializers () for item in batch_request_content .requests ]
87- request_info .content = json .dumps ({"requests" : requests_dict }).encode ("utf-8" )
216+ all_requests = []
217+ for batch_content in batch_request_content .batches :
218+ print (f"batch_content { batch_content } " )
219+ requests_dict = [item .get_field_deserializers () for item in batch_content .requests ]
220+ all_requests .extend (requests_dict )
221+
222+ request_info .content = json .dumps ({"requests" : all_requests }).encode ("utf-8" )
223+ print (f"All requests { request_info .content } " )
88224
89225 request_info .headers = HeadersCollection ()
90226 request_info .headers .try_add ("Content-Type" , APPLICATION_JSON )
0 commit comments