Skip to content

Commit cdabdf2

Browse files
feat: support setting headers via env
1 parent 396c5f9 commit cdabdf2

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/kernel/_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
RequestOptions,
2121
not_given,
2222
)
23-
from ._utils import is_given, get_async_library
23+
from ._utils import (
24+
is_given,
25+
is_mapping_t,
26+
get_async_library,
27+
)
2428
from ._compat import cached_property
2529
from ._models import FinalRequestOptions
2630
from ._version import __version__
@@ -158,6 +162,15 @@ def __init__(
158162
except KeyError as exc:
159163
raise ValueError(f"Unknown environment: {environment}") from exc
160164

165+
custom_headers_env = os.environ.get("KERNEL_CUSTOM_HEADERS")
166+
if custom_headers_env is not None:
167+
parsed: dict[str, str] = {}
168+
for line in custom_headers_env.split("\n"):
169+
colon = line.find(":")
170+
if colon >= 0:
171+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
172+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
173+
161174
super().__init__(
162175
version=__version__,
163176
base_url=base_url,
@@ -473,6 +486,15 @@ def __init__(
473486
except KeyError as exc:
474487
raise ValueError(f"Unknown environment: {environment}") from exc
475488

489+
custom_headers_env = os.environ.get("KERNEL_CUSTOM_HEADERS")
490+
if custom_headers_env is not None:
491+
parsed: dict[str, str] = {}
492+
for line in custom_headers_env.split("\n"):
493+
colon = line.find(":")
494+
if colon >= 0:
495+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
496+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
497+
476498
super().__init__(
477499
version=__version__,
478500
base_url=base_url,

0 commit comments

Comments
 (0)