Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

save and get the api key from the system keyring service #492

Merged
Show file tree
Hide file tree
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
28 changes: 20 additions & 8 deletions data_diff/dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from .version import __version__
from pathlib import Path

import keyring

from .cloud import DatafoldAPI, TCloudApiDataDiff, get_or_create_data_source

logger = getLogger(__name__)
Expand Down Expand Up @@ -230,14 +232,24 @@ def _initialize_api() -> Optional[DatafoldAPI]:

api_key = os.environ.get("DATAFOLD_API_KEY")
if not api_key:
rich.print("[red]API key not found, add it as an environment variable called DATAFOLD_API_KEY.")
yes_or_no = Confirm.ask("Would you like to generate a new API key?")
if yes_or_no:
webbrowser.open(f"{datafold_host}/login?next={datafold_host}/users/me")
rich.print('After generating, please, perform in the terminal "export DATAFOLD_API_KEY=<key>"')
return None
else:
raise ValueError("Cannot initialize API because the API key is not provided")
rich.print("[red]API key not found. Getting from the keyring service")
api_key = keyring.get_password("data-diff", "DATAFOLD_API_KEY")
if not api_key:
rich.print("[red]API key not found, add it as an environment variable called DATAFOLD_API_KEY.")

yes_or_no = Confirm.ask("Would you like to generate a new API key?")
if yes_or_no:
webbrowser.open(f"{datafold_host}/login?next={datafold_host}/users/me")
rich.print('After generating, please, perform in the terminal "export DATAFOLD_API_KEY=<key>"')
return None
else:
raise ValueError("Cannot initialize API because the API key is not provided")

rich.print("Saving the API key to the system keyring service")
try:
keyring.set_password("data-diff", "DATAFOLD_API_KEY", api_key)
except Exception as e:
rich.print(f"[red]Failed when saving the API key to the system keyring service. Reason: {e}")

return DatafoldAPI(api_key=api_key, host=datafold_host)

Expand Down
Loading