Skip to content

Commit d3f20c6

Browse files
authored
Merge pull request #97 from NodeJSmith/fix/improve_idp_id_clients
Fix/improve idp id clients by making them cached properties, remove get/set state dunders
2 parents 05c8219 + d113bf3 commit d3f20c6

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

.bumpversion.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.bumpversion]
2-
current_version = "0.14.0"
2+
current_version = "0.14.1"
33

44
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(?:-(?P<rc_l>rc)(?P<rc>0|[1-9]\\d*))?"
55

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "otf-api"
3-
version = "0.14.0"
3+
version = "0.14.1"
44
description = "Python OrangeTheory Fitness API Client"
55
authors = [{ name = "Jessica Smith", email = "[email protected]" }]
66
requires-python = ">=3.11"

source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
project = "OrangeTheory API"
1515
copyright = "2025, Jessica Smith"
1616
author = "Jessica Smith"
17-
release = "0.14.0"
17+
release = "0.14.1"
1818

1919
# -- General configuration ---------------------------------------------------
2020
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

src/otf_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _setup_logging() -> None:
3636

3737
_setup_logging()
3838

39-
__version__ = "0.14.0"
39+
__version__ = "0.14.1"
4040

4141

4242
__all__ = ["Otf", "OtfUser", "models"]

src/otf_api/auth/auth.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import typing
44
from collections.abc import Generator
55
from datetime import datetime
6+
from functools import lru_cache
67
from logging import getLogger
78
from time import sleep
89
from typing import Any, ClassVar
@@ -119,15 +120,21 @@ def __init__(
119120
self.mfa_tokens: dict[str, Any] = {}
120121
self.pool_domain_url: str | None = None
121122

122-
self.idp_client: CognitoIdentityProviderClient = Session().client(
123-
"cognito-idp", config=BOTO_CONFIG, region_name=REGION
124-
) # type: ignore
123+
self.handle_login(password)
125124

126-
self.id_client: CognitoIdentityClient = Session().client(
127-
"cognito-identity", config=BOTO_CONFIG, region_name=REGION
128-
) # type: ignore
125+
@property
126+
@lru_cache(maxsize=1)
127+
def idp_client(self) -> "CognitoIdentityProviderClient":
128+
"""Returns the Cognito Identity Provider client."""
129+
LOGGER.debug("Creating Cognito Identity Provider client")
130+
return Session().client("cognito-idp", config=BOTO_CONFIG, region_name=REGION) # type: ignore
129131

130-
self.handle_login(password)
132+
@property
133+
@lru_cache(maxsize=1)
134+
def id_client(self) -> "CognitoIdentityClient":
135+
"""Returns the Cognito Identity client."""
136+
LOGGER.debug("Creating Cognito Identity client")
137+
return Session().client("cognito-identity", config=BOTO_CONFIG, region_name=REGION) # type: ignore
131138

132139
def handle_login(self, password: str | None = None) -> None:
133140
"""Handles the login process for the user.
@@ -340,20 +347,6 @@ def _set_tokens(self, tokens: "InitiateAuthResponseTypeDef") -> None:
340347
self.device_group_key = device_metadata.get("DeviceGroupKey", self.device_group_key)
341348
CACHE.write_device_data_to_cache(self.device_metadata)
342349

343-
def __getstate__(self):
344-
"""Get the state of the object for pickling."""
345-
state = self.__dict__.copy()
346-
del state["idp_client"]
347-
del state["id_client"]
348-
return state
349-
350-
def __setstate__(self, state): # noqa
351-
"""Set the state of the object from a pickled state."""
352-
self.__dict__.update(state)
353-
self.idp_client = Session().client("cognito-idp", config=BOTO_CONFIG, region_name=REGION) # type: ignore
354-
355-
self.id_client = Session().client("cognito-identity", config=BOTO_CONFIG, region_name=REGION) # type: ignore
356-
357350

358351
class HttpxCognitoAuth(httpx.Auth):
359352
http_header: str = "Authorization"

src/otf_api/cache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,13 @@ def get_cache() -> OtfCache:
130130
LOGGER.debug("Using cache directory: %s", cache_dir)
131131
_CACHE = OtfCache(cache_dir)
132132
return _CACHE
133+
134+
135+
def clear_cache() -> None:
136+
"""Clears the cache."""
137+
try:
138+
cache = get_cache()
139+
cache.clear_device_data()
140+
cache.clear_tokens()
141+
except Exception:
142+
LOGGER.exception("Failed to clear cache")

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)