Skip to content

Commit

Permalink
providers: settings: Avoid calling password_lookup_sync each time
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmardojai committed Sep 5, 2024
1 parent a54dab8 commit 792612b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dialect/providers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, name: str, defaults: ProviderDefaults):
self.name = name
self.defaults = defaults # set of per-provider defaults
self._secret_attr = {"provider": name}
self._api_key: str | None = None

@property
def instance_url(self) -> str:
Expand All @@ -50,14 +51,20 @@ def instance_url(self, url: str):
def api_key(self) -> str:
"""API key."""

if self._api_key:
return self._api_key

# Check if we have an old API KEY in GSettings for migration
if gsettings := self.get_string("api-key"):
self.api_key = gsettings # Save to secret
self.set_string("api-key", "") # Clear GSetting
return gsettings

try:
return Secret.password_lookup_sync(SECRETS_SCHEMA, self._secret_attr, None) or self.defaults["api_key"]
self._api_key = (
Secret.password_lookup_sync(SECRETS_SCHEMA, self._secret_attr, None) or self.defaults["api_key"]
)
return self._api_key
except GLib.Error as exc:
logging.warning(exc)

Expand All @@ -75,7 +82,9 @@ def api_key(self, api_key: str):
api_key,
None,
)
self._api_key = api_key
else: # Remove secret
self._api_key = None
Secret.password_clear_sync(SECRETS_SCHEMA, self._secret_attr, None)

# Fake change in api-key setting
Expand Down

0 comments on commit 792612b

Please sign in to comment.