Skip to content

Commit

Permalink
Merge pull request #155 from andreaTP/implement-tryadd
Browse files Browse the repository at this point in the history
Add a try_add method for request headers
  • Loading branch information
baywet authored Oct 10, 2023
2 parents 39b844b + 0e23e00 commit af5ad23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
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"}

0 comments on commit af5ad23

Please sign in to comment.