Tidy up the formatting of HTTP/2 requests (#1860)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Tidy up the formatting of HTTP/2 requests * Black linting
authored by Tom Christie · committed by GitHub · parent d41840e · Browse files · 3 files·+13 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+13 added−9 removed
- other:+13 added−9 removed
Provenance
- Commit
e1abaf1- Tree
e54a705- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
d41840e
3 files changed+13 added−9 removed
modified:docs/index.md+1 added-1 removed
120 unmodified linesExpand all12112212312412512512612712812913024 unmodified lines120 unmodified linesExpand all * `idna` - Internationalized domain name support.* `sniffio` - Async library autodetection.* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)** `click` - Command line client support. *(Optional, with `httpx[cli]`)** `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)** `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)** `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
A huge amount of credit is due to `requests` for the API layout thatmuch of this work follows, as well as to `urllib3` for plenty of designinspiration around the lower-level networking details.24 unmodified linesmodified:httpx/_main.py+11 added-7 removed
1+import functools2 import json3 import sys4 import typing5 95 unmodified lines101 pass102 return "" # pragma: nocover103 104 104-def format_request_headers(request: Request) -> str:105+def format_request_headers(request: Request, http2: bool = False) -> str:106+ version = "HTTP/2" if http2 else "HTTP/1.1"107+ headers = [108+ (name.lower() if http2 else name, value) for name, value in request.headers.raw109+ ]110 target = request.url.raw[-1].decode("ascii")106- lines = [f"{request.method} {target} HTTP/1.1"] + [107- f"{name.decode('ascii')}: {value.decode('ascii')}"108- for name, value in request.headers.raw111+ lines = [f"{request.method} {target} {version}"] + [112+ f"{name.decode('ascii')}: {value.decode('ascii')}" for name, value in headers113 ]114 return "\n".join(lines)115 116 6 unmodified lines123 ]124 return "\n".join(lines)125 126 123-def print_request_headers(request: Request) -> None:127+def print_request_headers(request: Request, http2: bool = False) -> None:128 console = rich.console.Console()125- http_text = format_request_headers(request)129+ http_text = format_request_headers(request, http2=http2)130 syntax = rich.syntax.Syntax(http_text, "http", theme="ansi_dark", word_wrap=True)131 console.print(syntax)132 syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)133 console.print(syntax)264 unmodified lines398 method = "POST" if content or data or files or json else "GET"399 400 event_hooks: typing.Dict[str, typing.List[typing.Callable]] = {}401 if verbose:398- event_hooks["request"] = [print_request_headers]402+ event_hooks["request"] = [functools.partial(print_request_headers, http2=http2)]403 if follow_redirects:404 event_hooks["response"] = [print_redirects]405 406 try:36 unmodified linesmodified:README.md+1 added-1 removed
135 unmodified lines136 * `idna` - Internationalized domain name support.137 * `sniffio` - Async library autodetection.138 * `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*139 * `click` - Command line client support. *(Optional, with `httpx[cli]`)*140-* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*140 * `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*141+* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*142 143 A huge amount of credit is due to `requests` for the API layout that144 much of this work follows, as well as to `urllib3` for plenty of design145 inspiration around the lower-level networking details.3 unmodified lines