Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

31 lines · 784 bytes · Python

Last changed by

Status code tweaks (#77)Tom Christiebefore adoptionc12c271

History of this file

Take it

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 1why, one step back
1import codecs

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 2–4why, one step back
2import typing
34

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 5why, one step back
5def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 6–10why, one step back
6 """
7 Coerce str/bytes into a strictly byte-wise HTTP header key.8 """9 if isinstance(value, bytes):10 return value.lower()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 11why, one step back
11 return value.encode(encoding or "ascii").lower()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 12–13why, one step back
12
13

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 14why, one step back
14def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 15–19why, one step back
15 """
16 Coerce str/bytes into a strictly byte-wise HTTP header value.17 """18 if isinstance(value, bytes):19 return value

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 20why, one step back
20 return value.encode(encoding or "ascii")

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 21–23why, one step back
21
2223def is_known_encoding(encoding: str) -> bool:

Reorganise structureTom Christiebefore adoption0c6f618

lines 24–26why, one step back
24 """
25 Return `True` if `encoding` is a known codec.26 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 27–31why, one step back
27 try:
28 codecs.lookup(encoding)29 except LookupError:30 return False31 return True