From efd0cb6cf1e02aa48ea02b231e41fa741cecbbc5 Mon Sep 17 00:00:00 2001 From: Carl Lei Date: Sun, 3 Nov 2024 12:30:02 +0800 Subject: [PATCH] Support custom httpx transports --- lightkube/config/client_adapter.py | 18 ++++++++++++++---- lightkube/core/async_client.py | 3 +++ lightkube/core/client.py | 3 +++ lightkube/core/generic_client.py | 5 ++++- setup.py | 2 +- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lightkube/config/client_adapter.py b/lightkube/config/client_adapter.py index 34ecc54..eb0b04d 100644 --- a/lightkube/config/client_adapter.py +++ b/lightkube/config/client_adapter.py @@ -12,15 +12,25 @@ def Client( - config: SingleConfig, timeout: httpx.Timeout, trust_env: bool = True + config: SingleConfig, + timeout: httpx.Timeout, + trust_env: bool = True, + transport: httpx.BaseTransport = None, ) -> httpx.Client: - return httpx.Client(**httpx_parameters(config, timeout, trust_env)) + return httpx.Client( + transport=transport, **httpx_parameters(config, timeout, trust_env) + ) def AsyncClient( - config: SingleConfig, timeout: httpx.Timeout, trust_env: bool = True + config: SingleConfig, + timeout: httpx.Timeout, + trust_env: bool = True, + transport: httpx.AsyncBaseTransport = None, ) -> httpx.AsyncClient: - return httpx.AsyncClient(**httpx_parameters(config, timeout, trust_env)) + return httpx.AsyncClient( + transport=transport, **httpx_parameters(config, timeout, trust_env) + ) def httpx_parameters(config: SingleConfig, timeout: httpx.Timeout, trust_env: bool): diff --git a/lightkube/core/async_client.py b/lightkube/core/async_client.py index db13cf4..5059bb9 100644 --- a/lightkube/core/async_client.py +++ b/lightkube/core/async_client.py @@ -47,6 +47,7 @@ class AsyncClient: * **dry_run** - *(optional)* Apply server-side dry-run and guarantee that modifications will not be persisted in storage. Setting this field to `True` is equivalent of passing `--dry-run=server` to `kubectl` commands. + * **transport** - *(optional)* Custom httpx transport """ def __init__( @@ -58,6 +59,7 @@ def __init__( field_manager: str = None, trust_env: bool = True, dry_run: bool = False, + transport: httpx.AsyncBaseTransport = None, ): self._client = GenericAsyncClient( config, @@ -67,6 +69,7 @@ def __init__( field_manager=field_manager, trust_env=trust_env, dry_run=dry_run, + transport=transport, ) @property diff --git a/lightkube/core/client.py b/lightkube/core/client.py index f845772..c2cc8f1 100644 --- a/lightkube/core/client.py +++ b/lightkube/core/client.py @@ -52,6 +52,7 @@ class Client: * **dry_run** - *(optional)* Apply server-side dry-run and guarantee that modifications will not be persisted in storage. Setting this field to `True` is equivalent of passing `--dry-run=server` to `kubectl` commands. + * **transport** - *(optional)* Custom httpx transport """ def __init__( @@ -63,6 +64,7 @@ def __init__( field_manager: str = None, trust_env: bool = True, dry_run: bool = False, + transport: httpx.BaseTransport = None, ): self._client = GenericSyncClient( config, @@ -72,6 +74,7 @@ def __init__( field_manager=field_manager, trust_env=trust_env, dry_run=dry_run, + transport=transport, ) @property diff --git a/lightkube/core/generic_client.py b/lightkube/core/generic_client.py index 20a5429..3157312 100644 --- a/lightkube/core/generic_client.py +++ b/lightkube/core/generic_client.py @@ -84,6 +84,7 @@ def __init__( trust_env: bool = True, field_manager: str = None, dry_run: bool = False, + transport: Union[httpx.BaseTransport, httpx.AsyncBaseTransport] = None, ): self._timeout = httpx.Timeout(10) if timeout is None else timeout self._watch_timeout = httpx.Timeout(self._timeout) @@ -97,7 +98,9 @@ def __init__( config = config.get() self.config = config - self._client = self.AdapterClient(config, timeout, trust_env=trust_env) + self._client = self.AdapterClient( + config, timeout, trust_env=trust_env, transport=transport + ) self._field_manager = field_manager self._dry_run = dry_run self.namespace = namespace if namespace else config.namespace diff --git a/setup.py b/setup.py index f321ba0..d56d7bc 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='lightkube', - version="0.15.4", + version="0.15.5", description='Lightweight kubernetes client library', long_description=Path("README.md").read_text(), long_description_content_type="text/markdown",