From 792612bf90959785e8d63c8e7e76b1b49807295c Mon Sep 17 00:00:00 2001 From: Rafael Mardojai CM Date: Thu, 5 Sep 2024 08:49:09 -0500 Subject: [PATCH] providers: settings: Avoid calling `password_lookup_sync` each time --- dialect/providers/settings.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dialect/providers/settings.py b/dialect/providers/settings.py index 27cd6ac8..2ff3475e 100644 --- a/dialect/providers/settings.py +++ b/dialect/providers/settings.py @@ -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: @@ -50,6 +51,9 @@ 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 @@ -57,7 +61,10 @@ def api_key(self) -> str: 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) @@ -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