Skip to content

Commit b9950a0

Browse files
Convert client snippets to use context-managed syntax
1 parent f5239f7 commit b9950a0

File tree

5 files changed

+62
-54
lines changed

5 files changed

+62
-54
lines changed

docs/advanced.md

+43-36
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Using a Client instance to make requests will give you HTTP connection pooling,
66
will provide cookie persistence, and allows you to apply configuration across
77
all outgoing requests.
88

9-
A Client instance is equivalent to a Session instance in `requests`.
10-
119
!!! hint
1210
A Client instance is equivalent to a Session instance in `requests`.
1311

@@ -61,10 +59,10 @@ app = Flask(__name__)
6159
def hello():
6260
return "Hello World!"
6361

64-
client = httpx.Client(app=app)
65-
r = client.get('http://example/')
66-
assert r.status_code == 200
67-
assert r.text == "Hello World!"
62+
with httpx.Client(app=app) as client:
63+
r = client.get('http://example/')
64+
assert r.status_code == 200
65+
assert r.text == "Hello World!"
6866
```
6967

7068
For some more complex cases you might need to customize the WSGI or ASGI
@@ -79,7 +77,8 @@ For example:
7977
```python
8078
# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".
8179
dispatch = httpx.dispatch.WSGIDispatch(app=app, remote_addr="1.2.3.4")
82-
client = httpx.Client(dispatch=dispatch)
80+
with httpx.Client(dispatch=dispatch) as client:
81+
...
8382
```
8483

8584
## Build Request
@@ -88,10 +87,12 @@ You can use `Client.build_request()` to build a request and
8887
make modifications before sending the request.
8988

9089
```python
91-
>>> client = httpx.Client()
92-
>>> req = client.build_request("OPTIONS", "https://example.com")
93-
>>> req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS
94-
>>> client.send(r)
90+
>>> with httpx.Client() as client:
91+
... req = client.build_request("OPTIONS", "https://example.com")
92+
... req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS
93+
... r = client.send(req)
94+
...
95+
>>> r
9596
<Response [200 OK]>
9697
```
9798

@@ -102,11 +103,11 @@ One can set the version of the HTTP protocol for the client in case you want to
102103
For example:
103104

104105
```python
105-
h11_client = httpx.Client(http_versions=["HTTP/1.1"])
106-
h11_response = h11_client.get("https://myserver.com")
106+
with httpx.Client(http_versions=["HTTP/1.1"]) as h11_client:
107+
h11_response = h11_client.get("https://myserver.com")
107108

108-
h2_client = httpx.Client(http_versions=["HTTP/2"])
109-
h2_response = h2_client.get("https://myserver.com")
109+
with httpx.Client(http_versions=["HTTP/2"]) as h2_client:
110+
h2_response = h2_client.get("https://myserver.com")
110111
```
111112

112113
## .netrc Support
@@ -149,24 +150,31 @@ For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS
149150
to `http://127.0.0.1:3081` your `proxies` config would look like this:
150151

151152
```python
152-
>>> client = httpx.Client(proxies={
153-
"http": "http://127.0.0.1:3080",
154-
"https": "http://127.0.0.1:3081"
155-
})
153+
>>> proxies = {
154+
... "http": "http://127.0.0.1:3080",
155+
... "https": "http://127.0.0.1:3081"
156+
... }
157+
>>> with httpx.Client(proxies=proxies) as client:
158+
... ...
156159
```
157160

158161
Proxies can be configured for a specific scheme and host, all schemes of a host,
159162
all hosts for a scheme, or for all requests. When determining which proxy configuration
160163
to use for a given request this same order is used.
161164

162165
```python
163-
>>> client = httpx.Client(proxies={
164-
"http://example.com": "...", # Host+Scheme
165-
"all://example.com": "...", # Host
166-
"http": "...", # Scheme
167-
"all": "...", # All
168-
})
169-
>>> client = httpx.Client(proxies="...") # Shortcut for 'all'
166+
>>> proxies = {
167+
... "http://example.com": "...", # Host+Scheme
168+
... "all://example.com": "...", # Host
169+
... "http": "...", # Scheme
170+
... "all": "...", # All
171+
... }
172+
>>> with httpx.Client(proxies=proxies) as client:
173+
... ...
174+
...
175+
>>> proxy = "..." # Shortcut for {'all': '...'}
176+
>>> with httpx.Client(proxies=proxy) as client:
177+
... ...
170178
```
171179

