Skip to content
Myr

update docstring/docs for transport using request/response models (#2070)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* update docstring/docs for transport based on pull #1840 * Update httpx/_transports/base.py * lint'd

authored by toppk · committed by GitHub · parent 420911b · Browse files · 2 files+18 added−52 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

2 files+18 added−52 removed

  • other:+18 added−52 removed

2 files+18 added−52 removed

modified:docs/advanced.md+4 added-7 removed
1049 unmodified lines
1050subclass `httpx.BaseTransport` to implement a transport to use with `Client`,1051or subclass `httpx.AsyncBaseTransport` to implement a transport to1052use with `AsyncClient`.10531054At the layer of the transport API we're using plain primitives.1055No `Request` or `Response` models, no fancy `URL` or `Header` handling.1056This strict point of cut-off provides a clear design separation between the1057HTTPX API, and the low-level network handling.1054At the layer of the transport API we're using the familiar `Request` and1055`Response` models.10561057See the `handle_request` and `handle_async_request` docstrings for more details1058on the specifics of the Transport API.1059
8 unmodified lines
1068 """1069 A mock transport that always returns a JSON "Hello, world!" response.1070 """10711074 def handle_request(self, method, url, headers, stream, extensions):1072 def handle_request(self, request):1073 message = {"text": "Hello, world!"}1074 content = json.dumps(message).encode("utf-8")1075 stream = httpx.ByteStream(content)1076 headers = [(b"content-type", b"application/json")]1079 extensions = {}1080 return 200, headers, stream, extensions1077 return httpx.Response(200, headers=headers, stream=stream)1078```10791080Which we can use in the same way:1081
95 unmodified lines
modified:httpx/_transports/base.py+14 added-45 removed
21 unmodified lines
22 def handle_request(self, request: Request) -> Response:23 """24 Send a single HTTP request and return a response.2526 At this layer of API we're simply using plain primitives. No `Request` or27 `Response` models, no fancy `URL` or `Header` handling. This strict point28 of cut-off provides a clear design separation between the HTTPX API,29 and the low-level network handling.3026 Developers shouldn't typically ever need to call into this API directly,27 since the Client class provides all the higher level user-facing API28 niceties.2935 In order to properly release any network resources, the response stream36 should *either* be consumed immediately, with a call to `stream.read()`,37 or else the `handle_request` call should be followed with a try/finally38 block to ensuring the stream is always closed.30 In order to properly release any network resources, the response31 stream should *either* be consumed immediately, with a call to32 `response.stream.read()`, or else the `handle_request` call should33 be followed with a try/finally block to ensuring the stream is34 always closed.3536 Example usage:3738 with httpx.HTTPTransport() as transport:43 status_code, headers, stream, extensions = transport.handle_request(44 method=b'GET',45 url=(b'https', b'www.example.com', 443, b'/'),46 headers=[(b'Host', b'www.example.com')],47 stream=[],48 extensions={}39 req = httpx.Request(40 method=b"GET",41 url=(b"https", b"www.example.com", 443, b"/"),42 headers=[(b"Host", b"www.example.com")],43 )44 resp = transport.handle_request(req)50 body = stream.read()51 print(status_code, headers, body)45 body = resp.stream.read()46 print(resp.status_code, resp.headers, body)4753 Arguments:4855 method: The request method as bytes. Eg. b'GET'.56 url: The components of the request URL, as a tuple of `(scheme, host, port, target)`.57 The target will usually be the URL path, but also allows for alternative58 formulations, such as proxy requests which include the complete URL in59 the target portion of the HTTP request, or for "OPTIONS *" requests, which60 cannot be expressed in a URL string.61 headers: The request headers as a list of byte pairs.62 stream: The request body as a bytes iterator.63 extensions: An open ended dictionary, including optional extensions to the64 core request/response API. Keys may include:65 timeout: A dictionary of str:Optional[float] timeout values.66 May include values for 'connect', 'read', 'write', or 'pool'.49 Takes a `Request` instance as the only argument.5068 Returns a tuple of:6970 status_code: The response status code as an integer. Should be in the range 1xx-5xx.71 headers: The response headers as a list of byte pairs.72 stream: The response body as a bytes iterator.73 extensions: An open ended dictionary, including optional extensions to the74 core request/response API. Keys are plain strings, and may include:75 reason_phrase: The reason-phrase of the HTTP response, as bytes. Eg b'OK'.76 HTTP/2 onwards does not include a reason phrase on the wire.77 When no key is included, a default based on the status code may78 be used. An empty-string reason phrase should not be substituted79 for a default, as it indicates the server left the portion blank80 eg. the leading response bytes were b"HTTP/1.1 200 <CRLF>".81 http_version: The HTTP version, as bytes. Eg. b"HTTP/1.1".82 When no http_version key is included, HTTP/1.1 may be assumed.51 Returns a `Response` instance.52 """53 raise NotImplementedError(54 "The 'handle_request' method must be implemented."55 ) # pragma: nocover
27 unmodified lines