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
Provenance
- Commit
7c6fb5c- Tree
4d530de- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
3ca9a74
4 files changed+74 added−3 removed
modified:docs/compatibility.md+15 added-1 removed
2 unmodified lines345677891011121314151617181920212 unmodified linesHTTP3 aims to be compatible with the `requests` API wherever possible.
This documentation outlines places where the API differs...
**TODO**## QuickStart
Pretty much all the API mentioned in the `requests` QuickStart should be identicalto the API in our own documentation. The following exceptions apply:
* `Response.url` - Returns a `URL` instance, rather than a string. Use `str(response.url)` if you need a string instance.* `Response.status_code` - Returns an integer, which may be a `StatusCode` IntEnum. This has the same behaviour as any other integer, except that it provides more information in the instance representation.* `http3.codes` - In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`,but also provide lower-cased versions for API compatibility with `requests`.* `stream=True`. - Streaming responses provide the `.stream()` and `.raw()` byte iterator interfaces, rather than the `.iter_content()` method and the `.raw` socket interface.
## Advanced Usage
!!! warning TODOmodified:docs/quickstart.md+54 added-1 removed
287 unmodified lines288 {'cookies': {'peanut': 'butter'}}289 ```290 291 Cookies are returned in a `Cookies` instance, which is a dict-like data structure292-but with additional API for accessing cookies by their domain or path.292+with additional API for accessing cookies by their domain or path.293 294 ```python295 >>> cookies = http3.Cookies()296 >>> cookies.set('cookie_on_domain', 'hello, there!', domain='httpbin.org')1 unmodified line298 >>> r = http3.get('http://httpbin.org/cookies', cookies=cookies)299 >>> r.json()300 {'cookies': {'cookie_on_domain': 'hello, there!'}}301 ```302+303+## Redirection and History304+305+By default HTTP3 will follow redirects for anything except `HEAD` requests.306+307+The `history` property of the response can be used to inspect any followed redirects.308+It contains a list of all any redirect responses that were followed, in the order309+in which they were made.310+311+For example, GitHub redirects all HTTP requests to HTTPS.312+313+```python314+>>> r = http3.get('http://github.com/')315+>>> r.url316+URL('https://github.com/')317+>>> r.status_code318+<StatusCode.OK: 200>319+>>> r.history320+[<Response [301]>]321+```322+323+You can modify the default redirection handling with the allow_redirects parameter:324+325+```python326+>>> r = http3.get('http://github.com/', allow_redirects=False)327+>>> r.status_code328+301329+>>> r.history330+[]331+```332+333+If you’re making a `HEAD` request, you can use this to enable redirection:334+335+```python336+>>> r = http3.head('http://github.com/', allow_redirects=True)337+>>> r.url338+'https://github.com/'339+>>> r.history340+[<Response [301]>]341+```342+343+## Timeouts344+345+HTTP3 defaults to including reasonable timeouts for all network operations,346+meaning that if a connection is not properly established then it should always347+raise an error rather than hanging indefinitely.348+349+The default timeout for network inactivity is five seconds. You can modify the350+value to be more or less strict:351+352+```python353+>>> http3.get('https://github.com/', timeout=0.001)354+```modified:http3/client.py+1 added-1 removed
147 unmodified lines148 try:149 assert isinstance(response, AsyncResponse)150 response.history = list(history)151 self.cookies.extract_cookies(response)152- history = [response] + history152+ history = history + [response]153 154 if allow_redirects and response.is_redirect:155 request = self.build_redirect_request(request, response)156 else:680 unmodified linesmodified:tests/client/test_redirects.py+4 added
166 unmodified lines167 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) == 1175 176 177 @pytest.mark.asyncio178 async def test_too_many_redirects():72 unmodified lines