diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b84a79..d45a724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.8.7] - 2023-10-05 + +### Added +- Added a try_add method for request headers + +### Changed + ## [0.8.6] - 2023-09-19 ### Added diff --git a/kiota_abstractions/_version.py b/kiota_abstractions/_version.py index 01c2028..538ada5 100644 --- a/kiota_abstractions/_version.py +++ b/kiota_abstractions/_version.py @@ -1 +1 @@ -VERSION: str = "0.8.6" +VERSION: str = "0.8.7" diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 49dbdd8..091c700 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -117,6 +117,16 @@ def add_request_headers( else: self.headers[lowercase_key] = {str(value)} + def try_add_request_header(self, key: str, value: str) -> bool: + """Try to add an header to the request if it's not already set""" + if key and value: + lowercase_key = key.lower() + if lowercase_key in self.headers: + return False + self.headers[lowercase_key] = {str(value)} + return True + return False + def remove_request_headers(self, key: str) -> None: """Removes a request header from the current request @@ -223,7 +233,7 @@ def set_stream_content(self, value: BytesIO) -> None: Args: value (BytesIO): the binary stream """ - self.headers[self.CONTENT_TYPE_HEADER] = {self.BINARY_CONTENT_TYPE} + self.try_add_request_header(self.CONTENT_TYPE_HEADER, self.BINARY_CONTENT_TYPE) self.content = value def set_query_string_parameters_from_raw_object(self, q: Optional[QueryParams]) -> None: @@ -273,7 +283,7 @@ def _set_content_and_content_type_header( self, writer: SerializationWriter, content_type: Optional[str] ): if content_type: - self.headers[self.CONTENT_TYPE_HEADER] = {content_type} + self.try_add_request_header(self.CONTENT_TYPE_HEADER, content_type) self.content = writer.get_serialized_content() def _decode_uri_string(self, uri: Optional[str]) -> str: diff --git a/tests/test_request_information.py b/tests/test_request_information.py index 877ae4d..13af504 100644 --- a/tests/test_request_information.py +++ b/tests/test_request_information.py @@ -84,6 +84,12 @@ def test_request_headers(mock_request_information): assert "value3" in mock_request_information.request_headers["header2"] assert "value4" in mock_request_information.request_headers["header2"] +def test__try_add_request_header(mock_request_information): + """Test the final request header after try_add + """ + assert mock_request_information.try_add_request_header("header1", "value1") == True + assert mock_request_information.try_add_request_header("header1", "value2") == False + assert "value1" in mock_request_information.request_headers["header1"] def test_remove_request_headers(mock_request_information): """Tests removing a request header @@ -103,4 +109,4 @@ def test_set_stream_content(mock_request_information): """ mock_request_information.set_stream_content(b'stream') assert mock_request_information.content == b'stream' - assert mock_request_information.headers["Content-Type"] == {"application/octet-stream"} + assert mock_request_information.headers["content-type"] == {"application/octet-stream"}