Skip to content

Commit 039b6a8

Browse files
committed
Lazy load dependencies
1 parent 66a1c5a commit 039b6a8

File tree

5 files changed

+8
-51
lines changed

5 files changed

+8
-51
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: 3 additions & 13 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

@@ -58,11 +53,6 @@ class Authority(object):
5853
"""
5954
_domains_without_user_realm_discovery = set([])
6055

61-
@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
65-
6656
def __init__(self, authority_url, http_client, validate_authority=True):
6757
"""Creates an authority instance, and also validates it.
6858
@@ -84,7 +74,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
8474
payload = instance_discovery(
8575
"https://{}{}/oauth2/v2.0/authorize".format(
8676
self.instance, authority.path),
87-
self.http_client)
77+
self._http_client)
8878
if payload.get("error") == "invalid_instance":
8979
raise ValueError(
9080
"invalid_instance: "
@@ -104,7 +94,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
10494
try:
10595
openid_config = tenant_discovery(
10696
tenant_discovery_endpoint,
107-
self.http_client)
97+
self._http_client)
10898
except ValueError:
10999
raise ValueError(
110100
"Unable to get authority configuration for {}. "
@@ -124,7 +114,7 @@ def user_realm_discovery(self, username, correlation_id=None, response=None):
124114
# "federation_protocol", "cloud_audience_urn",
125115
# "federation_metadata_url", "federation_active_auth_url", etc.
126116
if self.instance not in self.__class__._domains_without_user_realm_discovery:
127-
resp = response or self.http_client.get(
117+
resp = response or self._http_client.get(
128118
"https://{netloc}/common/userrealm/{username}?api-version=1.0".format(
129119
netloc=self.instance, username=username),
130120
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)