Skip to content

Commit cddcbfe

Browse files
feat: support setting headers via env
1 parent f944611 commit cddcbfe

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/cas_parser/_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
RequestOptions,
2020
not_given,
2121
)
22-
from ._utils import is_given, get_async_library
22+
from ._utils import (
23+
is_given,
24+
is_mapping_t,
25+
get_async_library,
26+
)
2327
from ._compat import cached_property
2428
from ._models import SecurityOptions
2529
from ._version import __version__
@@ -115,6 +119,15 @@ def __init__(
115119
if base_url is None:
116120
base_url = f"https://api.casparser.in"
117121

122+
custom_headers_env = os.environ.get("CAS_PARSER_CUSTOM_HEADERS")
123+
if custom_headers_env is not None:
124+
parsed: dict[str, str] = {}
125+
for line in custom_headers_env.split("\n"):
126+
colon = line.find(":")
127+
if colon >= 0:
128+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
129+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
130+
118131
super().__init__(
119132
version=__version__,
120133
base_url=base_url,
@@ -424,6 +437,15 @@ def __init__(
424437
if base_url is None:
425438
base_url = f"https://api.casparser.in"
426439

440+
custom_headers_env = os.environ.get("CAS_PARSER_CUSTOM_HEADERS")
441+
if custom_headers_env is not None:
442+
parsed: dict[str, str] = {}
443+
for line in custom_headers_env.split("\n"):
444+
colon = line.find(":")
445+
if colon >= 0:
446+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
447+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
448+
427449
super().__init__(
428450
version=__version__,
429451
base_url=base_url,

0 commit comments

Comments
 (0)