Skip to content
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

Improve DefaultAzureCredential docstring and error message #9376

Merged
merged 2 commits into from
Jan 8, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
from .managed_identity import ManagedIdentityCredential
from .shared_cache import SharedTokenCacheCredential

try:
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False

if TYPE_CHECKING:
from typing import Any
from azure.core.credentials import AccessToken

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -83,12 +92,21 @@ def __init__(self, **kwargs):
super(DefaultAzureCredential, self).__init__(*credentials)

def get_token(self, *scopes, **kwargs):
# type: (*str, **Any) -> AccessToken
"""Request an access token for `scopes`.

.. note:: This method is called by Azure SDK clients. It isn't intended for use in application code.

:param str scopes: desired scopes for the token
:raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a
`message` attribute listing each authentication attempt and its error message.
"""
try:
return super(DefaultAzureCredential, self).get_token(*scopes, **kwargs)
except ClientAuthenticationError as e:
raise ClientAuthenticationError(
message="""
{}\n\nPlease visit the Azure identity Python SDK docs at
{}\n\nPlease visit the documentation at
https://aka.ms/python-sdk-identity#defaultazurecredential
to learn what options DefaultAzureCredential supports""".format(
e.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
# ------------------------------------
import logging
import os
from typing import TYPE_CHECKING

from azure.core.exceptions import ClientAuthenticationError

from ..._constants import EnvironmentVariables, KnownAuthorities
from .chained import ChainedTokenCredential
from .environment import EnvironmentCredential
from .managed_identity import ManagedIdentityCredential
from .shared_cache import SharedTokenCacheCredential

if TYPE_CHECKING:
from typing import Any

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -73,3 +79,24 @@ def __init__(self, **kwargs):
_LOGGER.info("Shared token cache is unavailable: '%s'", ex)

super().__init__(*credentials)

async def get_token(self, *scopes: str, **kwargs: "Any"):
"""Asynchronously request an access token for `scopes`.

.. note:: This method is called by Azure SDK clients. It isn't intended for use in application code.

:param str scopes: desired scopes for the token
:raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a
`message` attribute listing each authentication attempt and its error message.
"""
try:
return await super(DefaultAzureCredential, self).get_token(*scopes, **kwargs)
except ClientAuthenticationError as e:
raise ClientAuthenticationError(
message="""
{}\n\nPlease visit the documentation at
https://aka.ms/python-sdk-identity#defaultazurecredential
to learn what options DefaultAzureCredential supports""".format(
e.message
)
)