Skip to content
Myr

Tweaks (#89)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Close redirect responses * History should not be reverse-order * History should not be reverse-order * Docs updates

authored by Tom Christie · committed by GitHub · parent 3ca9a74 · Browse files · 4 files+74 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+74 added−3 removed

  • tests:+4 added−0 removed
  • other:+70 added−3 removed

4 files+74 added−3 removed

modified:docs/compatibility.md+15 added-1 removed
modified:docs/quickstart.md+54 added-1 removed
287 unmodified lines
288{'cookies': {'peanut': 'butter'}}289```290291Cookies are returned in a `Cookies` instance, which is a dict-like data structure292but with additional API for accessing cookies by their domain or path.292with additional API for accessing cookies by their domain or path.293294```python295>>> cookies = http3.Cookies()296>>> cookies.set('cookie_on_domain', 'hello, there!', domain='httpbin.org')
1 unmodified line
298>>> r = http3.get('http://httpbin.org/cookies', cookies=cookies)299>>> r.json()300{'cookies': {'cookie_on_domain': 'hello, there!'}}301```302303## Redirection and History304305By default HTTP3 will follow redirects for anything except `HEAD` requests.306307The `history` property of the response can be used to inspect any followed redirects.308It contains a list of all any redirect responses that were followed, in the order309in which they were made.310311For example, GitHub redirects all HTTP requests to HTTPS.312313```python314>>> r = http3.get('http://github.com/')315>>> r.url316URL('https://github.com/')317>>> r.status_code318<StatusCode.OK: 200>319>>> r.history320[<Response [301]>]321```322323You can modify the default redirection handling with the allow_redirects parameter:324325```python326>>> r = http3.get('http://github.com/', allow_redirects=False)327>>> r.status_code328301329>>> r.history330[]331```332333If you’re making a `HEAD` request, you can use this to enable redirection:334335```python336>>> r = http3.head('http://github.com/', allow_redirects=True)337>>> r.url338'https://github.com/'339>>> r.history340[<Response [301]>]341```342343## Timeouts344345HTTP3 defaults to including reasonable timeouts for all network operations,346meaning that if a connection is not properly established then it should always347raise an error rather than hanging indefinitely.348349The default timeout for network inactivity is five seconds. You can modify the350value to be more or less strict:351352```python353>>> http3.get('https://github.com/', timeout=0.001)354```
modified:http3/client.py+1 added-1 removed
147 unmodified lines
148 try:149 assert isinstance(response, AsyncResponse)150 response.history = list(history)151 self.cookies.extract_cookies(response)152 history = [response] + history152 history = history + [response]153154 if allow_redirects and response.is_redirect:155 request = self.build_redirect_request(request, response)156 else:
680 unmodified lines
modified:tests/client/test_redirects.py+4 added
166 unmodified lines
167 response = await client.get("https://example.org/multiple_redirects?count=20")168 assert response.status_code == codes.OK169 assert response.url == URL("https://example.org/multiple_redirects")170 assert len(response.history) == 20171 assert response.history[0].url == URL("https://example.org/multiple_redirects?count=20")172 assert response.history[1].url == URL("https://example.org/multiple_redirects?count=19")173 assert len(response.history[0].history) == 0174 assert len(response.history[1].history) == 1175176177@pytest.mark.asyncio178async def test_too_many_redirects():
72 unmodified lines