-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from Ousret/develop
Prepare release 2.3.0
- Loading branch information
Showing
9 changed files
with
292 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Dict, List, Optional, Union | ||
|
||
from kiss_headers.models import Header, Headers | ||
|
||
|
||
def encode(headers: Headers) -> Dict[str, List[Dict]]: | ||
""" | ||
Provide an opinionated but reliable way to encode headers to dict for serialization purposes. | ||
""" | ||
result: Dict[str, List[Dict]] = dict() | ||
|
||
for header in headers: | ||
|
||
if header.name not in result: | ||
result[header.name] = list() | ||
|
||
encoded_header: Dict[str, Union[Optional[str], List[str]]] = dict() | ||
|
||
for attribute, value in header: | ||
|
||
if attribute not in encoded_header: | ||
encoded_header[attribute] = value | ||
continue | ||
|
||
if isinstance(encoded_header[attribute], list) is False: | ||
# Here encoded_header[attribute] most certainly is str | ||
# Had to silent mypy error. | ||
encoded_header[attribute] = [encoded_header[attribute]] # type: ignore | ||
|
||
encoded_header[attribute].append(value) # type: ignore | ||
|
||
result[header.name].append(encoded_header) | ||
|
||
return result | ||
|
||
|
||
def decode(encoded_headers: Dict[str, List[Dict]]) -> Headers: | ||
""" | ||
Decode any previously encoded headers to a Headers object. | ||
""" | ||
headers: Headers = Headers() | ||
|
||
for header_name, encoded_header_list in encoded_headers.items(): | ||
if not isinstance(encoded_header_list, list): | ||
raise ValueError("Decode require first level values to be List") | ||
|
||
for encoded_header in encoded_header_list: | ||
if not isinstance(encoded_header, dict): | ||
raise ValueError("Decode require each list element to be Dict") | ||
|
||
header = Header(header_name, "") | ||
|
||
for attr, value in encoded_header.items(): | ||
if value is None: | ||
header += attr | ||
continue | ||
if isinstance(value, str): | ||
header[attr] = value | ||
continue | ||
|
||
for sub_value in value: | ||
header.insert(-1, **{attr: sub_value}) | ||
|
||
headers += header | ||
|
||
return headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
Expose version | ||
""" | ||
|
||
__version__ = "2.2.4" | ||
__version__ = "2.3.0" | ||
VERSION = __version__.split(".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ def get_version(): | |
|
||
# Package meta-data. | ||
NAME = "kiss-headers" | ||
DESCRIPTION = "Python package for object oriented headers, HTTP/1.1 style. Also parse headers." | ||
DESCRIPTION = "Python package for object oriented headers, HTTP/1.1 style. Parser and serializer for http headers." | ||
URL = "https://github.com/ousret/kiss-headers" | ||
EMAIL = "[email protected]" | ||
AUTHOR = "Ahmed TAHRI @Ousret" | ||
|
@@ -71,6 +71,7 @@ def get_version(): | |
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Topic :: Utilities", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
|
Oops, something went wrong.