Skip to content

Commit 6dbb631

Browse files
committed
feat: Defer environment variable check until session initialization instead of import time
1 parent c5b91e1 commit 6dbb631

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test*.ipynb
77
*.so
88
config.json
99
test_with_headers.ipynb
10+
.github/instructions/
1011

1112
# Distribution / packaging
1213
.Python

eeclient/client.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323

2424
logger = logging.getLogger("eeclient")
2525

26-
SEPAL_HOST = os.getenv("SEPAL_HOST")
27-
if not SEPAL_HOST:
28-
raise ValueError("SEPAL_HOST environment variable not set")
26+
# Default values that won't raise exceptions during import
2927
EARTH_ENGINE_API_URL = "https://earthengine.googleapis.com/v1alpha"
30-
SEPAL_API_DOWNLOAD_URL = f"https://{SEPAL_HOST}/api/user-files/download/?path=%2F.config%2Fearthengine%2Fcredentials"
31-
VERIFY_SSL = not (
32-
SEPAL_HOST == "host.docker.internal" or SEPAL_HOST == "danielg.sepal.io"
33-
)
28+
29+
# These will be set properly when EESession is initialized
30+
SEPAL_HOST = os.getenv("SEPAL_HOST")
31+
SEPAL_API_DOWNLOAD_URL = None
32+
VERIFY_SSL = True
3433

3534

3635
class EESession:
@@ -41,11 +40,24 @@ def __init__(self, sepal_headers: SepalHeaders, enforce_project_id: bool = True)
4140
4241
Args:
4342
sepal_headers (SepalHeaders): The headers sent by SEPAL
44-
enforce_project_id (Optional[str], optional): If set, it cannot be changed. Defaults to None.
43+
enforce_project_id (bool, optional): If set, it cannot be changed. Defaults to True.
4544
45+
Raises:
46+
ValueError: If SEPAL_HOST environment variable is not set
4647
"""
48+
# Get and validate environment variables that are required for the session
49+
self.sepal_host = os.getenv("SEPAL_HOST")
50+
if not self.sepal_host:
51+
raise ValueError("SEPAL_HOST environment variable not set")
52+
53+
self.sepal_api_download_url = f"https://{self.sepal_host}/api/user-files/download/?path=%2F.config%2Fearthengine%2Fcredentials"
54+
self.verify_ssl = not (
55+
self.sepal_host == "host.docker.internal" or self.sepal_host == "danielg.sepal.io"
56+
)
57+
4758
self.expiry_date = 0
4859
self.max_retries = 3
60+
self._credentials = None
4961

5062
self.enforce_project_id = enforce_project_id
5163
logger.debug(str(sepal_headers))
@@ -125,7 +137,7 @@ async def set_credentials(self) -> None:
125137
"Token is expired or about to expire; attempting to refresh credentials."
126138
)
127139
attempt = 0
128-
credentials_url = SEPAL_API_DOWNLOAD_URL
140+
credentials_url = self.sepal_api_download_url
129141

130142
# Prepare cookies for authentication.
131143
sepal_cookies = httpx.Cookies()
@@ -138,7 +150,7 @@ async def set_credentials(self) -> None:
138150
try:
139151
async with httpx.AsyncClient(
140152
cookies=sepal_cookies,
141-
verify=VERIFY_SSL,
153+
verify=self.verify_ssl,
142154
) as client:
143155
logger.debug(f"Attempt {attempt} to refresh credentials.")
144156
response = await client.get(credentials_url)

0 commit comments

Comments
 (0)