Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/requests_oauthlib/oauth2_auth.py: 47%

15 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:22 +0000

1from __future__ import unicode_literals 

2from oauthlib.oauth2 import WebApplicationClient, InsecureTransportError 

3from oauthlib.oauth2 import is_secure_transport 

4from requests.auth import AuthBase 

5 

6 

7class OAuth2(AuthBase): 

8 """Adds proof of authorization (OAuth2 token) to the request.""" 

9 

10 def __init__(self, client_id=None, client=None, token=None): 

11 """Construct a new OAuth 2 authorization object. 

12 

13 :param client_id: Client id obtained during registration 

14 :param client: :class:`oauthlib.oauth2.Client` to be used. Default is 

15 WebApplicationClient which is useful for any 

16 hosted application but not mobile or desktop. 

17 :param token: Token dictionary, must include access_token 

18 and token_type. 

19 """ 

20 self._client = client or WebApplicationClient(client_id, token=token) 

21 if token: 

22 for k, v in token.items(): 

23 setattr(self._client, k, v) 

24 

25 def __call__(self, r): 

26 """Append an OAuth 2 token to the request. 

27 

28 Note that currently HTTPS is required for all requests. There may be 

29 a token type that allows for plain HTTP in the future and then this 

30 should be updated to allow plain HTTP on a white list basis. 

31 """ 

32 if not is_secure_transport(r.url): 

33 raise InsecureTransportError() 

34 r.url, r.headers, r.body = self._client.add_token( 

35 r.url, http_method=r.method, body=r.body, headers=r.headers 

36 ) 

37 return r