172180
!!! warning
@@ -185,10 +193,9 @@ proxy = httpx.HTTPProxy(
185193
proxy_url="https://127.0.0.1",
186194
proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY
187195
)
188-
client = httpx.Client(proxies=proxy)
189-
190-
# This request will be tunneled instead of forwarded.
191-
client.get("http://example.com")
196+
with httpx.Client(proxies=proxy) as client:
197+
# This request will be tunneled instead of forwarded.
198+
r = client.get("http://example.com")
192199
```
193200

194201

@@ -213,15 +220,15 @@ You can set timeouts on two levels:
213220
httpx.get('http://example.com/api/v1/example', timeout=5)
214221

215222
# Or, with a client:
216-
client = httpx.Client()
217-
client.get("http://example.com/api/v1/example", timeout=5)
223+
with httpx.Client() as client:
224+
client.get("http://example.com/api/v1/example", timeout=5)
218225
```
219226

220227
- On a client instance, which results in the given `timeout` being used as a default for requests made with this client:
221228

222229
```python
223-
client = httpx.Client(timeout=5)
224-
client.get('http://example.com/api/v1/example')
230+
with httpx.Client(timeout=5) as client:
231+
client.get('http://example.com/api/v1/example')
225232
```
226233

227234
Besides, you can pass timeouts in two forms:
@@ -252,8 +259,8 @@ url = "http://example.com/api/v1/delay/10"
252259
httpx.get(url, timeout=None) # Times out after 5s
253260

254261

255-
client = httpx.Client(timeout=None)
256-
client.get(url) # Does not timeout, returns after 10s
262+
with httpx.Client(timeout=None) as client:
263+
client.get(url) # Does not timeout, returns after 10s
257264

258265

259266
timeout = httpx.TimeoutConfig(

docs/api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*An HTTP client, with connection pooling, HTTP/2, redirects, cookie persistence, etc.*
2424

2525
```python
26-
>>> client = httpx.Client()
27-
>>> response = client.get('https://example.org')
26+
>>> with httpx.Client() as client:
27+
... response = client.get('https://example.org')
2828
```
2929

3030
* `def __init__([auth], [params], [headers], [cookies], [verify], [cert], [timeout], [pool_limits], [max_redirects], [app], [dispatch])`

docs/compatibility.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ session = requests.Session(**kwargs)
2727
is equivalent to
2828

2929
```python
30-
client = httpx.Client(**kwargs)
30+
with httpx.Client(**kwargs) as client:
31+
...
3132
```
3233

3334
More detailed documentation and usage of `Client` can be found in [Advanced Usage](advanced.md).

docs/environment_variables.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ Example:
2626

2727
```python
2828
# test_script.py
29-
3029
import httpx
31-
client = httpx.Client()
32-
client.get("https://google.com")
30+
31+
with httpx.Client() as client:
32+
r = client.get("https://google.com")
3333
```
3434

3535
```console
@@ -64,10 +64,10 @@ Example:
6464

6565
```python
6666
# test_script.py
67-
6867
import httpx
69-
client = httpx.Client()
70-
client.get("https://google.com")
68+
69+
with httpx.Client() as client:
70+
r = client.get("https://google.com")
7171
```
7272

7373
```console

docs/parallel.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ control the authentication or dispatch behavior for all requests within the
4848
block.
4949

5050
```python
51-
>>> client = httpx.Client()
52-
>>> with client.parallel() as parallel:
53-
>>> ...
51+
>>> with httpx.Client() as client:
52+
... with client.parallel() as parallel:
53+
... ...
5454
```
5555

5656
## Async parallel requests
@@ -59,12 +59,12 @@ If you're working within an async framework, then you'll want to use a fully
5959
async API for making requests.
6060

6161
```python
62-
>>> client = httpx.AsyncClient()
63-
>>> async with client.parallel() as parallel:
64-
>>> pending_one = await parallel.get('https://example.com/1')
65-
>>> pending_two = await parallel.get('https://example.com/2')
66-
>>> response_one = await pending_one.get_response()
67-
>>> response_two = await pending_two.get_response()
62+
>>> async with httpx.AsyncClient() as client:
63+
... async with client.parallel() as parallel:
64+
... pending_one = await parallel.get('https://example.com/1')
65+
... pending_two = await parallel.get('https://example.com/2')
66+
... response_one = await pending_one.get_response()
67+
... response_two = await pending_two.get_response()
6868
```
6969

7070
See [the Async Client documentation](async.md) for more details.

0 commit comments

Comments
 (0)