Skip to content

Commit

Permalink
Merge pull request #510 from kognity/fix-load-server-metadata-2
Browse files Browse the repository at this point in the history
Add support for default timeout in OAuth2Session
  • Loading branch information
lepture authored Dec 6, 2022
2 parents 676645c + d644ad7 commit def72dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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)

0 comments on commit def72dc

Please sign in to comment.