diff --git a/sdk/core/azure-core/azure/core/_pipeline_client.py b/sdk/core/azure-core/azure/core/_pipeline_client.py index ec32e2b20964..44e5995c5f84 100644 --- a/sdk/core/azure-core/azure/core/_pipeline_client.py +++ b/sdk/core/azure-core/azure/core/_pipeline_client.py @@ -39,7 +39,6 @@ RequestIdPolicy, RetryPolicy, ) -from .pipeline.transport import RequestsTransport from .pipeline._tools import to_rest_response as _to_rest_response try: @@ -181,6 +180,7 @@ def _build_pipeline(self, config, **kwargs): # pylint: disable=no-self-use policies = policies_1 if not transport: + from .pipeline.transport import RequestsTransport transport = RequestsTransport(**kwargs) return Pipeline(transport, policies) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py b/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py index 326b2099bc11..6712178a61b5 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py @@ -25,8 +25,7 @@ # -------------------------------------------------------------------------- import datetime import email.utils -from requests.structures import CaseInsensitiveDict -from ...utils._utils import _FixedOffset +from ...utils._utils import _FixedOffset, _case_insensitive_dict def _parse_http_date(text): """Parse a HTTP date format into datetime.""" @@ -58,7 +57,7 @@ def get_retry_after(response): :return: Value of Retry-After in seconds. :rtype: float or None """ - headers = CaseInsensitiveDict(response.http_response.headers) + headers = _case_insensitive_dict(response.http_response.headers) retry_after = headers.get("retry-after") if retry_after: return parse_retry_after(retry_after) diff --git a/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py b/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py index a177dcbc8443..57067c36bcbd 100644 --- a/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py @@ -26,82 +26,133 @@ import sys from ._base import HttpTransport, HttpRequest, HttpResponse -from ._requests_basic import RequestsTransport, RequestsTransportResponse __all__ = [ 'HttpTransport', 'HttpRequest', 'HttpResponse', - 'RequestsTransport', - 'RequestsTransportResponse', ] # pylint: disable=unused-import, redefined-outer-name try: - from ._base_async import AsyncHttpTransport, AsyncHttpResponse - from ._requests_asyncio import AsyncioRequestsTransport, AsyncioRequestsTransportResponse + from ._requests_basic import RequestsTransport, RequestsTransportResponse __all__.extend([ - 'AsyncHttpTransport', - 'AsyncHttpResponse', - 'AsyncioRequestsTransport', - 'AsyncioRequestsTransportResponse' + 'RequestsTransport', + 'RequestsTransportResponse', ]) + try: + from ._base_async import AsyncHttpTransport, AsyncHttpResponse + from ._requests_asyncio import AsyncioRequestsTransport, AsyncioRequestsTransportResponse - if sys.version_info >= (3, 7): __all__.extend([ - 'TrioRequestsTransport', - 'TrioRequestsTransportResponse', - 'AioHttpTransport', - 'AioHttpTransportResponse', + 'AsyncHttpTransport', + 'AsyncHttpResponse', + 'AsyncioRequestsTransport', + 'AsyncioRequestsTransportResponse' ]) - def __dir__(): - return __all__ - - def __getattr__(name): - if name == 'AioHttpTransport': - try: - from ._aiohttp import AioHttpTransport - return AioHttpTransport - except ImportError: - raise ImportError("aiohttp package is not installed") - if name == 'AioHttpTransportResponse': - try: - from ._aiohttp import AioHttpTransportResponse - return AioHttpTransportResponse - except ImportError: - raise ImportError("aiohttp package is not installed") - if name == 'TrioRequestsTransport': - try: - from ._requests_trio import TrioRequestsTransport - return TrioRequestsTransport - except ImportError: - raise ImportError("trio package is not installed") - if name == 'TrioRequestsTransportResponse': - try: - from ._requests_trio import TrioRequestsTransportResponse - return TrioRequestsTransportResponse - except ImportError: - raise ImportError("trio package is not installed") - return name - - else: - try: - from ._requests_trio import TrioRequestsTransport, TrioRequestsTransportResponse + if sys.version_info >= (3, 7): __all__.extend([ 'TrioRequestsTransport', - 'TrioRequestsTransportResponse' + 'TrioRequestsTransportResponse', + 'AioHttpTransport', + 'AioHttpTransportResponse', ]) - except ImportError: - pass # Trio not installed - try: - from ._aiohttp import AioHttpTransport, AioHttpTransportResponse + def __dir__(): + return __all__ + + def __getattr__(name): + if name == 'AioHttpTransport': + try: + from ._aiohttp import AioHttpTransport + return AioHttpTransport + except ImportError: + raise ImportError("aiohttp package is not installed") + if name == 'AioHttpTransportResponse': + try: + from ._aiohttp import AioHttpTransportResponse + return AioHttpTransportResponse + except ImportError: + raise ImportError("aiohttp package is not installed") + if name == 'TrioRequestsTransport': + try: + from ._requests_trio import TrioRequestsTransport + return TrioRequestsTransport + except ImportError: + raise ImportError("trio package is not installed") + if name == 'TrioRequestsTransportResponse': + try: + from ._requests_trio import TrioRequestsTransportResponse + return TrioRequestsTransportResponse + except ImportError: + raise ImportError("trio package is not installed") + return name + + else: + try: + from ._requests_trio import TrioRequestsTransport, TrioRequestsTransportResponse + + __all__.extend([ + 'TrioRequestsTransport', + 'TrioRequestsTransportResponse' + ]) + except ImportError: + pass # Trio not installed + + try: + from ._aiohttp import AioHttpTransport, AioHttpTransportResponse + + __all__.extend([ + 'AioHttpTransport', + 'AioHttpTransportResponse', + ]) + except ImportError: + pass # Aiohttp not installed + except (ImportError, SyntaxError): + # requests library is installed but asynchronous pipelines not supported. + pass +except (ImportError, SyntaxError): + # requests library is not installed + try: + from ._base_async import AsyncHttpTransport, AsyncHttpResponse + __all__.extend([ + 'AsyncHttpTransport', + 'AsyncHttpResponse', + ]) + + if sys.version_info >= (3, 7): __all__.extend([ 'AioHttpTransport', 'AioHttpTransportResponse', ]) - except ImportError: - pass # Aiohttp not installed -except (ImportError, SyntaxError): - pass # Asynchronous pipelines not supported. + + def __dir__(): + return __all__ + + def __getattr__(name): + if name == 'AioHttpTransport': + try: + from ._aiohttp import AioHttpTransport + return AioHttpTransport + except ImportError: + raise ImportError("aiohttp package is not installed") + if name == 'AioHttpTransportResponse': + try: + from ._aiohttp import AioHttpTransportResponse + return AioHttpTransportResponse + except ImportError: + raise ImportError("aiohttp package is not installed") + return name + + else: + try: + from ._aiohttp import AioHttpTransport, AioHttpTransportResponse + __all__.extend([ + 'AioHttpTransport', + 'AioHttpTransportResponse', + ]) + except ImportError: + pass # Aiohttp not installed + except (ImportError, SyntaxError): + pass # Asynchronous pipelines not supported. diff --git a/sdk/core/azure-core/azure/core/utils/_utils.py b/sdk/core/azure-core/azure/core/utils/_utils.py index 5f3133e02955..08ae1ea21da7 100644 --- a/sdk/core/azure-core/azure/core/utils/_utils.py +++ b/sdk/core/azure-core/azure/core/utils/_utils.py @@ -99,6 +99,8 @@ def _case_insensitive_dict(*args, **kwargs): # multidict is installed by aiohttp from multidict import CIMultiDict + if len(kwargs) == 0 and len(args) == 1 and (not args[0]): + return CIMultiDict() # in case of case_insensitive_dict(None), we don't want to raise exception return CIMultiDict(*args, **kwargs) except ImportError: raise ValueError(