Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

39 lines · 980 bytes · Python

Last changed by

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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–31why, one step back
22
2324def get_reason_phrase(status_code: int) -> str:25 """26 Return an HTTP reason phrase, eg. "OK" for 200, or "Not Found" for 404.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–39why, one step back
32
3334def is_known_encoding(encoding: str) -> bool:35 try:36 codecs.lookup(encoding)37 except LookupError:38 return False39 return True