From b77bf69fd54a674be6ed53648a22fd10dfe216d9 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 4 Oct 2023 12:08:59 +0100 Subject: [PATCH 1/3] Add a try_add method for request headers --- CHANGELOG.md | 7 +++++++ kiota_abstractions/_version.py | 2 +- kiota_abstractions/request_information.py | 16 ++++++++++++++-- tests/test_request_information.py | 8 +++++++- 4 files changed, 29 insertions(+), 4 deletions(-) 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..283d5b9 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -117,6 +117,18 @@ 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 + else: + self.headers[lowercase_key] = {str(value)} + return True + else: + return False + def remove_request_headers(self, key: str) -> None: """Removes a request header from the current request @@ -223,7 +235,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 +285,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"} From 2f2fd26ffac448ee3fb8ac5cef6a955db1c6a111 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 4 Oct 2023 12:38:17 +0100 Subject: [PATCH 2/3] fix linting issues --- kiota_abstractions/request_information.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 283d5b9..1d369c2 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -126,8 +126,7 @@ def try_add_request_header(self, key: str, value: str) -> bool: else: self.headers[lowercase_key] = {str(value)} return True - else: - return False + return False def remove_request_headers(self, key: str) -> None: """Removes a request header from the current request From 0e23e005c1e36387252ceca90786afbc4d338215 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 4 Oct 2023 12:39:20 +0100 Subject: [PATCH 3/3] more linting --- kiota_abstractions/request_information.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kiota_abstractions/request_information.py b/kiota_abstractions/request_information.py index 1d369c2..091c700 100644 --- a/kiota_abstractions/request_information.py +++ b/kiota_abstractions/request_information.py @@ -123,9 +123,8 @@ def try_add_request_header(self, key: str, value: str) -> bool: lowercase_key = key.lower() if lowercase_key in self.headers: return False - else: - self.headers[lowercase_key] = {str(value)} - return True + self.headers[lowercase_key] = {str(value)} + return True return False def remove_request_headers(self, key: str) -> None: