Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

42 lines · 1.0 KiB · Python

Last changed by

Reorganise structureTom Christiebefore adoption0c6f618

History of this file

Take it

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 1why, one step back
1import codecs

Finessing interfaceTom Christiebefore adoptiond7619a9

line 2why, one step back
2import http

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 3–5why, one step back
3import typing
45

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 7–11why, one step back
7 """
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 back
12 return value.encode(encoding or "ascii").lower()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 13–14why, one step back
13
14

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 22–25why, one step back
22
2324def get_reason_phrase(status_code: int) -> str:25 """

Reorganise structureTom Christiebefore adoption0c6f618

line 26why, one step back
26 Return an HTTP reason phrase such as "OK" for 200, or "Not Found" for 404.

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 27–31why, one step back
27 """
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 back
32
3334def is_known_encoding(encoding: str) -> bool:

Reorganise structureTom Christiebefore adoption0c6f618

lines 35–37why, one step back
35 """
36 Return `True` if `encoding` is a known codec.37 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 38–42why, one step back
38 try:
39 codecs.lookup(encoding)40 except LookupError:41 return False42 return True