TimeoutConfig -> Timeout (#591)
This commit is contained in:
parent
d223de8ff3
commit
c033ed1b65
@ -290,10 +290,10 @@ async with httpx.Client(timeout=5) as client:
|
||||
Besides, you can pass timeouts in two forms:
|
||||
|
||||
- A number, which sets the read, write and connect timeouts to the same value, as in the examples above.
|
||||
- A `TimeoutConfig` instance, which allows to define the read, write and connect timeouts independently:
|
||||
- A `Timeout` instance, which allows to define the read, write and connect timeouts independently:
|
||||
|
||||
```python
|
||||
timeout = httpx.TimeoutConfig(
|
||||
timeout = httpx.Timeout(
|
||||
connect_timeout=5,
|
||||
read_timeout=10,
|
||||
write_timeout=15
|
||||
@ -321,7 +321,7 @@ async with httpx.Client(timeout=None) as client:
|
||||
await client.get(url) # Does not timeout, returns after 10s
|
||||
|
||||
|
||||
timeout = httpx.TimeoutConfig(
|
||||
timeout = httpx.Timeout(
|
||||
connect_timeout=5,
|
||||
read_timeout=None,
|
||||
write_timeout=5
|
||||
|
||||
@ -46,7 +46,7 @@ TRACE [2019-11-06 19:18:56] httpx.dispatch.connection_pool - acquire_connection
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
|
||||
TRACE [2019-11-06 19:18:56] httpx.config - load_ssl_context verify=True cert=None trust_env=True http_versions=HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])
|
||||
TRACE [2019-11-06 19:18:56] httpx.config - load_verify_locations cafile=/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.8/site-packages/certifi/cacert.pem
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - start_connect host='google.com' port=443 timeout=TimeoutConfig(timeout=5.0)
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - start_connect host='google.com' port=443 timeout=Timeout(timeout=5.0)
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - connected http_version='HTTP/2'
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.http2 - send_headers stream_id=1 method='GET' target='/' headers=[(b':method', b'GET'), (b':authority', b'google.com'), (b':scheme', b'https'), (b':path', b'/'), (b'user-agent', b'python-httpx/0.7.6'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')]
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.http2 - end_stream stream_id=1
|
||||
@ -59,7 +59,7 @@ TRACE [2019-11-06 19:18:56] httpx.dispatch.connection_pool - acquire_connection
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))
|
||||
TRACE [2019-11-06 19:18:56] httpx.config - load_ssl_context verify=True cert=None trust_env=True http_versions=HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])
|
||||
TRACE [2019-11-06 19:18:56] httpx.config - load_verify_locations cafile=/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.8/site-packages/certifi/cacert.pem
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - start_connect host='www.google.com' port=443 timeout=TimeoutConfig(timeout=5.0)
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - start_connect host='www.google.com' port=443 timeout=Timeout(timeout=5.0)
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.connection - connected http_version='HTTP/2'
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.http2 - send_headers stream_id=1 method='GET' target='/' headers=[(b':method', b'GET'), (b':authority', b'www.google.com'), (b':scheme', b'https'), (b':path', b'/'), (b'user-agent', b'python-httpx/0.7.6'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')]
|
||||
TRACE [2019-11-06 19:18:56] httpx.dispatch.http2 - end_stream stream_id=1
|
||||
|
||||
@ -14,6 +14,7 @@ from .config import (
|
||||
CertTypes,
|
||||
PoolLimits,
|
||||
SSLConfig,
|
||||
Timeout,
|
||||
TimeoutConfig,
|
||||
TimeoutTypes,
|
||||
VerifyTypes,
|
||||
@ -35,10 +36,10 @@ from .exceptions import (
|
||||
ReadTimeout,
|
||||
RedirectBodyUnavailable,
|
||||
RedirectLoop,
|
||||
RequestTimeout,
|
||||
ResponseClosed,
|
||||
ResponseNotRead,
|
||||
StreamConsumed,
|
||||
Timeout,
|
||||
TooManyRedirects,
|
||||
WriteTimeout,
|
||||
)
|
||||
@ -82,6 +83,7 @@ __all__ = [
|
||||
"CertTypes",
|
||||
"PoolLimits",
|
||||
"SSLConfig",
|
||||
"Timeout",
|
||||
"TimeoutConfig",
|
||||
"VerifyTypes",
|
||||
"HTTPConnection",
|
||||
@ -105,7 +107,6 @@ __all__ = [
|
||||
"ResponseNotRead",
|
||||
"StreamConsumed",
|
||||
"ProxyError",
|
||||
"Timeout",
|
||||
"TooManyRedirects",
|
||||
"WriteTimeout",
|
||||
"BaseSocketStream",
|
||||
@ -126,6 +127,7 @@ __all__ = [
|
||||
"QueryParamTypes",
|
||||
"Request",
|
||||
"RequestData",
|
||||
"RequestTimeout",
|
||||
"Response",
|
||||
"ResponseContent",
|
||||
"RequestFiles",
|
||||
|
||||
@ -5,7 +5,7 @@ import sys
|
||||
import typing
|
||||
from types import TracebackType
|
||||
|
||||
from ..config import PoolLimits, TimeoutConfig
|
||||
from ..config import PoolLimits, Timeout
|
||||
from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout
|
||||
from .base import (
|
||||
BaseBackgroundManager,
|
||||
@ -45,7 +45,7 @@ class SocketStream(BaseSocketStream):
|
||||
self,
|
||||
stream_reader: asyncio.StreamReader,
|
||||
stream_writer: asyncio.StreamWriter,
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
):
|
||||
self.stream_reader = stream_reader
|
||||
self.stream_writer = stream_writer
|
||||
@ -55,7 +55,7 @@ class SocketStream(BaseSocketStream):
|
||||
self._inner: typing.Optional[SocketStream] = None
|
||||
|
||||
async def start_tls(
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: TimeoutConfig
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: Timeout
|
||||
) -> "SocketStream":
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
@ -133,7 +133,7 @@ class SocketStream(BaseSocketStream):
|
||||
return "HTTP/2" if ident == "h2" else "HTTP/1.1"
|
||||
|
||||
async def read(
|
||||
self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None
|
||||
self, n: int, timeout: Timeout = None, flag: TimeoutFlag = None
|
||||
) -> bytes:
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
@ -166,7 +166,7 @@ class SocketStream(BaseSocketStream):
|
||||
self.stream_writer.write(data) # pragma: nocover
|
||||
|
||||
async def write(
|
||||
self, data: bytes, timeout: TimeoutConfig = None, flag: TimeoutFlag = None
|
||||
self, data: bytes, timeout: Timeout = None, flag: TimeoutFlag = None
|
||||
) -> None:
|
||||
if not data:
|
||||
return
|
||||
@ -264,7 +264,7 @@ class AsyncioBackend(ConcurrencyBackend):
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> SocketStream:
|
||||
try:
|
||||
stream_reader, stream_writer = await asyncio.wait_for( # type: ignore
|
||||
@ -283,7 +283,7 @@ class AsyncioBackend(ConcurrencyBackend):
|
||||
path: str,
|
||||
hostname: typing.Optional[str],
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> SocketStream:
|
||||
server_hostname = hostname if ssl_context else None
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ import typing
|
||||
|
||||
import sniffio
|
||||
|
||||
from ..config import PoolLimits, TimeoutConfig
|
||||
from ..config import PoolLimits, Timeout
|
||||
from .base import (
|
||||
BaseBackgroundManager,
|
||||
BaseEvent,
|
||||
@ -29,7 +29,7 @@ class AutoBackend(ConcurrencyBackend):
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
return await self.backend.open_tcp_stream(hostname, port, ssl_context, timeout)
|
||||
|
||||
@ -38,7 +38,7 @@ class AutoBackend(ConcurrencyBackend):
|
||||
path: str,
|
||||
hostname: typing.Optional[str],
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
return await self.backend.open_uds_stream(path, hostname, ssl_context, timeout)
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ import ssl
|
||||
import typing
|
||||
from types import TracebackType
|
||||
|
||||
from ..config import PoolLimits, TimeoutConfig
|
||||
from ..config import PoolLimits, Timeout
|
||||
|
||||
|
||||
def lookup_backend(
|
||||
@ -70,19 +70,19 @@ class BaseSocketStream:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
async def start_tls(
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: TimeoutConfig
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: Timeout
|
||||
) -> "BaseSocketStream":
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
async def read(
|
||||
self, n: int, timeout: TimeoutConfig = None, flag: typing.Any = None
|
||||
self, n: int, timeout: Timeout = None, flag: typing.Any = None
|
||||
) -> bytes:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
def write_no_block(self, data: bytes) -> None:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
async def write(self, data: bytes, timeout: TimeoutConfig = None) -> None:
|
||||
async def write(self, data: bytes, timeout: Timeout = None) -> None:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
async def close(self) -> None:
|
||||
@ -130,7 +130,7 @@ class ConcurrencyBackend:
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
@ -139,7 +139,7 @@ class ConcurrencyBackend:
|
||||
path: str,
|
||||
hostname: typing.Optional[str],
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ from types import TracebackType
|
||||
|
||||
import trio
|
||||
|
||||
from ..config import PoolLimits, TimeoutConfig
|
||||
from ..config import PoolLimits, Timeout
|
||||
from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout
|
||||
from .base import (
|
||||
BaseBackgroundManager,
|
||||
@ -23,9 +23,7 @@ def _or_inf(value: typing.Optional[float]) -> float:
|
||||
|
||||
class SocketStream(BaseSocketStream):
|
||||
def __init__(
|
||||
self,
|
||||
stream: typing.Union[trio.SocketStream, trio.SSLStream],
|
||||
timeout: TimeoutConfig,
|
||||
self, stream: typing.Union[trio.SocketStream, trio.SSLStream], timeout: Timeout,
|
||||
) -> None:
|
||||
self.stream = stream
|
||||
self.timeout = timeout
|
||||
@ -34,7 +32,7 @@ class SocketStream(BaseSocketStream):
|
||||
self.write_lock = trio.Lock()
|
||||
|
||||
async def start_tls(
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: TimeoutConfig
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: Timeout
|
||||
) -> "SocketStream":
|
||||
# Check that the write buffer is empty. We should never start a TLS stream
|
||||
# while there is still pending data to write.
|
||||
@ -61,7 +59,7 @@ class SocketStream(BaseSocketStream):
|
||||
return "HTTP/2" if ident == "h2" else "HTTP/1.1"
|
||||
|
||||
async def read(
|
||||
self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None
|
||||
self, n: int, timeout: Timeout = None, flag: TimeoutFlag = None
|
||||
) -> bytes:
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
@ -98,7 +96,7 @@ class SocketStream(BaseSocketStream):
|
||||
self.write_buffer += data # pragma: no cover
|
||||
|
||||
async def write(
|
||||
self, data: bytes, timeout: TimeoutConfig = None, flag: TimeoutFlag = None
|
||||
self, data: bytes, timeout: Timeout = None, flag: TimeoutFlag = None
|
||||
) -> None:
|
||||
if self.write_buffer:
|
||||
previous_data = self.write_buffer
|
||||
@ -176,7 +174,7 @@ class TrioBackend(ConcurrencyBackend):
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> SocketStream:
|
||||
connect_timeout = _or_inf(timeout.connect_timeout)
|
||||
|
||||
@ -196,7 +194,7 @@ class TrioBackend(ConcurrencyBackend):
|
||||
path: str,
|
||||
hostname: typing.Optional[str],
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> SocketStream:
|
||||
connect_timeout = _or_inf(timeout.connect_timeout)
|
||||
|
||||
|
||||
@ -10,9 +10,7 @@ from .utils import get_ca_bundle_from_env, get_logger
|
||||
|
||||
CertTypes = typing.Union[str, typing.Tuple[str, str], typing.Tuple[str, str, str]]
|
||||
VerifyTypes = typing.Union[str, bool, ssl.SSLContext]
|
||||
TimeoutTypes = typing.Union[
|
||||
float, typing.Tuple[float, float, float, float], "TimeoutConfig"
|
||||
]
|
||||
TimeoutTypes = typing.Union[float, typing.Tuple[float, float, float, float], "Timeout"]
|
||||
|
||||
|
||||
USER_AGENT = f"python-httpx/{__version__}"
|
||||
@ -203,7 +201,7 @@ class SSLConfig:
|
||||
)
|
||||
|
||||
|
||||
class TimeoutConfig:
|
||||
class Timeout:
|
||||
"""
|
||||
Timeout values.
|
||||
"""
|
||||
@ -228,7 +226,7 @@ class TimeoutConfig:
|
||||
assert read_timeout is None
|
||||
assert write_timeout is None
|
||||
assert pool_timeout is None
|
||||
if isinstance(timeout, TimeoutConfig):
|
||||
if isinstance(timeout, Timeout):
|
||||
self.connect_timeout = timeout.connect_timeout
|
||||
self.read_timeout = timeout.read_timeout
|
||||
self.write_timeout = timeout.write_timeout
|
||||
@ -299,8 +297,11 @@ class PoolLimits:
|
||||
)
|
||||
|
||||
|
||||
TimeoutConfig = Timeout # Synonym for backwards compat
|
||||
|
||||
|
||||
DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True)
|
||||
DEFAULT_TIMEOUT_CONFIG = TimeoutConfig(timeout=5.0)
|
||||
DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
|
||||
DEFAULT_POOL_LIMITS = PoolLimits(soft_limit=10, hard_limit=100)
|
||||
DEFAULT_CA_BUNDLE_PATH = Path(certifi.where())
|
||||
DEFAULT_MAX_REDIRECTS = 20
|
||||
|
||||
@ -7,7 +7,7 @@ from ..config import (
|
||||
DEFAULT_TIMEOUT_CONFIG,
|
||||
CertTypes,
|
||||
SSLConfig,
|
||||
TimeoutConfig,
|
||||
Timeout,
|
||||
TimeoutTypes,
|
||||
VerifyTypes,
|
||||
)
|
||||
@ -39,7 +39,7 @@ class HTTPConnection(Dispatcher):
|
||||
):
|
||||
self.origin = Origin(origin) if isinstance(origin, str) else origin
|
||||
self.ssl = SSLConfig(cert=cert, verify=verify, trust_env=trust_env)
|
||||
self.timeout = TimeoutConfig(timeout)
|
||||
self.timeout = Timeout(timeout)
|
||||
self.http2 = http2
|
||||
self.backend = lookup_backend(backend)
|
||||
self.release_func = release_func
|
||||
@ -72,7 +72,7 @@ class HTTPConnection(Dispatcher):
|
||||
timeout: TimeoutTypes = None,
|
||||
) -> None:
|
||||
ssl = self.ssl.with_overrides(verify=verify, cert=cert)
|
||||
timeout = self.timeout if timeout is None else TimeoutConfig(timeout)
|
||||
timeout = self.timeout if timeout is None else Timeout(timeout)
|
||||
|
||||
host = self.origin.host
|
||||
port = self.origin.port
|
||||
|
||||
@ -6,7 +6,7 @@ from ..config import (
|
||||
DEFAULT_TIMEOUT_CONFIG,
|
||||
CertTypes,
|
||||
PoolLimits,
|
||||
TimeoutConfig,
|
||||
Timeout,
|
||||
TimeoutTypes,
|
||||
VerifyTypes,
|
||||
)
|
||||
@ -92,7 +92,7 @@ class ConnectionPool(Dispatcher):
|
||||
):
|
||||
self.verify = verify
|
||||
self.cert = cert
|
||||
self.timeout = TimeoutConfig(timeout)
|
||||
self.timeout = Timeout(timeout)
|
||||
self.pool_limits = pool_limits
|
||||
self.http2 = http2
|
||||
self.is_closed = False
|
||||
@ -147,7 +147,7 @@ class ConnectionPool(Dispatcher):
|
||||
if timeout is None:
|
||||
pool_timeout = self.timeout.pool_timeout
|
||||
else:
|
||||
pool_timeout = TimeoutConfig(timeout).pool_timeout
|
||||
pool_timeout = Timeout(timeout).pool_timeout
|
||||
|
||||
await self.max_connections.acquire(timeout=pool_timeout)
|
||||
connection = HTTPConnection(
|
||||
|
||||
@ -3,7 +3,7 @@ import typing
|
||||
import h11
|
||||
|
||||
from ..concurrency.base import BaseSocketStream, TimeoutFlag
|
||||
from ..config import TimeoutConfig, TimeoutTypes
|
||||
from ..config import Timeout, TimeoutTypes
|
||||
from ..exceptions import ConnectionClosed, ProtocolError
|
||||
from ..models import Request, Response
|
||||
from ..utils import get_logger
|
||||
@ -41,7 +41,7 @@ class HTTP11Connection:
|
||||
self.timeout_flag = TimeoutFlag()
|
||||
|
||||
async def send(self, request: Request, timeout: TimeoutTypes = None) -> Response:
|
||||
timeout = None if timeout is None else TimeoutConfig(timeout)
|
||||
timeout = None if timeout is None else Timeout(timeout)
|
||||
|
||||
await self._send_request(request, timeout)
|
||||
await self._send_request_body(request, timeout)
|
||||
@ -67,9 +67,7 @@ class HTTP11Connection:
|
||||
pass
|
||||
await self.stream.close()
|
||||
|
||||
async def _send_request(
|
||||
self, request: Request, timeout: TimeoutConfig = None
|
||||
) -> None:
|
||||
async def _send_request(self, request: Request, timeout: Timeout = None) -> None:
|
||||
"""
|
||||
Send the request method, URL, and headers to the network.
|
||||
"""
|
||||
@ -86,7 +84,7 @@ class HTTP11Connection:
|
||||
await self._send_event(event, timeout)
|
||||
|
||||
async def _send_request_body(
|
||||
self, request: Request, timeout: TimeoutConfig = None
|
||||
self, request: Request, timeout: Timeout = None
|
||||
) -> None:
|
||||
"""
|
||||
Send the request body to the network.
|
||||
@ -110,7 +108,7 @@ class HTTP11Connection:
|
||||
# Once we've sent the request, we enable read timeouts.
|
||||
self.timeout_flag.set_read_timeouts()
|
||||
|
||||
async def _send_event(self, event: H11Event, timeout: TimeoutConfig = None) -> None:
|
||||
async def _send_event(self, event: H11Event, timeout: Timeout = None) -> None:
|
||||
"""
|
||||
Send a single `h11` event to the network, waiting for the data to
|
||||
drain before returning.
|
||||
@ -119,7 +117,7 @@ class HTTP11Connection:
|
||||
await self.stream.write(bytes_to_send, timeout)
|
||||
|
||||
async def _receive_response(
|
||||
self, timeout: TimeoutConfig = None
|
||||
self, timeout: Timeout = None
|
||||
) -> typing.Tuple[str, int, typing.List[typing.Tuple[bytes, bytes]]]:
|
||||
"""
|
||||
Read the response status and headers from the network.
|
||||
@ -138,7 +136,7 @@ class HTTP11Connection:
|
||||
return http_version, event.status_code, event.headers
|
||||
|
||||
async def _receive_response_data(
|
||||
self, timeout: TimeoutConfig = None
|
||||
self, timeout: Timeout = None
|
||||
) -> typing.AsyncIterator[bytes]:
|
||||
"""
|
||||
Read the response data from the network.
|
||||
@ -151,7 +149,7 @@ class HTTP11Connection:
|
||||
assert isinstance(event, h11.EndOfMessage) or event is h11.PAUSED
|
||||
break # pragma: no cover
|
||||
|
||||
async def _receive_event(self, timeout: TimeoutConfig = None) -> H11Event:
|
||||
async def _receive_event(self, timeout: Timeout = None) -> H11Event:
|
||||
"""
|
||||
Read a single `h11` event, reading more data from the network if needed.
|
||||
"""
|
||||
|
||||
@ -12,7 +12,7 @@ from ..concurrency.base import (
|
||||
TimeoutFlag,
|
||||
lookup_backend,
|
||||
)
|
||||
from ..config import TimeoutConfig, TimeoutTypes
|
||||
from ..config import Timeout, TimeoutTypes
|
||||
from ..exceptions import ProtocolError
|
||||
from ..models import Request, Response
|
||||
from ..utils import get_logger
|
||||
@ -39,7 +39,7 @@ class HTTP2Connection:
|
||||
self.window_update_received = {} # type: typing.Dict[int, BaseEvent]
|
||||
|
||||
async def send(self, request: Request, timeout: TimeoutTypes = None) -> Response:
|
||||
timeout = None if timeout is None else TimeoutConfig(timeout)
|
||||
timeout = None if timeout is None else Timeout(timeout)
|
||||
|
||||
# Start sending the request.
|
||||
if not self.initialized:
|
||||
@ -97,9 +97,7 @@ class HTTP2Connection:
|
||||
self.stream.write_no_block(data_to_send)
|
||||
self.initialized = True
|
||||
|
||||
async def send_headers(
|
||||
self, request: Request, timeout: TimeoutConfig = None
|
||||
) -> int:
|
||||
async def send_headers(self, request: Request, timeout: Timeout = None) -> int:
|
||||
stream_id = self.h2_state.get_next_available_stream_id()
|
||||
headers = [
|
||||
(b":method", request.method.encode("ascii")),
|
||||
@ -124,7 +122,7 @@ class HTTP2Connection:
|
||||
self,
|
||||
stream_id: int,
|
||||
stream: typing.AsyncIterator[bytes],
|
||||
timeout: TimeoutConfig = None,
|
||||
timeout: Timeout = None,
|
||||
) -> None:
|
||||
try:
|
||||
async for data in stream:
|
||||
@ -135,7 +133,7 @@ class HTTP2Connection:
|
||||
self.timeout_flags[stream_id].set_read_timeouts()
|
||||
|
||||
async def send_data(
|
||||
self, stream_id: int, data: bytes, timeout: TimeoutConfig = None
|
||||
self, stream_id: int, data: bytes, timeout: Timeout = None
|
||||
) -> None:
|
||||
while data:
|
||||
# The data will be divided into frames to send based on the flow control
|
||||
@ -159,14 +157,14 @@ class HTTP2Connection:
|
||||
data_to_send = self.h2_state.data_to_send()
|
||||
await self.stream.write(data_to_send, timeout)
|
||||
|
||||
async def end_stream(self, stream_id: int, timeout: TimeoutConfig = None) -> None:
|
||||
async def end_stream(self, stream_id: int, timeout: Timeout = None) -> None:
|
||||
logger.trace(f"end_stream stream_id={stream_id}")
|
||||
self.h2_state.end_stream(stream_id)
|
||||
data_to_send = self.h2_state.data_to_send()
|
||||
await self.stream.write(data_to_send, timeout)
|
||||
|
||||
async def receive_response(
|
||||
self, stream_id: int, timeout: TimeoutConfig = None
|
||||
self, stream_id: int, timeout: Timeout = None
|
||||
) -> typing.Tuple[int, typing.List[typing.Tuple[bytes, bytes]]]:
|
||||
"""
|
||||
Read the response status and headers from the network.
|
||||
@ -190,7 +188,7 @@ class HTTP2Connection:
|
||||
return (status_code, headers)
|
||||
|
||||
async def body_iter(
|
||||
self, stream_id: int, timeout: TimeoutConfig = None
|
||||
self, stream_id: int, timeout: Timeout = None
|
||||
) -> typing.AsyncIterator[bytes]:
|
||||
while True:
|
||||
event = await self.receive_event(stream_id, timeout)
|
||||
@ -203,7 +201,7 @@ class HTTP2Connection:
|
||||
break
|
||||
|
||||
async def receive_event(
|
||||
self, stream_id: int, timeout: TimeoutConfig = None
|
||||
self, stream_id: int, timeout: Timeout = None
|
||||
) -> h2.events.Event:
|
||||
while not self.events[stream_id]:
|
||||
flag = self.timeout_flags[stream_id]
|
||||
|
||||
@ -20,31 +20,31 @@ class HTTPError(Exception):
|
||||
# Timeout exceptions...
|
||||
|
||||
|
||||
class Timeout(HTTPError):
|
||||
class RequestTimeout(HTTPError):
|
||||
"""
|
||||
A base class for all timeouts.
|
||||
"""
|
||||
|
||||
|
||||
class ConnectTimeout(Timeout):
|
||||
class ConnectTimeout(RequestTimeout):
|
||||
"""
|
||||
Timeout while establishing a connection.
|
||||
"""
|
||||
|
||||
|
||||
class ReadTimeout(Timeout):
|
||||
class ReadTimeout(RequestTimeout):
|
||||
"""
|
||||
Timeout while reading response data.
|
||||
"""
|
||||
|
||||
|
||||
class WriteTimeout(Timeout):
|
||||
class WriteTimeout(RequestTimeout):
|
||||
"""
|
||||
Timeout while writing request data.
|
||||
"""
|
||||
|
||||
|
||||
class PoolTimeout(Timeout):
|
||||
class PoolTimeout(RequestTimeout):
|
||||
"""
|
||||
Timeout while waiting to acquire a connection from the pool.
|
||||
"""
|
||||
|
||||
@ -5,7 +5,7 @@ import h2.config
|
||||
import h2.connection
|
||||
import h2.events
|
||||
|
||||
from httpx import AsyncioBackend, BaseSocketStream, Request, TimeoutConfig
|
||||
from httpx import AsyncioBackend, BaseSocketStream, Request, Timeout
|
||||
from tests.concurrency import sleep
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ class MockHTTP2Backend:
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
self.server = MockHTTP2Server(self.app, backend=self.backend)
|
||||
return self.server
|
||||
@ -177,7 +177,7 @@ class MockRawSocketBackend:
|
||||
hostname: str,
|
||||
port: int,
|
||||
ssl_context: typing.Optional[ssl.SSLContext],
|
||||
timeout: TimeoutConfig,
|
||||
timeout: Timeout,
|
||||
) -> BaseSocketStream:
|
||||
self.received_data.append(
|
||||
b"--- CONNECT(%s, %d) ---" % (hostname.encode(), port)
|
||||
@ -194,7 +194,7 @@ class MockRawSocketStream(BaseSocketStream):
|
||||
self.backend = backend
|
||||
|
||||
async def start_tls(
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: TimeoutConfig
|
||||
self, hostname: str, ssl_context: ssl.SSLContext, timeout: Timeout
|
||||
) -> BaseSocketStream:
|
||||
self.backend.received_data.append(b"--- START_TLS(%s) ---" % hostname.encode())
|
||||
return MockRawSocketStream(self.backend)
|
||||
@ -205,7 +205,7 @@ class MockRawSocketStream(BaseSocketStream):
|
||||
def write_no_block(self, data: bytes) -> None:
|
||||
self.backend.received_data.append(data)
|
||||
|
||||
async def write(self, data: bytes, timeout: TimeoutConfig = None) -> None:
|
||||
async def write(self, data: bytes, timeout: Timeout = None) -> None:
|
||||
if data:
|
||||
self.write_no_block(data)
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from httpx import AsyncioBackend, SSLConfig, TimeoutConfig
|
||||
from httpx import AsyncioBackend, SSLConfig, Timeout
|
||||
from httpx.concurrency.trio import TrioBackend
|
||||
from tests.concurrency import run_concurrently
|
||||
|
||||
@ -41,7 +41,7 @@ async def read_response(stream, timeout: float, should_contain: bytes) -> bytes:
|
||||
)
|
||||
async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher):
|
||||
ctx = SSLConfig().load_ssl_context_no_verify()
|
||||
timeout = TimeoutConfig(5)
|
||||
timeout = Timeout(5)
|
||||
|
||||
stream = await backend.open_tcp_stream(
|
||||
https_server.url.host, https_server.url.port, None, timeout
|
||||
@ -73,7 +73,7 @@ async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher)
|
||||
)
|
||||
async def test_start_tls_on_uds_socket_stream(https_uds_server, backend, get_cipher):
|
||||
ctx = SSLConfig().load_ssl_context_no_verify()
|
||||
timeout = TimeoutConfig(5)
|
||||
timeout = Timeout(5)
|
||||
|
||||
stream = await backend.open_uds_stream(
|
||||
https_uds_server.config.uds, https_uds_server.url.host, None, timeout
|
||||
@ -101,7 +101,7 @@ async def test_concurrent_read(server, backend):
|
||||
Regression test for: https://github.com/encode/httpx/issues/527
|
||||
"""
|
||||
stream = await backend.open_tcp_stream(
|
||||
server.url.host, server.url.port, ssl_context=None, timeout=TimeoutConfig(5)
|
||||
server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)
|
||||
)
|
||||
try:
|
||||
await stream.write(b"GET / HTTP/1.1\r\n\r\n")
|
||||
|
||||
@ -134,12 +134,12 @@ def test_limits_eq():
|
||||
|
||||
|
||||
def test_timeout_eq():
|
||||
timeout = httpx.TimeoutConfig(timeout=5.0)
|
||||
assert timeout == httpx.TimeoutConfig(timeout=5.0)
|
||||
timeout = httpx.Timeout(timeout=5.0)
|
||||
assert timeout == httpx.Timeout(timeout=5.0)
|
||||
|
||||
|
||||
def test_timeout_from_nothing():
|
||||
timeout = httpx.TimeoutConfig()
|
||||
timeout = httpx.Timeout()
|
||||
assert timeout.connect_timeout is None
|
||||
assert timeout.read_timeout is None
|
||||
assert timeout.write_timeout is None
|
||||
@ -147,32 +147,32 @@ def test_timeout_from_nothing():
|
||||
|
||||
|
||||
def test_timeout_from_none():
|
||||
timeout = httpx.TimeoutConfig(timeout=None)
|
||||
assert timeout == httpx.TimeoutConfig()
|
||||
timeout = httpx.Timeout(timeout=None)
|
||||
assert timeout == httpx.Timeout()
|
||||
|
||||
|
||||
def test_timeout_from_one_none_value():
|
||||
timeout = httpx.TimeoutConfig(read_timeout=None)
|
||||
assert timeout == httpx.TimeoutConfig()
|
||||
timeout = httpx.Timeout(read_timeout=None)
|
||||
assert timeout == httpx.Timeout()
|
||||
|
||||
|
||||
def test_timeout_from_tuple():
|
||||
timeout = httpx.TimeoutConfig(timeout=(5.0, 5.0, 5.0, 5.0))
|
||||
assert timeout == httpx.TimeoutConfig(timeout=5.0)
|
||||
timeout = httpx.Timeout(timeout=(5.0, 5.0, 5.0, 5.0))
|
||||
assert timeout == httpx.Timeout(timeout=5.0)
|
||||
|
||||
|
||||
def test_timeout_from_config_instance():
|
||||
timeout = httpx.TimeoutConfig(timeout=5.0)
|
||||
assert httpx.TimeoutConfig(timeout) == httpx.TimeoutConfig(timeout=5.0)
|
||||
timeout = httpx.Timeout(timeout=5.0)
|
||||
assert httpx.Timeout(timeout) == httpx.Timeout(timeout=5.0)
|
||||
|
||||
|
||||
def test_timeout_repr():
|
||||
timeout = httpx.TimeoutConfig(timeout=5.0)
|
||||
assert repr(timeout) == "TimeoutConfig(timeout=5.0)"
|
||||
timeout = httpx.Timeout(timeout=5.0)
|
||||
assert repr(timeout) == "Timeout(timeout=5.0)"
|
||||
|
||||
timeout = httpx.TimeoutConfig(read_timeout=5.0)
|
||||
timeout = httpx.Timeout(read_timeout=5.0)
|
||||
assert repr(timeout) == (
|
||||
"TimeoutConfig(connect_timeout=None, read_timeout=5.0, "
|
||||
"Timeout(connect_timeout=None, read_timeout=5.0, "
|
||||
"write_timeout=None, pool_timeout=None)"
|
||||
)
|
||||
|
||||
|
||||
@ -6,13 +6,13 @@ from httpx import (
|
||||
PoolLimits,
|
||||
PoolTimeout,
|
||||
ReadTimeout,
|
||||
TimeoutConfig,
|
||||
Timeout,
|
||||
WriteTimeout,
|
||||
)
|
||||
|
||||
|
||||
async def test_read_timeout(server, backend):
|
||||
timeout = TimeoutConfig(read_timeout=1e-6)
|
||||
timeout = Timeout(read_timeout=1e-6)
|
||||
|
||||
async with Client(timeout=timeout, backend=backend) as client:
|
||||
with pytest.raises(ReadTimeout):
|
||||
@ -20,7 +20,7 @@ async def test_read_timeout(server, backend):
|
||||
|
||||
|
||||
async def test_write_timeout(server, backend):
|
||||
timeout = TimeoutConfig(write_timeout=1e-6)
|
||||
timeout = Timeout(write_timeout=1e-6)
|
||||
|
||||
async with Client(timeout=timeout, backend=backend) as client:
|
||||
with pytest.raises(WriteTimeout):
|
||||
@ -29,7 +29,7 @@ async def test_write_timeout(server, backend):
|
||||
|
||||
|
||||
async def test_connect_timeout(server, backend):
|
||||
timeout = TimeoutConfig(connect_timeout=1e-6)
|
||||
timeout = Timeout(connect_timeout=1e-6)
|
||||
|
||||
async with Client(timeout=timeout, backend=backend) as client:
|
||||
with pytest.raises(ConnectTimeout):
|
||||
@ -39,7 +39,7 @@ async def test_connect_timeout(server, backend):
|
||||
|
||||
async def test_pool_timeout(server, backend):
|
||||
pool_limits = PoolLimits(hard_limit=1)
|
||||
timeout = TimeoutConfig(pool_timeout=1e-4)
|
||||
timeout = Timeout(pool_timeout=1e-4)
|
||||
|
||||
async with Client(
|
||||
pool_limits=pool_limits, timeout=timeout, backend=backend
|
||||
|
||||
Loading…
Reference in New Issue
Block a user