99from urllib .parse import urlparse
1010
1111from kiota_abstractions .headers_collection import HeadersCollection as RequestHeaders
12+ from kiota_abstractions .method import Method
1213from kiota_abstractions .request_information import RequestInformation
1314from kiota_abstractions .serialization import Parsable
1415from kiota_abstractions .serialization import SerializationWriter
@@ -29,7 +30,7 @@ def __init__(
2930 id : str = "" ,
3031 depends_on : Optional [List [Union [str , 'BatchRequestItem' ]]] = []
3132 ):
32- """
33+ """
3334 Initializes a new instance of the BatchRequestItem class.
3435 Args:
3536 request_information (RequestInformation): The request information.
@@ -44,7 +45,7 @@ def __init__(
4445 self ._method = request_information .http_method .name
4546 else :
4647 self ._method = request_information .http_method
47- self ._headers = request_information .request_headers
48+ self ._headers : Optional [ Dict [ str , str ]] = request_information .request_headers
4849 self ._body = request_information .content
4950 self .url = request_information .url .replace ('/users/me-token-to-replace' , '/me' , 1 )
5051 self ._depends_on : Optional [List [str ]] = []
@@ -57,30 +58,34 @@ def create_with_urllib_request(
5758 id : str = "" ,
5859 depends_on : Optional [List [str ]] = None
5960 ) -> 'BatchRequestItem' :
60- """
61+ """
6162 Creates a new instance of the BatchRequestItem class from a urllib request.
6263 Args:
6364 request (urllib.request.Request): The urllib request.
6465 id (str, optional): The ID of the request item. Defaults to "".
6566 depends_on (Optional[List[str]], optional): The IDs of
6667 the requests that this request depends on. Defaults to None.
67- Returns:
68+ Returns:
6869 BatchRequestItem: A new instance of the BatchRequestItem class.
6970 """
7071 request_info = RequestInformation ()
71- request_info .http_method = request .get_method ()
72+ try :
73+ request_info .http_method = Method [request .get_method ().upper ()]
74+ except KeyError :
75+ raise KeyError (f"Request Method: { request .get_method ()} is invalid" )
76+
7277 request_info .url = request .full_url
7378 request_info .headers = RequestHeaders ()
7479 for key , value in request .headers .items ():
7580 request_info .headers .try_add (header_name = key , header_value = value )
76- request_info .content = request .data
77- return BatchRequestItem (request_info , id , depends_on )
81+ request_info .content = request .data # type: ignore
82+ return BatchRequestItem (request_info , id , depends_on ) # type: ignore # union types not analysed correctly
7883
7984 def set_depends_on (self , requests : Optional [List [Union [str , 'BatchRequestItem' ]]]) -> None :
8085 """
8186 Sets the IDs of the requests that this request depends on.
8287 Args:
83- requests (Optional[List[Union[str, BatchRequestItem]]): The
88+ requests (Optional[List[Union[str, BatchRequestItem]]): The
8489 IDs of the requests that this request depends on.
8590 """
8691 if requests :
@@ -119,7 +124,7 @@ def set_url(self, url: str) -> None:
119124
120125 @property
121126 def id (self ) -> str :
122- """
127+ """
123128 Gets the ID of the request item.
124129 Returns:
125130 str: The ID of the request item.
@@ -136,11 +141,11 @@ def id(self, value: str) -> None:
136141 self ._id = value
137142
138143 @property
139- def headers (self ) -> List [ RequestHeaders ]:
144+ def headers (self ) -> Optional [ Dict [ str , str ] ]:
140145 """
141146 Gets the headers of the request item.
142147 Returns:
143- List[RequestHeaders ]: The headers of the request item.
148+ Optional[Dict[str, str] ]: The headers of the request item.
144149 """
145150 return self ._headers
146151
@@ -151,15 +156,22 @@ def headers(self, headers: Dict[str, Union[List[str], str]]) -> None:
151156 Args:
152157 headers (Dict[str, Union[List[str], str]]): The headers of the request item.
153158 """
154- self ._headers .clear ()
155- self ._headers .update (headers )
159+ if self ._headers :
160+ self ._headers .clear ()
161+ else :
162+ self ._headers = {}
163+ headers_collection = RequestHeaders ()
164+ for header , value in headers .items ():
165+ headers_collection .add (header , value )
166+ for key , values in headers_collection .get_all ().items ():
167+ self ._headers [key ] = ', ' .join (values )
156168
157169 @property
158- def body (self ) -> None :
170+ def body (self ) -> Optional [ bytes ] :
159171 """
160172 Gets the body of the request item.
161173 Returns:
162- None : The body of the request item.
174+ Optional[bytes] : The body of the request item.
163175 """
164176 return self ._body
165177
@@ -170,7 +182,7 @@ def body(self, body: BytesIO) -> None:
170182 Args:
171183 body : (BytesIO): The body of the request item.
172184 """
173- self ._body = body
185+ self ._body = body . getvalue ()
174186
175187 @property
176188 def method (self ) -> str :
@@ -207,7 +219,7 @@ def create_from_discriminator_value(
207219 ) -> 'BatchRequestItem' :
208220 """
209221 Creates a new instance of the appropriate class based
210- on discriminator value param parse_node: The parse node
222+ on discriminator value param parse_node: The parse node
211223 to use to read the discriminator value and create the object
212224 Returns: BatchRequestItem
213225 """
@@ -216,10 +228,10 @@ def create_from_discriminator_value(
216228 return BatchRequestItem ()
217229
218230 def get_field_deserializers (self ) -> Dict [str , Any ]:
219- """
231+ """
220232 Gets the deserialization information for this object.
221233 Returns:
222- Dict[str, Any]: The deserialization information for
234+ Dict[str, Any]: The deserialization information for
223235 this object where each entry is a property key with its
224236 deserialization callback.
225237 """
@@ -233,7 +245,7 @@ def get_field_deserializers(self) -> Dict[str, Any]:
233245 }
234246
235247 def serialize (self , writer : SerializationWriter ) -> None :
236- """
248+ """
237249 Writes the objects properties to the current writer.
238250 Args:
239251 writer (SerializationWriter): The writer to write to.
@@ -242,12 +254,15 @@ def serialize(self, writer: SerializationWriter) -> None:
242254 writer .write_str_value ('method' , self .method )
243255 writer .write_str_value ('url' , self .url )
244256 writer .write_collection_of_primitive_values ('depends_on' , self ._depends_on )
245- headers = {key : ", " .join (val ) for key , val in self ._headers .items ()}
246- writer .write_collection_of_object_values ('headers' , headers )
257+ writer .write_collection_of_object_values ('headers' , self ._headers ) # type: ignore # need method to serialize dicts
247258 if self ._body :
248259 json_object = json .loads (self ._body )
249260 is_json_string = json_object and isinstance (json_object , dict )
250- writer .write_collection_of_object_values (
251- 'body' ,
252- json_object if is_json_string else base64 .b64encode (self ._body ).decode ('utf-8' )
253- )
261+ # /$batch API expects JSON object or base 64 encoded value for the body
262+ if is_json_string :
263+ writer .write_collection_of_object_values ( # type: ignore # need method to serialize dicts
264+ 'body' ,
265+ json_object
266+ )
267+ else :
268+ writer .write_str_value ('body' , base64 .b64encode (self ._body ).decode ('utf-8' ))
0 commit comments