Skip to content

Commit e184b7f

Browse files
committed
Hide internals
1 parent a00ebdb commit e184b7f

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Except for `requests_auth.testing`, only direct access via `requests_auth.` was considered publicly exposed. This is now explicit, as inner packages are now using private prefix (`_`).
1515
If you were relying on some classes or functions that are now internal, feel free to open an issue.
1616
- `requests_auth.JsonTokenFileCache` and `requests_auth.TokenMemoryCache` `get_token` method does not handle kwargs anymore, the `on_missing_token` callable does not expect any arguments anymore.
17+
- `requests_auth.JsonTokenFileCache` does not expose `tokens_path` or `last_save_time` attributes anymore and is also allowing `pathlib.Path` instances as cache location.
18+
- `requests_auth.TokenMemoryCache` does not expose `forbid_concurrent_cache_access` or `forbid_concurrent_missing_token_function_call` attributes anymore.
1719

1820
### Fixed
1921
- Type information is now provided following [PEP 561](https://www.python.org/dev/peps/pep-0561/).

requests_auth/_oauth2/tokens.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import datetime
55
import threading
66
import logging
7+
from pathlib import Path
8+
79
from requests_auth._errors import *
810

911
logger = logging.getLogger(__name__)
@@ -42,8 +44,8 @@ class TokenMemoryCache:
4244

4345
def __init__(self):
4446
self.tokens = {}
45-
self.forbid_concurrent_cache_access = threading.Lock()
46-
self.forbid_concurrent_missing_token_function_call = threading.Lock()
47+
self._forbid_concurrent_cache_access = threading.Lock()
48+
self._forbid_concurrent_missing_token_function_call = threading.Lock()
4749

4850
def _add_bearer_token(self, key: str, token: str):
4951
"""
@@ -91,7 +93,7 @@ def _add_token(
9193
:param expiry: UTC timestamp of expiry
9294
:param refresh_token: refresh token value
9395
"""
94-
with self.forbid_concurrent_cache_access:
96+
with self._forbid_concurrent_cache_access:
9597
self.tokens[key] = token, expiry, refresh_token
9698
self._save_tokens()
9799
logger.debug(
@@ -121,7 +123,7 @@ def get_token(
121123
"""
122124
logger.debug(f'Retrieving token with "{key}" key.')
123125
refresh_token = None
124-
with self.forbid_concurrent_cache_access:
126+
with self._forbid_concurrent_cache_access:
125127
self._load_tokens()
126128
if key in self.tokens:
127129
token = self.tokens[key]
@@ -140,13 +142,13 @@ def get_token(
140142

141143
if refresh_token is not None and on_expired_token is not None:
142144
try:
143-
with self.forbid_concurrent_missing_token_function_call:
145+
with self._forbid_concurrent_missing_token_function_call:
144146
state, token, expires_in, refresh_token = on_expired_token(
145147
refresh_token
146148
)
147149
self._add_access_token(state, token, expires_in, refresh_token)
148150
logger.debug(f"Refreshed token with key {key}.")
149-
with self.forbid_concurrent_cache_access:
151+
with self._forbid_concurrent_cache_access:
150152
if state in self.tokens:
151153
bearer, expiry, refresh_token = self.tokens[state]
152154
logger.debug(
@@ -158,7 +160,7 @@ def get_token(
158160

159161
logger.debug("Token cannot be found in cache.")
160162
if on_missing_token is not None:
161-
with self.forbid_concurrent_missing_token_function_call:
163+
with self._forbid_concurrent_missing_token_function_call:
162164
new_token = on_missing_token()
163165
if len(new_token) == 2: # Bearer token
164166
state, token = new_token
@@ -173,7 +175,7 @@ def get_token(
173175
logger.warning(
174176
f"Using a token received on another key than expected. Expecting {key} but was {state}."
175177
)
176-
with self.forbid_concurrent_cache_access:
178+
with self._forbid_concurrent_cache_access:
177179
if state in self.tokens:
178180
bearer, expiry, refresh_token = self.tokens[state]
179181
logger.debug(
@@ -187,7 +189,7 @@ def get_token(
187189
raise AuthenticationFailed()
188190

189191
def clear(self):
190-
with self.forbid_concurrent_cache_access:
192+
with self._forbid_concurrent_cache_access:
191193
logger.debug("Clearing token cache.")
192194
self.tokens = {}
193195
self._clear()
@@ -207,36 +209,36 @@ class JsonTokenFileCache(TokenMemoryCache):
207209
Class to manage tokens using a cache file.
208210
"""
209211

210-
def __init__(self, tokens_path: str):
212+
def __init__(self, tokens_path: Union[str, Path]):
211213
TokenMemoryCache.__init__(self)
212-
self.tokens_path = tokens_path
213-
self.last_save_time = 0
214+
self._tokens_path = Path(tokens_path)
215+
self._last_save_time = 0
214216
self._load_tokens()
215217

216218
def _clear(self):
217-
self.last_save_time = 0
219+
self._last_save_time = 0
218220
try:
219-
os.remove(self.tokens_path)
221+
self._tokens_path.unlink(missing_ok=True)
220222
except:
221223
logger.debug("Cannot remove tokens file.")
222224

223225
def _save_tokens(self):
224226
try:
225-
with open(self.tokens_path, "w") as tokens_cache_file:
227+
with self._tokens_path.open(mode="w") as tokens_cache_file:
226228
json.dump(self.tokens, tokens_cache_file)
227-
self.last_save_time = os.path.getmtime(self.tokens_path)
229+
self._last_save_time = os.path.getmtime(self._tokens_path)
228230
except:
229231
logger.exception("Cannot save tokens.")
230232

231233
def _load_tokens(self):
232-
if not os.path.exists(self.tokens_path):
234+
if not self._tokens_path.exists():
233235
logger.debug("No token loaded. Token cache does not exists.")
234236
return
235237
try:
236-
last_modification_time = os.path.getmtime(self.tokens_path)
237-
if last_modification_time > self.last_save_time:
238-
self.last_save_time = last_modification_time
239-
with open(self.tokens_path, "r") as tokens_cache_file:
238+
last_modification_time = os.path.getmtime(self._tokens_path)
239+
if last_modification_time > self._last_save_time:
240+
self._last_save_time = last_modification_time
241+
with self._tokens_path.open(mode="r") as tokens_cache_file:
240242
self.tokens = json.load(tokens_cache_file)
241243
except:
242244
logger.exception("Cannot load tokens.")

0 commit comments

Comments
 (0)