-
Notifications
You must be signed in to change notification settings - Fork 15
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 from_bytes_dict alternative constructor for HttpResponseHeaders #33
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import json | ||
from typing import Optional, Dict, List, TypeVar, Type | ||
from typing import Optional, Dict, List, TypeVar, Type, Union | ||
|
||
import attrs | ||
from multidict import CIMultiDict | ||
|
@@ -14,6 +14,7 @@ | |
from .utils import memoizemethod_noargs | ||
|
||
T_headers = TypeVar("T_headers", bound="HttpResponseHeaders") | ||
BytesDict = Dict[bytes, Union[bytes, List[bytes]]] | ||
|
||
|
||
class HttpResponseBody(bytes): | ||
|
@@ -74,6 +75,45 @@ def from_name_value_pairs(cls: Type[T_headers], arg: List[Dict]) -> T_headers: | |
""" | ||
return cls([(pair["name"], pair["value"]) for pair in arg]) | ||
|
||
@classmethod | ||
def from_bytes_dict( | ||
cls: Type[T_headers], arg: BytesDict, encoding: str = "utf-8" | ||
) -> T_headers: | ||
"""An alternative constructor for instantiation where the header-value | ||
pairs are in raw bytes form. | ||
|
||
This supports multiple header values in the form of ``List[bytes]`` | ||
alongside a plain ``bytes`` value. | ||
|
||
By default, it converts the ``bytes`` value using "utf-8". However, this | ||
can easily be overridden using the ``encoding`` parameter. | ||
|
||
>>> raw_values = { | ||
... b"Content-Encoding": [b"gzip", b"br"], | ||
... b"Content-Type": [b"text/html"], | ||
... b"content-length": b"648", | ||
... } | ||
>>> headers = HttpResponseHeaders.from_bytes_dict(raw_values) | ||
>>> headers | ||
<HttpResponseHeaders('Content-Encoding': 'gzip', 'Content-Encoding': 'br', 'Content-Type': 'text/html', 'content-length': '648')> | ||
""" | ||
|
||
def _norm(data): | ||
if isinstance(data, str): | ||
return data | ||
elif isinstance(data, bytes): | ||
return data.decode(encoding) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should raise an exception if data is not bytes or str There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Adressed this in ddb7d20 alongside the ability to handle |
||
|
||
converted = [] | ||
|
||
for header, value in arg.items(): | ||
if isinstance(value, list): | ||
converted.extend([(_norm(header), _norm(v)) for v in value]) | ||
else: | ||
converted.append((_norm(header), _norm(value))) | ||
|
||
return cls(converted) | ||
|
||
def declared_encoding(self) -> Optional[str]: | ||
""" Return encoding detected from the Content-Type header, or None | ||
if encoding is not found """ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
utf-8
default value was based on how Scrapy was using it as the default for its Headers.