Skip to content
Myr

README.md

This file

Path

README.md

167 lines · 4.7 KiB · Markdown

Last changed by

Update README.mdTom Christiebefore adoption0f178ca

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

line 7why, one step back
7* Support for streaming requests and responses. (Done)

Update README.mdTom Christiebefore adoption9927dc8

line 8why, one step back
8* Support for connection pooling. (Done)

Update README.mdTom Christiebefore adoption6acdfb8

line 9why, one step back
9* gzip, deflate, and brotli decoding. (Done)

Update README.mdTom Christiebefore adoption01a3c09

lines 10–11why, one step back
10* SSL verification. (Done)
11* Proxy support. (Not done)

Update README.mdTom Christiebefore adoption2ad8501

line 12why, one step back
12* HTTP/2 support. (Not done)

Update README.mdTom Christiebefore adoption0f178ca

line 13why, one step back
13* Support *both* async and sync operations. (Done)

Update READMETom Christiebefore adoption665743a

line 14why, one step back
14

Update README.mdTom Christiebefore adoption01a3c09

line 15why, one step back
15## Motivation

Update READMETom Christiebefore adoption665743a

line 16why, one step back
16

Update README.mdTom Christiebefore adoption01a3c09

line 17why, one step back
17Some 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 18why, one step back
18

Update README.mdTom Christiebefore adoption01a3c09

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

Update READMETom Christiebefore adoption665743a

line 20why, one step back
20

Update README.mdTom Christiebefore adoption01a3c09

line 21why, one step back
21## Credit

Update READMETom Christiebefore adoption665743a

line 22why, one step back
22

Update README.mdTom Christiebefore adoption01a3c09

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

Update READMETom Christiebefore adoption665743a

line 26why, one step back
26

Update README.mdTom Christiebefore adoption0d3eb0e

line 27why, one step back
27## Usage

Update README.mdTom Christiebefore adoption01a3c09

line 28why, one step back
28

Update README.mdTom Christiebefore adoption337eb70

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

StuffTom Christiebefore adoption30530d4

lines 31–33why, one step back
31```python
32import httpcore33

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

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

StuffTom Christiebefore adoption30530d4

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

Update README.mdTom Christiebefore adoption337eb70

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

StuffTom Christiebefore adoption30530d4

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

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

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

StuffTom Christiebefore adoption30530d4

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

Update README.mdTom Christiebefore adoption337eb70

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

StuffTom Christiebefore adoption30530d4

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

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

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

Update READMETom Christiebefore adoption68b80dc

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

Update README.mdTom Christiebefore adoption337eb70

line 54why, one step back
54Streaming responses:

Update READMETom Christiebefore adoption68b80dc

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

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

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

Update READMETom Christiebefore adoption68b80dc

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

Added proxy exampleTom Christiebefore adoption87b21b0

line 61why, one step back
61```

Update README.mdTom Christiebefore adoption01a3c09

line 62why, one step back
62

README tweaksTom Christiebefore adoption8e1a263

lines 63–69why, one step back
63Raw data without gzip/deflate/brotli decompression applied:
6465```python66http = httpcore.ConnectionPool()67response = await http.request(method, url, stream=True)68async for part in response.raw():69 ...

Update README for 0.2.0Tom Christiebefore adoptionbb3d452

lines 70–78why, one step back
70```
7172Thread-synchronous requests:7374```python75http = httpcore.SyncConnectionPool()76response = http.request('GET', 'http://example.com')77assert response.status_code == 20078assert response.body == b'Hello, world'

README tweaksTom Christiebefore adoption8e1a263

lines 79–80why, one step back
79```
80

Update README.mdTom Christiebefore adoption01a3c09

line 81why, one step back
81## Building a Gateway Server

Added proxy exampleTom Christiebefore adoption87b21b0

lines 82–93why, one step back
82
83The level of abstraction fits in really well if you're just writing at84the raw ASGI level. Eg. Here's an how an ASGI gateway server looks against the85API, including streaming uploads and downloads...8687```python88import httpcore899091class GatewayServer:92 def __init__(self, base_url):93 self.base_url = base_url

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

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

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

lines 98–100why, one step back
98 path = scope['path']
99 query = scope['query_string']100 method = scope['method']

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 101–102why, one step back
101 headers = [
102 (k, v) for (k, v) in scope['headers']

README tweaksTom Christiebefore adoption8e1a263

line 103why, one step back
103 if k not in (b'host', b'transfer-encoding')

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

line 104why, one step back
104 ]

Added proxy exampleTom Christiebefore adoption87b21b0

lines 105–109why, one step back
105
106 url = self.base_url + path107 if query:108 url += '?' + query.decode()109

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 110–116why, one step back
110 initial_body, more_body = await self.initial_body(receive)
111 if more_body:112 # Streaming request.113 body = self.stream_body(receive, initial_body)114 else:115 # Standard request.116 body = initial_body

Added proxy exampleTom Christiebefore adoption87b21b0

line 117why, one step back
117

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

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

Added proxy exampleTom Christiebefore adoption87b21b0

lines 119–126why, one step back
119 method, url, headers=headers, body=body, stream=True
120 )121122 await send({123 'type': 'http.response.start',124 'status': response.status_code,125 'headers': response.headers126 })

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

line 127why, one step back
127 data = b''

README tweaksTom Christiebefore adoption8e1a263

line 128why, one step back
128 async for next_data in response.raw():

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 129–136why, one step back
129 if data:
130 await send({131 'type': 'http.response.body',132 'body': data,133 'more_body': True134 })135 data = next_data136 await send({'type': 'http.response.body', 'body': data})

Added proxy exampleTom Christiebefore adoption87b21b0

line 137why, one step back
137

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

lines 138–152why, one step back
138 async def initial_body(self, receive):
139 """140 Pull the first body message off the 'receive' channel.141 Allows us to determine if we should use a streaming request or not.142 """143 message = await receive()144 body = message.get('body', b'')145 more_body = message.get('more_body', False)146 return (body, more_body)147148 async def stream_body(self, receive, initial_body):149 """150 Async iterator returning bytes for the request body.151 """152 yield initial_body

Tweak GatewayServer exampleTom Christiebefore adoptiond572f4c

lines 153–157why, one step back
153 while True:
154 message = await receive()155 yield message.get('body', b'')156 if not message.get('more_body', False):157 break

Improve GatewayService exampleTom Christiebefore adoption9d59a5a

line 158why, one step back
158

Added proxy exampleTom Christiebefore adoption87b21b0

lines 159–166why, one step back
159
160app = GatewayServer('http://example.org')161```162163Run with...164165```shell166uvicorn example:app

StuffTom Christiebefore adoption30530d4

line 167why, one step back
167```