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

Add support for default timeout in OAuth2Session #510

Merged
merged 1 commit into from
Dec 6, 2022
Merged
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
2 changes: 2 additions & 0 deletions authlib/integrations/requests_client/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, client_id=None, client_secret=None,
token=None, token_placement='header',
update_token=None, **kwargs):

self.default_timeout = kwargs.get('timeout')
Session.__init__(self)
update_session_configure(self, kwargs)

Expand All @@ -99,6 +100,7 @@ def fetch_access_token(self, url=None, **kwargs):

def request(self, method, url, withhold_token=False, auth=None, **kwargs):
"""Send request with auto refresh token feature (if available)."""
kwargs['timeout'] = kwargs.get('timeout') or self.default_timeout
if not withhold_token and auth is None:
if not self.token:
raise MissingTokenError()
Expand Down
37 changes: 37 additions & 0 deletions tests/clients/test_requests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,40 @@ def verifier(r, **kwargs):
sess = requests.Session()
sess.send = verifier
sess.get('https://i.b', auth=client.token_auth)

def test_use_default_request_timeout(self):
expected_timeout = 10

def verifier(r, **kwargs):
timeout = kwargs.get('timeout')
self.assertEqual(timeout, expected_timeout)
resp = mock.MagicMock()
return resp

client = OAuth2Session(
client_id=self.client_id,
token=self.token,
timeout=expected_timeout,
)

client.send = verifier
client.request('GET', 'https://i.b', withhold_token=False)

def test_override_default_request_timeout(self):
default_timeout = 15
expected_timeout = 10

def verifier(r, **kwargs):
timeout = kwargs.get('timeout')
self.assertEqual(timeout, expected_timeout)
resp = mock.MagicMock()
return resp

client = OAuth2Session(
client_id=self.client_id,
token=self.token,
timeout=default_timeout,
)

client.send = verifier
client.request('GET', 'https://i.b', withhold_token=False, timeout=expected_timeout)