Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a try_add method for request headers #155

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kiota_abstractions/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION: str = "0.8.6"
VERSION: str = "0.8.7"
14 changes: 12 additions & 2 deletions kiota_abstractions/request_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_request_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"}