Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions httpx/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ def _build_auth_header(self, username: str | bytes, password: str | bytes) -> st


class DigestAuth(Auth):
"""
Allows the 'auth' argument to be passed as a (username, password) pair,
and uses HTTP Digest authentication.
"""

_ALGORITHM_TO_HASH_FUNCTION: dict[str, typing.Callable[[bytes], _Hash]] = {
"MD5": hashlib.md5,
"MD5-SESS": hashlib.md5,
Expand Down
30 changes: 30 additions & 0 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def create_ssl_context(
cert: CertTypes | None = None,
trust_env: bool = True,
) -> ssl.SSLContext:
"""
Create an SSL context, as used by clients and transports to verify
server certificates.

**Parameters:**

* **verify** - Either `True` to use an SSL context with the default
CA bundle, `False` to disable verification, or an instance of
`ssl.SSLContext` to use a custom context.
* **cert** - *(optional)* An SSL certificate used by the client when
connecting to the server, to provide mutual TLS authentication.
* **trust_env** - *(optional)* Enables or disables usage of environment
variables for configuration.
"""
import ssl
import warnings

Expand Down Expand Up @@ -199,6 +213,22 @@ def __repr__(self) -> str:


class Proxy:
"""
Configuration for a proxy to route requests through.

**Parameters:**

* **url** - The proxy URL, which must use one of the `"http"`, `"https"`,
`"socks5"`, or `"socks5h"` schemes. Any authentication credentials
included in the URL are treated as the proxy authentication.
* **ssl_context** - *(optional)* An SSL context to use when connecting
to the proxy.
* **auth** - *(optional)* A (username, password) pair to use for
proxy authentication.
* **headers** - *(optional)* Headers to include when connecting
to the proxy.
"""

def __init__(
self,
url: URL | str,
Expand Down
5 changes: 5 additions & 0 deletions httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@


class ByteStream(AsyncByteStream, SyncByteStream):
"""
A byte stream wrapping an in-memory `bytes` value, that can be used
as either a synchronous or an asynchronous stream.
"""

def __init__(self, stream: bytes) -> None:
self._stream = stream

Expand Down
14 changes: 14 additions & 0 deletions httpx/_transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@


class BaseTransport:
"""
Base class for all synchronous transports.

To implement a custom transport, subclass `BaseTransport` and override
the `.handle_request()` method.
"""

def __enter__(self: T) -> T:
return self

Expand Down Expand Up @@ -63,6 +70,13 @@ def close(self) -> None:


class AsyncBaseTransport:
"""
Base class for all asynchronous transports.

To implement a custom transport, subclass `AsyncBaseTransport` and
override the `.handle_async_request()` method.
"""

async def __aenter__(self: A) -> A:
return self

Expand Down
78 changes: 78 additions & 0 deletions httpx/_transports/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,45 @@ def close(self) -> None:


class HTTPTransport(BaseTransport):
"""
A synchronous transport that handles sending requests over the network,
using a connection pool.

This is the default transport used by `Client`. It may also be
instantiated directly, in order to use any additional configuration
that is specific to this transport.

```python
transport = httpx.HTTPTransport(retries=1)
client = httpx.Client(transport=transport)
```

**Parameters:**

* **verify** - *(optional)* Either `True` to use an SSL context with the
default CA bundle, `False` to disable verification, or an instance of
`ssl.SSLContext` to use a custom context.
* **cert** - *(optional)* An SSL certificate used by the client when
connecting to the server, to provide mutual TLS authentication.
* **trust_env** - *(optional)* Enables or disables usage of environment
variables for configuration.
* **http1** - *(optional)* A boolean indicating if HTTP/1.1 support
should be enabled. Defaults to `True`.
* **http2** - *(optional)* A boolean indicating if HTTP/2 support
should be enabled. Defaults to `False`.
* **limits** - *(optional)* The limits configuration to use.
* **proxy** - *(optional)* A proxy URL where all the traffic should
be routed.
* **uds** - *(optional)* A path to a Unix domain socket to use
instead of a TCP connection.
* **local_address** - *(optional)* A local IP address to use for
outgoing connections.
* **retries** - *(optional)* The maximum number of times to retry
a failed connection attempt.
* **socket_options** - *(optional)* Additional socket options
to apply to outgoing connections.
"""

def __init__(
self,
verify: ssl.SSLContext | str | bool = True,
Expand Down Expand Up @@ -277,6 +316,45 @@ async def aclose(self) -> None:


class AsyncHTTPTransport(AsyncBaseTransport):
"""
An asynchronous transport that handles sending requests over the network,
using a connection pool.

This is the default transport used by `AsyncClient`. It may also be
instantiated directly, in order to use any additional configuration
that is specific to this transport.

```python
transport = httpx.AsyncHTTPTransport(retries=1)
client = httpx.AsyncClient(transport=transport)
```

**Parameters:**

* **verify** - *(optional)* Either `True` to use an SSL context with the
default CA bundle, `False` to disable verification, or an instance of
`ssl.SSLContext` to use a custom context.
* **cert** - *(optional)* An SSL certificate used by the client when
connecting to the server, to provide mutual TLS authentication.
* **trust_env** - *(optional)* Enables or disables usage of environment
variables for configuration.
* **http1** - *(optional)* A boolean indicating if HTTP/1.1 support
should be enabled. Defaults to `True`.
* **http2** - *(optional)* A boolean indicating if HTTP/2 support
should be enabled. Defaults to `False`.
* **limits** - *(optional)* The limits configuration to use.
* **proxy** - *(optional)* A proxy URL where all the traffic should
be routed.
* **uds** - *(optional)* A path to a Unix domain socket to use
instead of a TCP connection.
* **local_address** - *(optional)* A local IP address to use for
outgoing connections.
* **retries** - *(optional)* The maximum number of times to retry
a failed connection attempt.
* **socket_options** - *(optional)* Additional socket options
to apply to outgoing connections.
"""

def __init__(
self,
verify: ssl.SSLContext | str | bool = True,
Expand Down
17 changes: 17 additions & 0 deletions httpx/_transports/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@


class MockTransport(AsyncBaseTransport, BaseTransport):
"""
A transport that maps requests to responses, using a custom handler,
without making any actual network requests.

Useful for testing, stubbing, or otherwise mocking out network
behaviour. The handler may be either a function or an async function,
for use with `Client` or `AsyncClient` respectively.

```python
def handler(request):
return httpx.Response(200, json={"text": "Hello, world!"})

transport = httpx.MockTransport(handler)
client = httpx.Client(transport=transport)
```
"""

def __init__(self, handler: SyncHandler | AsyncHandler) -> None:
self.handler = handler

Expand Down
16 changes: 16 additions & 0 deletions httpx/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@


class SyncByteStream:
"""
A synchronous byte stream, such as the stream used for the content
of a request or a response.

To implement a custom stream, subclass `SyncByteStream` and override
the `.__iter__()` method.
"""

def __iter__(self) -> Iterator[bytes]:
raise NotImplementedError(
"The '__iter__' method must be implemented."
Expand All @@ -104,6 +112,14 @@ def close(self) -> None:


class AsyncByteStream:
"""
An asynchronous byte stream, such as the stream used for the content
of a request or a response.

To implement a custom stream, subclass `AsyncByteStream` and override
the `.__aiter__()` method.
"""

async def __aiter__(self) -> AsyncIterator[bytes]:
raise NotImplementedError(
"The '__aiter__' method must be implemented."
Expand Down
21 changes: 21 additions & 0 deletions tests/test_exported_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,24 @@ def test_all_imports_are_exported() -> None:
),
key=str.casefold,
)


def test_public_api_docstrings() -> None:
# The transports, streams, and configuration classes that make up the
# public API should all include docstrings, so that they are properly
# documented in IDE autocompletion and hover help. See issue #3355.
members = [
httpx.AsyncBaseTransport,
httpx.AsyncByteStream,
httpx.AsyncHTTPTransport,
httpx.BaseTransport,
httpx.ByteStream,
httpx.DigestAuth,
httpx.HTTPTransport,
httpx.MockTransport,
httpx.Proxy,
httpx.SyncByteStream,
httpx.create_ssl_context,
]
for member in members:
assert member.__doc__ and member.__doc__.strip(), member.__name__