Skip to content

Commit 66f1761

Browse files
author
Eric Toombs
committed
Removed the TokenUpdated exception.
It was redundant. There is no reason why any code that must react to the exception couldn't just use token_updater.
1 parent 3a2a852 commit 66f1761

File tree

5 files changed

+20
-46
lines changed

5 files changed

+20
-46
lines changed

docs/api.rst

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ OAuth 2.0
2626
.. autoclass:: OAuth2
2727
:members:
2828

29-
.. autoclass:: TokenUpdated
30-
:members:
31-
3229

3330
OAuth 2.0 Session
3431
-----------------

docs/oauth2_workflow.rst

+14-16
Original file line numberDiff line numberDiff line change
@@ -257,29 +257,27 @@ is necessary but refreshing is done manually.
257257
>>> client = OAuth2Session(client_id, token=token)
258258
>>> r = client.get(protected_url)
259259
260-
(Second) Define automatic token refresh automatic but update manually
260+
(Second) Define automatic token refresh; no external update required.
261261
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262262

263-
This is the, arguably awkward, middle between the basic and convenient refresh
264-
methods in which a token is automatically refreshed, but saving the new token
265-
is done manually.
263+
Use this when the application does not need to take any action when the token
264+
is updated. It requires no exception catching and results in clean code.
265+
Remember however that you still need to update ``expires_in`` to trigger the
266+
refresh. And be sure to save ``client.token`` before destroying ``client``.
266267

267268
.. code-block:: pycon
268269
269-
>>> from requests_oauthlib import OAuth2Session, TokenUpdated
270-
>>> try:
271-
... client = OAuth2Session(client_id, token=token,
272-
... auto_refresh_kwargs=extra, auto_refresh_url=refresh_url)
273-
... r = client.get(protected_url)
274-
>>> except TokenUpdated as e:
275-
... token_saver(e.token)
270+
>>> from requests_oauthlib import OAuth2Session
271+
>>> client = OAuth2Session(client_id, token=token,
272+
... auto_refresh_kwargs=extra, auto_refresh_url=refresh_url)
273+
>>> r = client.get(protected_url)
276274
277-
(Third, Recommended) Define automatic token refresh and update
278-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275+
(Third) Define automatic token refresh with external update
276+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279277

280-
The third and recommended method will automatically fetch refresh tokens and
281-
save them. It requires no exception catching and results in clean code. Remember
282-
however that you still need to update ``expires_in`` to trigger the refresh.
278+
The third method is the same as the second, only with a custom token update
279+
handler. Use this if your application needs to react immediately to a change in
280+
access token.
283281

284282
.. code-block:: pycon
285283

requests_oauthlib/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .oauth1_auth import OAuth1
44
from .oauth1_session import OAuth1Session
55
from .oauth2_auth import OAuth2
6-
from .oauth2_session import OAuth2Session, TokenUpdated
6+
from .oauth2_session import OAuth2Session
77

88
__version__ = "1.3.1"
99

requests_oauthlib/oauth2_session.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
log = logging.getLogger(__name__)
1212

1313

14-
class TokenUpdated(Warning):
15-
def __init__(self, token):
16-
super(TokenUpdated, self).__init__()
17-
self.token = token
18-
19-
2014
class OAuth2Session(requests.Session):
2115
"""Versatile OAuth 2 extension to :class:`requests.Session`.
2216
@@ -68,10 +62,7 @@ def __init__(
6862
:auto_refresh_kwargs: Extra arguments to pass to the refresh token
6963
endpoint.
7064
:token_updater: Method with one argument, token, to be used to update
71-
your token database on automatic token refresh. If not
72-
set a TokenUpdated warning will be raised when a token
73-
has been refreshed. This warning will carry the token
74-
in its token argument.
65+
your token database on automatic token refresh.
7566
:param kwargs: Arguments to pass to the Session constructor.
7667
"""
7768
super(OAuth2Session, self).__init__(**kwargs)
@@ -534,11 +525,9 @@ def request(
534525
"Updating token to %s using %s.", token, self.token_updater
535526
)
536527
self.token_updater(token)
537-
url, headers, data = self._client.add_token(
538-
url, http_method=method, body=data, headers=headers
539-
)
540-
else:
541-
raise TokenUpdated(token)
528+
url, headers, data = self._client.add_token(
529+
url, http_method=method, body=data, headers=headers
530+
)
542531
else:
543532
raise
544533

tests/test_oauth2_session.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from oauthlib.oauth2 import MismatchingStateError
1919
from oauthlib.oauth2 import WebApplicationClient, MobileApplicationClient
2020
from oauthlib.oauth2 import LegacyApplicationClient, BackendApplicationClient
21-
from requests_oauthlib import OAuth2Session, TokenUpdated
21+
from requests_oauthlib import OAuth2Session
2222
import requests
2323

2424
from requests.auth import _basic_auth_str
@@ -142,16 +142,6 @@ def fake_refresh(r, **kwargs):
142142
sess = OAuth2Session(client=client, token=self.expired_token)
143143
self.assertRaises(TokenExpiredError, sess.get, "https://i.b")
144144

145-
# Auto refresh but no auto update
146-
for client in self.clients:
147-
sess = OAuth2Session(
148-
client=client,
149-
token=self.expired_token,
150-
auto_refresh_url="https://i.b/refresh",
151-
)
152-
sess.send = fake_refresh
153-
self.assertRaises(TokenUpdated, sess.get, "https://i.b")
154-
155145
# Auto refresh and auto update
156146
def token_updater(token):
157147
self.assertEqual(token, self.token)

0 commit comments

Comments
 (0)