Skip to content

Commit 36a590b

Browse files
feat: support setting headers via env
1 parent bdb98e8 commit 36a590b

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/gradient/_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 ._version import __version__
2630
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
@@ -151,6 +155,15 @@ def __init__(
151155
if base_url is None:
152156
base_url = f"https://api.digitalocean.com"
153157

158+
custom_headers_env = os.environ.get("GRADIENT_CUSTOM_HEADERS")
159+
if custom_headers_env is not None:
160+
parsed: dict[str, str] = {}
161+
for line in custom_headers_env.split("\n"):
162+
colon = line.find(":")
163+
if colon >= 0:
164+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
165+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
166+
154167
super().__init__(
155168
version=__version__,
156169
base_url=base_url,
@@ -528,6 +541,15 @@ def __init__(
528541
if base_url is None:
529542
base_url = f"https://api.digitalocean.com"
530543

544+
custom_headers_env = os.environ.get("GRADIENT_CUSTOM_HEADERS")
545+
if custom_headers_env is not None:
546+
parsed: dict[str, str] = {}
547+
for line in custom_headers_env.split("\n"):
548+
colon = line.find(":")
549+
if colon >= 0:
550+
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
551+
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}
552+
531553
super().__init__(
532554
version=__version__,
533555
base_url=base_url,

0 commit comments

Comments
 (0)