DEFAULT_LIMITS,
DEFAULT_MAX_REDIRECTS,
DEFAULT_TIMEOUT_CONFIG,
- UNSET,
Limits,
Proxy,
Timeout,
- UnsetType,
)
from ._decoders import SUPPORTED_DECODERS
from ._exceptions import (
U = typing.TypeVar("U", bound="AsyncClient")
+class UseClientDefault:
+ """
+ For some parameters such as `auth=...` and `timeout=...` we need to be able
+ to indicate the default "unset" state, in a way that is distinctly different
+ to using `None`.
+
+ The default "unset" state indicates that whatever default is set on the
+ client should be used. This is different to setting `None`, which
+ explicitly disables the parameter, possibly overriding a client default.
+
+ For example we use `timeout=USE_CLIENT_DEFAULT` in the `request()` signature.
+ Omitting the `timeout` parameter will send a request using whatever default
+ timeout has been configured on the client. Including `timeout=None` will
+ ensure no timeout is used.
+
+ Note that user code shouldn't need to use the `USE_CLIENT_DEFAULT` constant,
+ but it is used internally when a parameter is not included.
+ """
+
+ pass # pragma: nocover
+
+
+USE_CLIENT_DEFAULT = UseClientDefault()
+
+
logger = get_logger(__name__)
USER_AGENT = f"python-httpx/{__version__}"
raise TypeError(f'Invalid "auth" argument: {auth!r}')
def _build_request_auth(
- self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET
+ self,
+ request: Request,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Auth:
- auth = self._auth if isinstance(auth, UnsetType) else self._build_auth(auth)
+ auth = (
+ self._auth if isinstance(auth, UseClientDefault) else self._build_auth(auth)
+ )
if auth is not None:
return auth
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Build and send a request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> typing.Iterator[Response]:
"""
Alternative to `httpx.request()` that streams the response body
request: Request,
*,
stream: bool = False,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a request.
raise RuntimeError("Cannot send a request, as the client has been closed.")
self._state = ClientState.OPENED
- timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)
+ timeout = (
+ self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)
+ )
auth = self._build_request_auth(request, auth)
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `GET` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send an `OPTIONS` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `HEAD` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `POST` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `PUT` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `PATCH` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `DELETE` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Build and send a request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> typing.AsyncIterator[Response]:
"""
Alternative to `httpx.request()` that streams the response body
request: Request,
*,
stream: bool = False,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a request.
raise RuntimeError("Cannot send a request, as the client has been closed.")
self._state = ClientState.OPENED
- timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)
+ timeout = (
+ self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)
+ )
auth = self._build_request_auth(request, auth)
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `GET` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send an `OPTIONS` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `HEAD` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `POST` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `PUT` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `PATCH` request.
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
- auth: typing.Union[AuthTypes, UnsetType] = UNSET,
+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,
) -> Response:
"""
Send a `DELETE` request.