Skip to content

Commit 304e069

Browse files
committed
Lazy load dependencies
1 parent 66a1c5a commit 304e069

File tree

5 files changed

+12
-49
lines changed

5 files changed

+12
-49
lines changed

msal/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from threading import Lock
1212
import os
1313

14-
import requests
15-
1614
from .oauth2cli import Client, JwtAssertionCreator
1715
from .oauth2cli.oidc import decode_part
1816
from .authority import Authority
@@ -425,6 +423,8 @@ def __init__(
425423
if http_client:
426424
self.http_client = http_client
427425
else:
426+
import requests # Lazy load
427+
428428
self.http_client = requests.Session()
429429
self.http_client.verify = verify
430430
self.http_client.proxies = proxies

msal/authority.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
from urlparse import urlparse
66
import logging
77

8-
# Historically some customers patched this module-wide requests instance.
9-
# We keep it here for now. They will be removed in next major release.
10-
import requests
11-
import requests as _requests
12-
138
from .exceptions import MsalServiceError
149

1510

@@ -59,9 +54,10 @@ class Authority(object):
5954
_domains_without_user_realm_discovery = set([])
6055

6156
@property
62-
def http_client(self): # Obsolete. We will remove this in next major release.
63-
# A workaround: if module-wide requests is patched, we honor it.
64-
return self._http_client if requests is _requests else requests
57+
def http_client(self): # Obsolete. We will remove this eventually
58+
warnings.warn(
59+
"authority.http_client might be removed in MSAL Python 1.21+", DeprecationWarning)
60+
return self._http_client
6561

6662
def __init__(self, authority_url, http_client, validate_authority=True):
6763
"""Creates an authority instance, and also validates it.
@@ -84,7 +80,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
8480
payload = instance_discovery(
8581
"https://{}{}/oauth2/v2.0/authorize".format(
8682
self.instance, authority.path),
87-
self.http_client)
83+
self._http_client)
8884
if payload.get("error") == "invalid_instance":
8985
raise ValueError(
9086
"invalid_instance: "
@@ -104,7 +100,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
104100
try:
105101
openid_config = tenant_discovery(
106102
tenant_discovery_endpoint,
107-
self.http_client)
103+
self._http_client)
108104
except ValueError:
109105
raise ValueError(
110106
"Unable to get authority configuration for {}. "
@@ -124,7 +120,7 @@ def user_realm_discovery(self, username, correlation_id=None, response=None):
124120
# "federation_protocol", "cloud_audience_urn",
125121
# "federation_metadata_url", "federation_active_auth_url", etc.
126122
if self.instance not in self.__class__._domains_without_user_realm_discovery:
127-
resp = response or self.http_client.get(
123+
resp = response or self._http_client.get(
128124
"https://{netloc}/common/userrealm/{username}?api-version=1.0".format(
129125
netloc=self.instance, username=username),
130126
headers={'Accept': 'application/json',

msal/oauth2cli/assertion.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import uuid
55
import logging
66

7-
import jwt
8-
97

108
logger = logging.getLogger(__name__)
119

@@ -99,6 +97,7 @@ def create_normal_assertion(
9997
Parameters are defined in https://tools.ietf.org/html/rfc7523#section-3
10098
Key-value pairs in additional_claims will be added into payload as-is.
10199
"""
100+
import jwt # Lazy loading
102101
now = time.time()
103102
payload = {
104103
'aud': audience,

msal/oauth2cli/oauth2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import string
1818
import hashlib
1919

20-
import requests
21-
2220
from .authcode import AuthCodeReceiver as _AuthCodeReceiver
2321

2422
try:
@@ -159,6 +157,8 @@ def __init__(
159157
"when http_client is in use")
160158
self._http_client = http_client
161159
else:
160+
import requests # Lazy loading
161+
162162
self._http_client = requests.Session()
163163
self._http_client.verify = True if verify is None else verify
164164
self._http_client.proxies = proxies

tests/test_authority_patch.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)