Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the TokenUpdated exception. #511

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ OAuth 2.0
.. autoclass:: OAuth2
:members:

.. autoclass:: TokenUpdated
:members:


OAuth 2.0 Session
-----------------
Expand Down
30 changes: 14 additions & 16 deletions docs/oauth2_workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,27 @@ is necessary but refreshing is done manually.
>>> client = OAuth2Session(client_id, token=token)
>>> r = client.get(protected_url)

(Second) Define automatic token refresh automatic but update manually
(Second) Define automatic token refresh; no external update required.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. code-block:: pycon

>>> from requests_oauthlib import OAuth2Session, TokenUpdated
>>> try:
... client = OAuth2Session(client_id, token=token,
... auto_refresh_kwargs=extra, auto_refresh_url=refresh_url)
... r = client.get(protected_url)
>>> except TokenUpdated as e:
... token_saver(e.token)
>>> from requests_oauthlib import OAuth2Session
>>> client = OAuth2Session(client_id, token=token,
... auto_refresh_kwargs=extra, auto_refresh_url=refresh_url)
>>> r = client.get(protected_url)

(Third, Recommended) Define automatic token refresh and update
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(Third) Define automatic token refresh with external update
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. code-block:: pycon

Expand Down
2 changes: 1 addition & 1 deletion requests_oauthlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .oauth1_auth import OAuth1
from .oauth1_session import OAuth1Session
from .oauth2_auth import OAuth2
from .oauth2_session import OAuth2Session, TokenUpdated
from .oauth2_session import OAuth2Session

__version__ = "1.3.1"

Expand Down
21 changes: 5 additions & 16 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
log = logging.getLogger(__name__)


class TokenUpdated(Warning):
def __init__(self, token):
super(TokenUpdated, self).__init__()
self.token = token


class OAuth2Session(requests.Session):
"""Versatile OAuth 2 extension to :class:`requests.Session`.

Expand Down Expand Up @@ -68,10 +62,7 @@ def __init__(
:auto_refresh_kwargs: Extra arguments to pass to the refresh token
endpoint.
:token_updater: Method with one argument, token, to be used to update
your token database on automatic token refresh. If not
set a TokenUpdated warning will be raised when a token
has been refreshed. This warning will carry the token
in its token argument.
your token database on automatic token refresh.
:param kwargs: Arguments to pass to the Session constructor.
"""
super(OAuth2Session, self).__init__(**kwargs)
Expand Down Expand Up @@ -512,7 +503,7 @@ def request(
)
# Attempt to retrieve and save new access token if expired
except TokenExpiredError:
if self.auto_refresh_url:
if self.auto_refresh_url and 'refresh_token' in self.token:
log.debug(
"Auto refresh is set, attempting to refresh at %s.",
self.auto_refresh_url,
Expand All @@ -534,11 +525,9 @@ def request(
"Updating token to %s using %s.", token, self.token_updater
)
self.token_updater(token)
url, headers, data = self._client.add_token(
url, http_method=method, body=data, headers=headers
)
else:
raise TokenUpdated(token)
url, headers, data = self._client.add_token(
url, http_method=method, body=data, headers=headers
)
else:
raise

Expand Down
12 changes: 1 addition & 11 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from oauthlib.oauth2 import MismatchingStateError
from oauthlib.oauth2 import WebApplicationClient, MobileApplicationClient
from oauthlib.oauth2 import LegacyApplicationClient, BackendApplicationClient
from requests_oauthlib import OAuth2Session, TokenUpdated
from requests_oauthlib import OAuth2Session
import requests

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

# Auto refresh but no auto update
for client in self.clients:
sess = OAuth2Session(
client=client,
token=self.expired_token,
auto_refresh_url="https://i.b/refresh",
)
sess.send = fake_refresh
self.assertRaises(TokenUpdated, sess.get, "https://i.b")

# Auto refresh and auto update
def token_updater(token):
self.assertEqual(token, self.token)
Expand Down