Skip to content
Myr

README.md

This file

Path

README.md

179 lines · 6.6 KiB · Markdown

Last changed by

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

History of this file

Take it

StuffTom Christiebefore adoption30530d4

lines 1–2why, one step back
1# HTTPCore
2

Update READMETom Christiebefore adoption665743a

lines 3–34why, one step back
3I 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.

Update READMETom Christiebefore adoption1124cc4

lines 35–38why, one step back
35* A big chunk of this is implemented now. Streaming requests and responses are
36 supported. Connection pooling is stubbed-out at the moment, but all in place.37 GZip decoding is stubbed-out at the moment, but easy to do, and all in place.38 Theres various stuff around connection closing and error handling still to finesse.

Update READMETom Christiebefore adoption665743a

lines 39–62why, one step back
39* Take a quick look over the test cases or the package itself to get a feel
40 for it. It's all type annotated, and should be easy to find your way around.41* I've not yet added corresponding sync API points to the implementation, but42 they will come.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.62

StuffTom Christiebefore adoption30530d4

lines 63–65why, one step back
63```python
64import httpcore65

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 66–67why, one step back
66http = httpcore.ConnectionPool()
67response = await http.request('GET', 'http://example.com')

StuffTom Christiebefore adoption30530d4

lines 68–71why, one step back
68assert response.status_code == 200
69assert response.body == b'Hello, world'70```71

Update READMETom Christiebefore adoption68b80dc

line 72why, one step back
72Top-level API...

StuffTom Christiebefore adoption30530d4

lines 73–74why, one step back
73
74```python

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 75–76why, one step back
75http = httpcore.ConnectionPool([ssl], [timeout], [limits])
76response = await http.request(method, url, [headers], [body], [stream])

StuffTom Christiebefore adoption30530d4

lines 77–78why, one step back
77```
78

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 79why, one step back
79ConnectionPool as a context-manager...

StuffTom Christiebefore adoption30530d4

lines 80–81why, one step back
80
81```python

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 82–83why, one step back
82async 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 back
84```
8586Streaming...8788```python

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 89–90why, one step back
89http = httpcore.ConnectionPool()
90response = await http.request(method, url, stream=True)

Update READMETom Christiebefore adoption68b80dc

lines 91–92why, one step back
91async for part in response.stream():
92 ...

Added proxy exampleTom Christiebefore adoption87b21b0

lines 93–105why, one step back
93```
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_url

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 106why, one step back
106 self.http = httpcore.ConnectionPool()

Added proxy exampleTom Christiebefore adoption87b21b0

lines 107–108why, one step back
107
108 async def __call__(self, scope, receive, send):

Only serve http requests, ignore websocket and lifespan eventsTom Christiebefore adoption3d6aba6

line 109why, one step back
109 assert scope['type'] == 'http'

Added proxy exampleTom Christiebefore adoption87b21b0

lines 110–112why, one step back
110 path = scope['path']
111 query = scope['query_string']112 method = scope['method']

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 113–116why, one step back
113 headers = [
114 (k, v) for (k, v) in scope['headers']115 if k not in (b'host', b'content-length', b'transfer-encoding')116 ]

Added proxy exampleTom Christiebefore adoption87b21b0

lines 117–121why, one step back
117
118 url = self.base_url + path119 if query:120 url += '?' + query.decode()121

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 122–128why, one step back
122 initial_body, more_body = await self.initial_body(receive)
123 if more_body:124 # Streaming request.125 body = self.stream_body(receive, initial_body)126 else:127 # Standard request.128 body = initial_body

Added proxy exampleTom Christiebefore adoption87b21b0

line 129why, one step back
129

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 130why, one step back
130 response = await self.http.request(

Added proxy exampleTom Christiebefore adoption87b21b0

lines 131–138why, one step back
131 method, url, headers=headers, body=body, stream=True
132 )133134 await send({135 'type': 'http.response.start',136 'status': response.status_code,137 'headers': response.headers138 })

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 139–148why, one step back
139 data = b''
140 async for next_data in response.stream():141 if data:142 await send({143 'type': 'http.response.body',144 'body': data,145 'more_body': True146 })147 data = next_data148 await send({'type': 'http.response.body', 'body': data})

Added proxy exampleTom Christiebefore adoption87b21b0

line 149why, one step back
149

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 150–164why, one step back
150 async def initial_body(self, receive):
151 """152 Pull the first body message off the 'receive' channel.153 Allows us to determine if we should use a streaming request or not.154 """155 message = await receive()156 body = message.get('body', b'')157 more_body = message.get('more_body', False)158 return (body, more_body)159160 async def stream_body(self, receive, initial_body):161 """162 Async iterator returning bytes for the request body.163 """164 yield initial_body

Tweak GatewayServer exampleTom Christiebefore adoptiond572f4c

lines 165–169why, one step back
165 while True:
166 message = await receive()167 yield message.get('body', b'')168 if not message.get('more_body', False):169 break

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

line 170why, one step back
170

Added proxy exampleTom Christiebefore adoption87b21b0

lines 171–178why, one step back
171
172app = GatewayServer('http://example.org')173```174175Run with...176177```shell178uvicorn example:app

StuffTom Christiebefore adoption30530d4

line 179why, one step back
179```