Skip to content

Commit

Permalink
remove requests import (Azure#17498)
Browse files Browse the repository at this point in the history
  • Loading branch information
swathipil authored Mar 24, 2021
1 parent df3eab8 commit ee28d35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
25 changes: 25 additions & 0 deletions sdk/core/azure-core/azure/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ def _convert_to_isoformat(date_time):

deserialized = deserialized.replace(tzinfo=tzinfo)
return deserialized

def _case_insensitive_dict(*args, **kwargs):
"""Return a case-insensitive dict from a structure that a dict would have accepted.
Rational is I don't want to re-implement this, but I don't want
to assume "requests" or "aiohttp" are installed either.
So I use the one from "requests" or the one from "aiohttp" ("multidict")
If one day this library is used in an HTTP context without "requests" nor "aiohttp" installed,
we can add "multidict" as a dependency or re-implement our own.
"""
try:
from requests.structures import CaseInsensitiveDict

return CaseInsensitiveDict(*args, **kwargs)
except ImportError:
pass
try:
# multidict is installed by aiohttp
from multidict import CIMultiDict

return CIMultiDict(*args, **kwargs)
except ImportError:
raise ValueError(
"Neither 'requests' or 'multidict' are installed and no case-insensitive dict impl have been found"
)
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 import _FixedOffset
from ..._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
27 changes: 1 addition & 26 deletions sdk/core/azure-core/azure/core/pipeline/transport/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
PipelineContext,
)
from .._tools import await_result as _await_result
from ..._utils import _case_insensitive_dict


if TYPE_CHECKING:
Expand All @@ -87,32 +88,6 @@
_LOGGER = logging.getLogger(__name__)


def _case_insensitive_dict(*args, **kwargs):
"""Return a case-insensitive dict from a structure that a dict would have accepted.
Rational is I don't want to re-implement this, but I don't want
to assume "requests" or "aiohttp" are installed either.
So I use the one from "requests" or the one from "aiohttp" ("multidict")
If one day this library is used in an HTTP context without "requests" nor "aiohttp" installed,
we can add "multidict" as a dependency or re-implement our own.
"""
try:
from requests.structures import CaseInsensitiveDict

return CaseInsensitiveDict(*args, **kwargs)
except ImportError:
pass
try:
# multidict is installed by aiohttp
from multidict import CIMultiDict

return CIMultiDict(*args, **kwargs)
except ImportError:
raise ValueError(
"Neither 'requests' or 'multidict' are installed and no case-insensitive dict impl have been found"
)


def _format_url_section(template, **kwargs):
"""String format the template with the kwargs, auto-skip sections of the template that are NOT in the kwargs.
Expand Down

0 comments on commit ee28d35

Please sign in to comment.