import typing
from types import TracebackType
+from .auth import HTTPBasicAuth
from .config import (
DEFAULT_MAX_REDIRECTS,
DEFAULT_POOL_LIMITS,
from .interfaces import ConcurrencyBackend, Dispatcher
from .models import (
URL,
+ AuthTypes,
Headers,
HeaderTypes,
QueryParamTypes,
class AsyncClient:
def __init__(
self,
+ auth: AuthTypes = None,
ssl: SSLConfig = DEFAULT_SSL_CONFIG,
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
ssl=ssl, timeout=timeout, pool_limits=pool_limits, backend=backend
)
+ self.auth = auth
self.max_redirects = max_redirects
self.dispatch = dispatch
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = False, # Note: Differs to usual default.
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
query_params=query_params,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
response = await self.send(
request,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
request.prepare()
async def send(
+ self,
+ request: Request,
+ *,
+ stream: bool = False,
+ auth: AuthTypes = None,
+ ssl: SSLConfig = None,
+ timeout: TimeoutConfig = None,
+ allow_redirects: bool = True,
+ ) -> Response:
+ if auth is None:
+ auth = self.auth
+
+ url = request.url
+ if auth is None and (url.username or url.password):
+ auth = HTTPBasicAuth(username=url.username, password=url.password)
+
+ if auth is not None:
+ if isinstance(auth, tuple):
+ auth = HTTPBasicAuth(username=auth[0], password=auth[1])
+ request = auth(request)
+
+ response = await self.send_handling_redirects(
+ request,
+ stream=stream,
+ ssl=ssl,
+ timeout=timeout,
+ allow_redirects=allow_redirects,
+ )
+ return response
+
+ async def send_handling_redirects(
self,
request: Request,
*,
async def send_next() -> Response:
nonlocal request, response, ssl, allow_redirects, timeout, history
request = self.build_redirect_request(request, response)
- response = await self.send(
+ response = await self.send_handling_redirects(
request,
stream=stream,
allow_redirects=allow_redirects,
class Client:
def __init__(
self,
+ auth: AuthTypes = None,
ssl: SSLConfig = DEFAULT_SSL_CONFIG,
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
backend: ConcurrencyBackend = None,
) -> None:
self._client = AsyncClient(
+ auth=auth,
ssl=ssl,
timeout=timeout,
pool_limits=pool_limits,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
response = self.send(
request,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
url,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
url,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = False, # Note: Differs to usual default.
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
url,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
data=data,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
data=data,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
data=data,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
data=data,
headers=headers,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,
request: Request,
*,
stream: bool = False,
+ auth: AuthTypes = None,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
self._client.send(
request,
stream=stream,
+ auth=auth,
allow_redirects=allow_redirects,
ssl=ssl,
timeout=timeout,