README.md
This file
Path
README.md
148 lines · 4.4 KiB · Markdown
Last changed by
Update README.mdTom Christiebefore adoption337eb70
History of this fileTake it
StuffTom Christiebefore adoption30530d4
lines 1–2why, one step back1# HTTPCore2Update README.mdTom Christiebefore adoption01a3c09
line 3why, one step back3A low-level async HTTP library.Update READMETom Christiebefore adoption665743a
line 4why, one step back4Update README.mdTom Christiebefore adoption01a3c09
line 5why, one step back5## Proposed functionalityUpdate READMETom Christiebefore adoption665743a
line 6why, one step back6Update README.mdTom Christiebefore adoption01a3c09
lines 7–12why, one step back7* 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 back13Update README.mdTom Christiebefore adoption01a3c09
line 14why, one step back14## MotivationUpdate READMETom Christiebefore adoption665743a
line 15why, one step back15Update README.mdTom Christiebefore adoption01a3c09
line 16why, one step back16Some 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 back17Update README.mdTom Christiebefore adoption01a3c09
line 18why, one step back18The 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 back19Update README.mdTom Christiebefore adoption01a3c09
line 20why, one step back20## CreditUpdate READMETom Christiebefore adoption665743a
line 21why, one step back21Update README.mdTom Christiebefore adoption01a3c09
lines 22–24why, one step back22* 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 back25Update README.mdTom Christiebefore adoption01a3c09
lines 26–27why, one step back26## Usage27Update README.mdTom Christiebefore adoption337eb70
lines 28–29why, one step back28Making a request:29StuffTom Christiebefore adoption30530d4
lines 30–32why, one step back30```python31import httpcore32Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 33–34why, one step back33http = httpcore.ConnectionPool()34response = await http.request('GET', 'http://example.com')StuffTom Christiebefore adoption30530d4
lines 35–38why, one step back35assert response.status_code == 20036assert response.body == b'Hello, world'37```38Update README.mdTom Christiebefore adoption337eb70
line 39why, one step back39Top-level API:StuffTom Christiebefore adoption30530d4
lines 40–41why, one step back4041```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 42–43why, one step back42http = httpcore.ConnectionPool([ssl], [timeout], [limits])43response = await http.request(method, url, [headers], [body], [stream])StuffTom Christiebefore adoption30530d4
lines 44–45why, one step back44```45Update README.mdTom Christiebefore adoption337eb70
line 46why, one step back46ConnectionPool as a context-manager:StuffTom Christiebefore adoption30530d4
lines 47–48why, one step back4748```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 49–50why, one step back49async 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 back51```52Update README.mdTom Christiebefore adoption337eb70
line 53why, one step back53Streaming responses:Update READMETom Christiebefore adoption68b80dc
lines 54–55why, one step back5455```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 56–57why, one step back56http = httpcore.ConnectionPool()57response = await http.request(method, url, stream=True)Update READMETom Christiebefore adoption68b80dc
lines 58–59why, one step back58async for part in response.stream():59 ...Added proxy exampleTom Christiebefore adoption87b21b0
line 60why, one step back60```Update README.mdTom Christiebefore adoption01a3c09
lines 61–62why, one step back6162## Building a Gateway ServerAdded proxy exampleTom Christiebefore adoption87b21b0
lines 63–74why, one step back6364The 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_urlMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 75why, one step back75 self.http = httpcore.ConnectionPool()Added proxy exampleTom Christiebefore adoption87b21b0
lines 76–77why, one step back7677 async def __call__(self, scope, receive, send):Only serve http requests, ignore websocket and lifespan eventsTom Christiebefore adoption3d6aba6
line 78why, one step back78 assert scope['type'] == 'http'Added proxy exampleTom Christiebefore adoption87b21b0
lines 79–81why, one step back79 path = scope['path']80 query = scope['query_string']81 method = scope['method']Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 82–85why, one step back82 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 back8687 url = self.base_url + path88 if query:89 url += '?' + query.decode()90Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 91–97why, one step back91 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_bodyAdded proxy exampleTom Christiebefore adoption87b21b0
line 98why, one step back98Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 99why, one step back99 response = await self.http.request(Added proxy exampleTom Christiebefore adoption87b21b0
lines 100–107why, one step back100 method, url, headers=headers, body=body, stream=True101 )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 back108 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 back118Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 119–133why, one step back119 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_bodyTweak GatewayServer exampleTom Christiebefore adoptiond572f4c
lines 134–138why, one step back134 while True:135 message = await receive()136 yield message.get('body', b'')137 if not message.get('more_body', False):138 breakImprove GatewayService exampleTom Christiebefore adoption9d59a5a
line 139why, one step back139Added proxy exampleTom Christiebefore adoption87b21b0
lines 140–147why, one step back140141app = GatewayServer('http://example.org')142```143144Run with...145146```shell147uvicorn example:appStuffTom Christiebefore adoption30530d4
line 148why, one step back148```