README.md
This file
Path
README.md
153 lines · 5.9 KiB · Markdown
Last changed by
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
History of this fileTake it
StuffTom Christiebefore adoption30530d4
lines 1–2why, one step back1# HTTPCore2Update READMETom Christiebefore adoption665743a
lines 3–62why, one step back3I started to dive into implementation and API design here.45I know this isn't what you were suggesting with `requests-core`, but it'd be6worth you taking a slow look at this and seeing if there's anything that you7think is a no-go.89`httpcore` provides the same proposed *functionality* as requests-core, but at a slightly10lower abstraction level.1112Rather than returning `Response` models, it returns the minimal possible13interface. There's no `response.text` or any other cleverness, `response.headers`14are plain byte-pair lists, rather than a headers datastructure etc...1516**The proposal here is that `httpcore` would be a silent-partner dependency of `requests3`,17taking the place of the existing `urllib3` dependency.**1819---2021The benefits to my mind of this level of abstraction are that it is as22agnostic as possible to whatever request/response models are built on top23of it, and exposes only plain datastructures that reflect the network response.2425* An `encode/httpcore` package would be something I'd gladly maintain. The naming26 makes sense to me, as there's no strictly implied relationship to `requests`,27 although it would fulfil all the requirements for `requests3` to build on,28 and would have a strict semver policy.29* An `encode/httpcore` package is something that would play in well to the30 collaboratively sponsored OSS story that Encode is pitching. It'd provide what31 you need for `requests3` without encroaching on the `requests` brand.32 We'd position it similarly to how `urllib3` is positioned to `requests` now.33 A focused, low-level networking library, that `requests` then builds the34 developer-focused API on top of.35* The current implementation includes all the async API points.36 The `PoolManger.request()` and `PoolManager.close()` methods are currently37 stubbed-out. All the remaining implementation hangs off of those two points.38* Take a quick look over the test cases or the package itself to get a feel39 for it. It's all type annotated, and should be easy to find your way around.40* I've not yet added corresponding sync API points to the implementation, but41 they will come.42* There's [a chunk of implmentation work towards connection pooling in `requests-async`](https://github.com/encode/requests-async/blob/5ec2aa80bd4499997fa744f3be19a0bdeccbaeed/requests_async/connections.py). I've not had enough time to nail it yet, but it's got the broad brush-strokes, and given me enough to get a rough feel for how much work there is to do.43* We would absolutely want to implement HTTP/2 support.44* Trio support is something that could *potentially* come later, but it needs to45 be a secondary consideration.46* I think all the functionality required is stubbed out in the API, with two exceptions.47 (1) I've not yet added any proxy configuration API. Haven't looked into that enough48 yet. (2) I've not yet added any retry configuration API, since I havn't really49 looked enough into which side of requests vs. urllib3 that sits on, or exactly how50 urllib3 tackles retries, etc.51* I'd be planning to prioritize working on this from Mon 15th April. I don't think52 it'd take too long to get it to a feature complete and API stable state.53 (With the exception of the later HTTP/2 work, which I can't really assess yet.)54 I probably don't have any time left before then - need to focus on what I'm55 delivering to DjangoCon Europe over the rest of this week.56* To my mind the killer app for `requests3`/`httpcore` is a high-performance57 proxy server / gateway service in Python. Pitching the growing ASGI ecosystem58 is an important part of that story.59* I think there's enough headroom before PyCon to have something ready to pitch by then.60 I could be involved in sprints remotely if there's areas we still need to fill in,61 anyplace.62StuffTom Christiebefore adoption30530d4
lines 63–65why, one step back63```python64import httpcore65Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 66–67why, one step back66http = httpcore.ConnectionPool()67response = await http.request('GET', 'http://example.com')StuffTom Christiebefore adoption30530d4
lines 68–71why, one step back68assert response.status_code == 20069assert response.body == b'Hello, world'70```71Update READMETom Christiebefore adoption68b80dc
line 72why, one step back72Top-level API...StuffTom Christiebefore adoption30530d4
lines 73–74why, one step back7374```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 75–76why, one step back75http = httpcore.ConnectionPool([ssl], [timeout], [limits])76response = await http.request(method, url, [headers], [body], [stream])StuffTom Christiebefore adoption30530d4
lines 77–78why, one step back77```78Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 79why, one step back79ConnectionPool as a context-manager...StuffTom Christiebefore adoption30530d4
lines 80–81why, one step back8081```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 82–83why, one step back82async with httpcore.ConnectionPool([ssl], [timeout], [limits]) as http:83 response = await http.request(method, url, [headers], [body], [stream])Update READMETom Christiebefore adoption68b80dc
lines 84–88why, one step back84```8586Streaming...8788```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 89–90why, one step back89http = httpcore.ConnectionPool()90response = await http.request(method, url, stream=True)Update READMETom Christiebefore adoption68b80dc
lines 91–92why, one step back91async for part in response.stream():92 ...Added proxy exampleTom Christiebefore adoption87b21b0
lines 93–105why, one step back93```9495The level of abstraction fits in really well if you're just writing at96the raw ASGI level. Eg. Here's an how an ASGI gateway server looks against the97API, including streaming uploads and downloads...9899```python100import httpcore101102103class GatewayServer:104 def __init__(self, base_url):105 self.base_url = base_urlMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 106why, one step back106 self.http = httpcore.ConnectionPool()Added proxy exampleTom Christiebefore adoption87b21b0
lines 107–108why, one step back107108 async def __call__(self, scope, receive, send):Only serve http requests, ignore websocket and lifespan eventsTom Christiebefore adoption3d6aba6
line 109why, one step back109 assert scope['type'] == 'http'Added proxy exampleTom Christiebefore adoption87b21b0
lines 110–127why, one step back110 path = scope['path']111 query = scope['query_string']112 method = scope['method']113 headers = scope['headers']114115 url = self.base_url + path116 if query:117 url += '?' + query.decode()118119 async def body():120 nonlocal receive121122 while True:123 message = await receive()124 yield message.get('body', b'')125 if not message.get('more_body', False):126 break127Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 128why, one step back128 response = await self.http.request(Added proxy exampleTom Christiebefore adoption87b21b0
lines 129–152why, one step back129 method, url, headers=headers, body=body, stream=True130 )131132 await send({133 'type': 'http.response.start',134 'status': response.status_code,135 'headers': response.headers136 })137 async for data in response.stream():138 await send({139 'type': 'http.response.body',140 'body': data,141 'more_body': True142 })143 await send({'type': 'http.response.body'})144145146app = GatewayServer('http://example.org')147```148149Run with...150151```shell152uvicorn example:appStuffTom Christiebefore adoption30530d4
line 153why, one step back153```