Skip to content

Lazy load dependencies #454

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

Merged
merged 1 commit into from
Jan 24, 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
4 changes: 2 additions & 2 deletions msal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from threading import Lock
import os

import requests

from .oauth2cli import Client, JwtAssertionCreator
from .oauth2cli.oidc import decode_part
from .authority import Authority
Expand Down Expand Up @@ -425,6 +423,8 @@ def __init__(
if http_client:
self.http_client = http_client
else:
import requests # Lazy load

self.http_client = requests.Session()
self.http_client.verify = verify
self.http_client.proxies = proxies
Expand Down
18 changes: 7 additions & 11 deletions msal/authority.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
from urlparse import urlparse
import logging

# Historically some customers patched this module-wide requests instance.
# We keep it here for now. They will be removed in next major release.
import requests
import requests as _requests

from .exceptions import MsalServiceError


Expand Down Expand Up @@ -59,9 +54,10 @@ class Authority(object):
_domains_without_user_realm_discovery = set([])

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

def __init__(self, authority_url, http_client, validate_authority=True):
"""Creates an authority instance, and also validates it.
Expand All @@ -84,7 +80,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
payload = instance_discovery(
"https://{}{}/oauth2/v2.0/authorize".format(
self.instance, authority.path),
self.http_client)
self._http_client)
if payload.get("error") == "invalid_instance":
raise ValueError(
"invalid_instance: "
Expand All @@ -104,7 +100,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
try:
openid_config = tenant_discovery(
tenant_discovery_endpoint,
self.http_client)
self._http_client)
except ValueError:
raise ValueError(
"Unable to get authority configuration for {}. "
Expand All @@ -124,7 +120,7 @@ def user_realm_discovery(self, username, correlation_id=None, response=None):
# "federation_protocol", "cloud_audience_urn",
# "federation_metadata_url", "federation_active_auth_url", etc.
if self.instance not in self.__class__._domains_without_user_realm_discovery:
resp = response or self.http_client.get(
resp = response or self._http_client.get(
"https://{netloc}/common/userrealm/{username}?api-version=1.0".format(
netloc=self.instance, username=username),
headers={'Accept': 'application/json',
Expand Down
3 changes: 1 addition & 2 deletions msal/oauth2cli/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import uuid
import logging

import jwt


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -99,6 +97,7 @@ def create_normal_assertion(
Parameters are defined in https://tools.ietf.org/html/rfc7523#section-3
Key-value pairs in additional_claims will be added into payload as-is.
"""
import jwt # Lazy loading
now = time.time()
payload = {
'aud': audience,
Expand Down
4 changes: 2 additions & 2 deletions msal/oauth2cli/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import string
import hashlib

import requests

from .authcode import AuthCodeReceiver as _AuthCodeReceiver

try:
Expand Down Expand Up @@ -159,6 +157,8 @@ def __init__(
"when http_client is in use")
self._http_client = http_client
else:
import requests # Lazy loading

self._http_client = requests.Session()
self._http_client.verify = True if verify is None else verify
self._http_client.proxies = proxies
Expand Down
32 changes: 0 additions & 32 deletions tests/test_authority_patch.py

This file was deleted.