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

cut hard dependency on requests #19930

Merged
merged 4 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_pipeline_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
RequestIdPolicy,
RetryPolicy,
)
from .pipeline.transport import RequestsTransport
from .pipeline._tools import to_rest_response as _to_rest_response

try:
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions sdk/core/azure-core/azure/core/pipeline/policies/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down
161 changes: 105 additions & 56 deletions sdk/core/azure-core/azure/core/pipeline/transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,82 +26,131 @@

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add comments to all the the try/except blocks here? The logic is a little hard to follow :)

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):
pass
except (ImportError, SyntaxError):
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.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/azure/core/utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's args[0] in this case? Maybe add a small comment as to what this if-statement is addressing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to handle case_insensitive_dict(None). requests.structures.CaseInsensitiveDict returns empty dict while multidict.CIMultiDict raise error.

Here we want to return dict().

return CIMultiDict()
return CIMultiDict(*args, **kwargs)
except ImportError:
raise ValueError(
Expand Down