23
23
24
24
logger = logging .getLogger ("eeclient" )
25
25
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
29
27
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
34
33
35
34
36
35
class EESession :
@@ -41,11 +40,24 @@ def __init__(self, sepal_headers: SepalHeaders, enforce_project_id: bool = True)
41
40
42
41
Args:
43
42
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 .
45
44
45
+ Raises:
46
+ ValueError: If SEPAL_HOST environment variable is not set
46
47
"""
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
+
47
58
self .expiry_date = 0
48
59
self .max_retries = 3
60
+ self ._credentials = None
49
61
50
62
self .enforce_project_id = enforce_project_id
51
63
logger .debug (str (sepal_headers ))
@@ -125,7 +137,7 @@ async def set_credentials(self) -> None:
125
137
"Token is expired or about to expire; attempting to refresh credentials."
126
138
)
127
139
attempt = 0
128
- credentials_url = SEPAL_API_DOWNLOAD_URL
140
+ credentials_url = self . sepal_api_download_url
129
141
130
142
# Prepare cookies for authentication.
131
143
sepal_cookies = httpx .Cookies ()
@@ -138,7 +150,7 @@ async def set_credentials(self) -> None:
138
150
try :
139
151
async with httpx .AsyncClient (
140
152
cookies = sepal_cookies ,
141
- verify = VERIFY_SSL ,
153
+ verify = self . verify_ssl ,
142
154
) as client :
143
155
logger .debug (f"Attempt { attempt } to refresh credentials." )
144
156
response = await client .get (credentials_url )
0 commit comments