diff --git a/pyproject.toml b/pyproject.toml index a313730..61aea4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,6 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] dependencies = [ - "truststore==0.10", "h11==0.14", + "truststore==0.10", ] diff --git a/src/ahttpx/_client.py b/src/ahttpx/_client.py index 95d6df9..61a3977 100644 --- a/src/ahttpx/_client.py +++ b/src/ahttpx/_client.py @@ -11,9 +11,18 @@ class Client: - def __init__(self): - self.url = URL("") - self.headers = Headers({"User-Agent": "dev"}) + def __init__( + self, + url: URL | str | None = None, + headers: Headers | Mapping[str, str] | None = None + ): + if url is None: + url = "" + if headers is None: + headers = {"User-Agent": "dev"} + + self.url = URL(url) + self.headers = Headers(headers) self.transport = ConnectionPool() self.via = RedirectMiddleware(self.transport) diff --git a/src/ahttpx/_pool.py b/src/ahttpx/_pool.py index c27a443..de00ac8 100644 --- a/src/ahttpx/_pool.py +++ b/src/ahttpx/_pool.py @@ -5,6 +5,7 @@ import types import h11 +import truststore from ._models import Request, Response, URL from ._network import Lock, NetworkBackend, Semaphore, NetworkStream @@ -12,8 +13,6 @@ class ConnectionPool: def __init__(self): - import truststore - self._connections = [] self._ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self._network_backend = NetworkBackend() diff --git a/src/httpx/_client.py b/src/httpx/_client.py index b04083e..ee61eaf 100644 --- a/src/httpx/_client.py +++ b/src/httpx/_client.py @@ -11,10 +11,20 @@ class Client: - def __init__(self): - self.url = URL("") - self.headers = Headers({"User-Agent": "dev"}) - self.transport = RedirectMiddleware(ConnectionPool()) + def __init__( + self, + url: URL | str | None = None, + headers: Headers | Mapping[str, str] | None = None + ): + if url is None: + url = "" + if headers is None: + headers = {"User-Agent": "dev"} + + self.url = URL(url) + self.headers = Headers(headers) + self.transport = ConnectionPool() + self.via = RedirectMiddleware(self.transport) def build_request( self, @@ -38,7 +48,7 @@ def request( content: Content | Iterable[bytes] | bytes | None = None, ) -> Response: request = self.build_request(method, url, headers=headers, content=content) - with self.transport.send(request) as response: + with self.via.send(request) as response: response.read() return response @@ -51,7 +61,7 @@ def stream( content: Content | Iterable[bytes] | bytes | None = None, ) -> Iterator[Response]: request = self.build_request(method, url, headers=headers, content=content) - with self.transport.send(request) as response: + with self.via.send(request) as response: yield response def get( diff --git a/src/httpx/_pool.py b/src/httpx/_pool.py index 9973f73..9ba55cc 100644 --- a/src/httpx/_pool.py +++ b/src/httpx/_pool.py @@ -5,6 +5,7 @@ import types import h11 +import truststore from ._models import Request, Response, URL from ._network import Lock, NetworkBackend, Semaphore, NetworkStream @@ -12,8 +13,6 @@ class ConnectionPool: def __init__(self): - import truststore - self._connections = [] self._ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self._network_backend = NetworkBackend()