1- """Define utilities needed by the MP web server."""
1+ """Define flask-dependent utilities for the web server."""
2+
23from __future__ import annotations
34
45try :
1011 _has_request_context = None
1112 request = None
1213
13- from mp_api .client import MPRester
1414from mp_api .client .core .utils import validate_api_key
1515
1616def has_request_context () -> bool :
@@ -34,8 +34,8 @@ def get_request_headers() -> dict[str,Any]:
3434 """
3535 return request .headers if has_request_context () else {}
3636
37- def is_localhost () -> bool :
38- """Determine if current env is local or production.
37+ def is_dev_env () -> bool :
38+ """Determine if current env is local/developmental or production.
3939
4040 Returns:
4141 bool: True if the environment is locally hosted.
@@ -83,39 +83,24 @@ def is_logged_in_user(consumer: dict[str, str] | None = None) -> bool:
8383 return bool (not c .get ("X-Anonymous-Consumer" ) and c .get ("X-Consumer-Id" ))
8484
8585
86- def get_user_api_key (consumer : dict [str , str ] | None = None ) -> str | None :
86+ def get_user_api_key (
87+ api_key : str | None = None ,
88+ consumer : dict [str , str ] | None = None
89+ ) -> str | None :
8790 """Get the api key that belongs to the current user.
8891
8992 If running on localhost, api key is obtained from
9093 the environment variable MP_API_KEY.
9194
9295 Args:
96+ api_key (str or None) : User API key
9397 consumer (dict of str to str, or None): Headers associated with the consumer
9498
9599 Returns:
96100 str, the API key, or None if no API key could be identified.
97101 """
98- c = consumer or get_consumer ()
99-
100- if is_localhost ():
101- return validate_api_key ()
102- elif is_logged_in_user (c ):
102+ if is_dev_env ():
103+ return validate_api_key (api_key = api_key )
104+ elif is_logged_in_user (c := consumer or get_consumer ()):
103105 return c .get ("X-Consumer-Custom-Id" )
104- return None
105-
106-
107- def get_rester (** kwargs ) -> MPRester :
108- """Create MPRester with headers set for localhost and production compatibility.
109-
110- Args:
111- **kwargs : kwargs to pass to MPRester
112-
113- Returns:
114- MPRester
115- """
116- if is_localhost ():
117- dev_api_key = get_user_api_key ()
118- SESSION .headers ["x-api-key" ] = dev_api_key or ""
119- return MPRester (api_key = dev_api_key , session = SESSION , ** kwargs )
120-
121- return MPRester (headers = get_consumer (), session = SESSION , ** kwargs )
106+ return None
0 commit comments