Skip to content
Myr

README.md

This file

Path

README.md

148 lines · 4.4 KiB · Markdown

Last changed by

Update README.mdTom Christiebefore adoption337eb70

History of this file

Take it

StuffTom Christiebefore adoption30530d4

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

Update README.mdTom Christiebefore adoption01a3c09

line 3why, one step back
3A low-level async HTTP library.

Update READMETom Christiebefore adoption665743a

line 4why, one step back
4

Update README.mdTom Christiebefore adoption01a3c09

line 5why, one step back
5## Proposed functionality

Update READMETom Christiebefore adoption665743a

line 6why, one step back
6

Update README.mdTom Christiebefore adoption01a3c09

lines 7–12why, one step back
7* Support for streaming requests and responses. (Done)
8* Support for connection pooling. (Not done, but structure in place)9* gzip, deflate, and brotli decoding. (Not done, but structure in place)10* SSL verification. (Done)11* Proxy support. (Not done)12* Support *both* async and sync operations. (Sync will be lightweight shim on top of async. Not done.)

Update READMETom Christiebefore adoption665743a

line 13why, one step back
13

Update README.mdTom Christiebefore adoption01a3c09

line 14why, one step back
14## Motivation

Update READMETom Christiebefore adoption665743a

line 15why, one step back
15

Update README.mdTom Christiebefore adoption01a3c09

line 16why, one step back
16Some of the trickier remaining issues on `requests-async` such as request/response streaming, connection pooling, proxy support, would require a fully async variant of urllib3. I considered and started work on a straight port of `urllib3-async`, but having started to dive into it, my judgement is that a from-scratch implementation will be less overall work to achieve.

Update READMETom Christiebefore adoption665743a

line 17why, one step back
17

Update README.mdTom Christiebefore adoption01a3c09

line 18why, one step back
18The intent is that this library could be the low-level implementation, that `requests-async` would then wrap up.

Update READMETom Christiebefore adoption665743a

line 19why, one step back
19

Update README.mdTom Christiebefore adoption01a3c09

line 20why, one step back
20## Credit

Update READMETom Christiebefore adoption665743a

line 21why, one step back
21

Update README.mdTom Christiebefore adoption01a3c09

lines 22–24why, one step back
22* Some inspiration from the design-work of `urllib3`, but redone from scratch, and built as an async-first library.
23* Dependant on the absolutely excellent `h11` package.24* Uses the `certifi` package for the default SSL verification.

Update READMETom Christiebefore adoption665743a

line 25why, one step back
25

Update README.mdTom Christiebefore adoption01a3c09

lines 26–27why, one step back
26## Usage
27

Update README.mdTom Christiebefore adoption337eb70

lines 28–29why, one step back
28Making a request:
29

StuffTom Christiebefore adoption30530d4

lines 30–32why, one step back
30```python
31import httpcore32

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

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

StuffTom Christiebefore adoption30530d4

lines 35–38why, one step back
35assert response.status_code == 200
36assert response.body == b'Hello, world'37```38

Update README.mdTom Christiebefore adoption337eb70

line 39why, one step back
39Top-level API:

StuffTom Christiebefore adoption30530d4

lines 40–41why, one step back
40
41```python

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

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

StuffTom Christiebefore adoption30530d4

lines 44–45why, one step back
44```
45

Update README.mdTom Christiebefore adoption337eb70

line 46why, one step back
46ConnectionPool as a context-manager:

StuffTom Christiebefore adoption30530d4

lines 47–48why, one step back
47
48```python

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

lines 49–50why, one step back
49async with httpcore.ConnectionPool([ssl], [timeout], [limits]) as http:
50 response = await http.request(method, url, [headers], [body], [stream])

Update READMETom Christiebefore adoption68b80dc

lines 51–52why, one step back
51```
52

Update README.mdTom Christiebefore adoption337eb70

line 53why, one step back
53Streaming responses:

Update READMETom Christiebefore adoption68b80dc

lines 54–55why, one step back
54
55```python

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

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

Update READMETom Christiebefore adoption68b80dc

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

Added proxy exampleTom Christiebefore adoption87b21b0

line 60why, one step back
60```

Update README.mdTom Christiebefore adoption01a3c09

lines 61–62why, one step back
61
62## Building a Gateway Server

Added proxy exampleTom Christiebefore adoption87b21b0

lines 63–74why, one step back
63
64The level of abstraction fits in really well if you're just writing at65the raw ASGI level. Eg. Here's an how an ASGI gateway server looks against the66API, including streaming uploads and downloads...6768```python69import httpcore707172class GatewayServer:73 def __init__(self, base_url):74 self.base_url = base_url

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

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

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

lines 79–81why, one step back
79 path = scope['path']
80 query = scope['query_string']81 method = scope['method']

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 82–85why, one step back
82 headers = [
83 (k, v) for (k, v) in scope['headers']84 if k not in (b'host', b'content-length', b'transfer-encoding')85 ]

Added proxy exampleTom Christiebefore adoption87b21b0

lines 86–90why, one step back
86
87 url = self.base_url + path88 if query:89 url += '?' + query.decode()90

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 91–97why, one step back
91 initial_body, more_body = await self.initial_body(receive)
92 if more_body:93 # Streaming request.94 body = self.stream_body(receive, initial_body)95 else:96 # Standard request.97 body = initial_body

Added proxy exampleTom Christiebefore adoption87b21b0

line 98why, one step back
98

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

lines 100–107why, one step back
100 method, url, headers=headers, body=body, stream=True
101 )102103 await send({104 'type': 'http.response.start',105 'status': response.status_code,106 'headers': response.headers107 })

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 108–117why, one step back
108 data = b''
109 async for next_data in response.stream():110 if data:111 await send({112 'type': 'http.response.body',113 'body': data,114 'more_body': True115 })116 data = next_data117 await send({'type': 'http.response.body', 'body': data})

Added proxy exampleTom Christiebefore adoption87b21b0

line 118why, one step back
118

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 119–133why, one step back
119 async def initial_body(self, receive):
120 """121 Pull the first body message off the 'receive' channel.122 Allows us to determine if we should use a streaming request or not.123 """124 message = await receive()125 body = message.get('body', b'')126 more_body = message.get('more_body', False)127 return (body, more_body)128129 async def stream_body(self, receive, initial_body):130 """131 Async iterator returning bytes for the request body.132 """133 yield initial_body

Tweak GatewayServer exampleTom Christiebefore adoptiond572f4c

lines 134–138why, one step back
134 while True:
135 message = await receive()136 yield message.get('body', b'')137 if not message.get('more_body', False):138 break

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

line 139why, one step back
139

Added proxy exampleTom Christiebefore adoption87b21b0

lines 140–147why, one step back
140
141app = GatewayServer('http://example.org')142```143144Run with...145146```shell147uvicorn example:app

StuffTom Christiebefore adoption30530d4

line 148why, one step back
148```