utils.py
This file
Path
httpcore/utils.py
42 lines · 1.0 KiB · Python
Last changed by
Reorganise structureTom Christiebefore adoption0c6f618
History of this fileTake it
Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 1why, one step back1import codecsFinessing interfaceTom Christiebefore adoptiond7619a9
line 2why, one step back2import httpMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 3–5why, one step back3import typing45Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 6why, one step back6def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 7–11why, one step back7 """8 Coerce str/bytes into a strictly byte-wise HTTP header key.9 """10 if isinstance(value, bytes):11 return value.lower()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 12why, one step back12 return value.encode(encoding or "ascii").lower()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 13–14why, one step back1314Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 15why, one step back15def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 16–20why, one step back16 """17 Coerce str/bytes into a strictly byte-wise HTTP header value.18 """19 if isinstance(value, bytes):20 return valueRejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 21why, one step back21 return value.encode(encoding or "ascii")Finessing interfaceTom Christiebefore adoptiond7619a9
lines 22–25why, one step back222324def get_reason_phrase(status_code: int) -> str:25 """Reorganise structureTom Christiebefore adoption0c6f618
line 26why, one step back26 Return an HTTP reason phrase such as "OK" for 200, or "Not Found" for 404.Finessing interfaceTom Christiebefore adoptiond7619a9
lines 27–31why, one step back27 """28 try:29 return http.HTTPStatus(status_code).phrase30 except ValueError as exc:31 return ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 32–34why, one step back323334def is_known_encoding(encoding: str) -> bool:Reorganise structureTom Christiebefore adoption0c6f618
lines 35–37why, one step back35 """36 Return `True` if `encoding` is a known codec.37 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 38–42why, one step back38 try:39 codecs.lookup(encoding)40 except LookupError:41 return False42 return True