Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail gracefully when Google Maps API key Colab secret isn't present #2036

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions geemap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16205,6 +16205,18 @@ def is_on_aws():
return on_aws


def _get_colab_secret(key: str) -> Optional[str]:
"""Returns a Colab secret (if available), otherwise None."""
if in_colab_shell():
from google.colab import userdata

try:
return userdata.get(key)
except (userdata.SecretNotFoundError, userdata.NotebookAccessError):
return None # Secret doesn't exist or insufficient access.
return None


def get_google_maps_api_key(key: str = "GOOGLE_MAPS_API_KEY") -> Optional[str]:
"""
Retrieves the Google Maps API key from the environment or Colab user data.
Expand All @@ -16217,10 +16229,6 @@ def get_google_maps_api_key(key: str = "GOOGLE_MAPS_API_KEY") -> Optional[str]:
Returns:
str: The API key, or None if it could not be found.
"""
if in_colab_shell():
from google.colab import userdata

if api_key := userdata.get(key):
return api_key

if api_key := _get_colab_secret(key):
return api_key
return os.environ.get(key, None)
Loading