Warn if cert / verify / trust_env are passed to client.request() (#597)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add cert and verify warnings on Client.request * Resolve typo Co-Authored-By: Florimond Manca <florimond.manca@gmail.com> * Resolve typo Co-Authored-By: Florimond Manca <florimond.manca@gmail.com> * trust_env should be set on client init * Update docs for per-Client SSL settings * Update requests compat docs
authored by Tom Christie · committed by GitHub · parent c38fd68 · Browse files · 4 files·+91 added−22 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+91 added−22 removed
- other:+91 added−22 removed
Provenance
- Commit
1e23855- Tree
15b62f1- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
c38fd68
4 files changed+91 added−22 removed
modified:docs/advanced.md+26 added-4 removed
172 unmodified lines173 174 ...175 ```176 177+When using `Client` instances, `trust_env` should be set on the client itself, rather that on the request methods:178+179+```python180+client = httpx.Client(trust_env=False)181+```182+183 ## Unix Domain Sockets184 185 You can configure an `httpx` client to connect through a unix domain socket via the `uds` parameter. This is useful when making requests to a server that is bound to a socket file rather than an IP address.186 191 unmodified lines378 ## SSL certificates379 380 When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA).381 376-### Default CA bundle382+### Changing the verification defaults383 384 By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). This is what you want in most cases, even though some advanced situations may require you to use a different set of certificates.385 380-### Using a custom CA bundle386+If you'd like to use a custom CA bundle, you can use the `verify` parameter.387 382-If you'd like to use a custom CA bundle, you can use the `verify` parameter that is available on the high-level API as well as clients. For example:383-388 ```python389 import httpx390 391 r = await httpx.get("https://example.org", verify="path/to/client.pem")392 ```393 394+You can also disable the SSL verification...395+396+```python397+import httpx398+399+r = await httpx.get("https://example.org", verify=False)400+```401+402+### SSL configuration on client instances403+404+If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.405+406+```python407+client = httpx.Client(verify=False)408+```409+410+The `client.get(...)` method and other request methods *do not* changing the SSL settings on a per-request basis. If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool.411+412 ### Making HTTPS requests to a local server413 414 When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.415 65 unmodified linesmodified:docs/compatibility.md+43 added-12 removed
2 unmodified lines3456778910910111213141516121318141520161718192021222324252627282930313233343536373839404142434445464748495051525354555657584 unmodified lines6364656634356768692 unmodified linesHTTPX aims to be compatible with the `requests` API wherever possible.
This documentation outlines places where the API differs...
## QuickStart## Request URLS
Pretty much any API mentioned in the `requests` QuickStart should be identicalto the API in our own documentation. The following exceptions apply:Accessing `response.url` will return a `URL` instance, rather than a string.Use `str(response.url)` if you need a string instance.
* `Response.url` - Returns a `URL` instance, rather than a string. Use `str(response.url)` if you need a string instance.* `httpx.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()`. - HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response. Streaming request data is made avialable with `.stream_bytes()`, `.stream_text()`, `.stream_lines()`, and `.stream_raw()`.* `.get`, `.delete`, `.head`, `.options` - These methods do not support `files`, `data`, or `json` arguments. Use `.request` if you need to need to send data using these http methods.* We don't support `response.is_ok` since the naming is ambiguous there, and might incorrectly imply an equivalence to `response.status_code == codes.OK`. Instead we provide the `response.is_error` property. Use `if not response.is_error:` instead of `if response.is_ok:`.## Status Codes
## Advanced UsageIn our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.
### requests.SessionRequests includes various synoynms for status codes that HTTPX does not support.
## Streaming responses
HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response.
For example:
```pythonasync with request.stream("GET", "https://www.example.com") as response: ...```
Within a `stream()` block request data is made available with:
* `.stream_bytes()` - Instead of `response.iter_content()`* `.stream_text()` - Instead of `response.iter_content(decode_unicode=True)`* `.stream_lines()` - Instead of `response.iter_lines()`* `.stream_raw()` - Use this instead of `response.raw`* `.read()` - Read the entire response body, making `request.text` and `response.content` available.
## SSL configuration
When using a `Client` instance, the `trust_env`, `verify`, and `cert` arguments should always be passed on client instantiation, rather than passed to the request method.
If you need more than one different SSL configuration, you should use different client instances for each SSL configuration.
## Request body on HTTP methods
The HTTP `GET`, `DELETE`, `HEAD`, and `OPTIONS` methods are specified as not supporting a request body. To stay in line with this, the `.get`, `.delete`, `.head` and `.options` functions do not support `files`, `data`, or `json` arguments.
If you really do need to send request data using these http methods you should use the generic `.request` function instead.
## Checking for 4xx/5xx responses
We don't support `response.is_ok` since the naming is ambiguous there, and might incorrectly imply an equivalence to `response.status_code == codes.OK`. Instead we provide the `response.is_error` property. Use `if not response.is_error:` instead of `if response.is_ok:`.
## Client instances
The HTTPX equivalent of `requests.Session` is `httpx.Client`.
```pythonsession = requests.Session(**kwargs)4 unmodified lines```pythonclient = httpx.Client(**kwargs)```
More detailed documentation and usage of `Client` can be found in [Advanced Usage](advanced.md).
## Mocking
If you need to mock HTTPX the same way that test utilities like `responses` and `requests-mock` does for `requests`, see [RESPX](https://github.com/lundberg/respx).modified:docs/environment_variables.md+4 added-6 removed
1 Environment Variables2 =====================3 4 The HTTPX library can be configured via environment variables.5-Environment variables are used by default. To ignore environment variables, `trust_env` has to be set `False`.6-There are two ways to set `trust_env` to disable environment variables:5+Environment variables are used by default. To ignore environment variables, `trust_env` has to be set `False`. There are two ways to set `trust_env` to disable environment variables:6 8-* On the client via `httpx.Client(trust_env=False)`9-* Per request via `client.get("<url>", trust_env=False)`7+* On the client via `httpx.Client(trust_env=False)`.8+* Using the top-level API, such as `httpx.get("<url>", trust_env=False)`.9 11-Here is a list of environment variables that HTTPX recognizes12-and what function they serve:10+Here is a list of environment variables that HTTPX recognizes and what function they serve:11 12 `HTTPX_LOG_LEVEL`13 -----------------14 140 unmodified linesmodified:httpx/client.py+18 added
221 unmodified lines222 verify: VerifyTypes = None,223 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,224 trust_env: bool = None,225 ) -> Response:226+ if cert is not None:227+ warnings.warn(228+ "Passing a 'cert' argument when making a request on a client "229+ "is due to be deprecated. Instantiate a new client instead, "230+ "passing any 'cert' arguments to the client itself."231+ )232+ if verify is not None:233+ warnings.warn(234+ "Passing a 'verify' argument when making a request on a client "235+ "is due to be deprecated. Instantiate a new client instead, "236+ "passing any 'verify' arguments to the client itself."237+ )238+ if trust_env is not None:239+ warnings.warn(240+ "Passing a 'trust_env' argument when making a request on a client "241+ "is due to be deprecated. Instantiate a new client instead, "242+ "passing any 'trust_env' argument to the client itself."243+ )244 if stream:245 warnings.warn(246 "The 'stream=True' argument is due to be deprecated. "247 "Use 'async with client.stream(method, url, ...) as response' instead."716 unmodified lines