Skip to content
11 changes: 10 additions & 1 deletion sdk/core/azure-core/azure/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
from azure.core.pipeline.transport.base import _HttpResponseBase


def _get_status_code(response):
# type: (_HttpResponseBase) -> int
try:
return response.status_code # Requests
except AttributeError:
return response.status # Aiohttp


def raise_with_traceback(exception, *args, **kwargs):
# type: (Callable, Any, Any) -> None
"""Raise exception with a specified traceback.
Expand Down Expand Up @@ -113,7 +121,8 @@ def __init__(self, message=None, response=None, **kwargs):
self.response = response
if response:
self.reason = response.reason
self.status_code = response.status_code
self.status_code = _get_status_code(response)

message = message or "Operation returned an invalid status '{}'".format(self.reason)
try:
try:
Expand Down
10 changes: 9 additions & 1 deletion sdk/core/azure-core/azure/core/pipeline/policies/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
ContentDecodePolicyType = TypeVar('ContentDecodePolicyType', bound='ContentDecodePolicy')


def _get_content_type(response):
# type: (PipelineResponse) -> str
try:
return response.content_type.strip().lower()
except AttributeError:
return response.content_type[0].strip().lower()


class HeadersPolicy(SansIOHTTPPolicy):
"""A simple policy that sends the given headers with the request.

Expand Down Expand Up @@ -350,7 +358,7 @@ def deserialize_from_http_generics(cls, response):
# Try to use content-type from headers if available
content_type = None
if response.content_type: # type: ignore
content_type = response.content_type[0].strip().lower() # type: ignore
content_type = _get_content_type(response)

# Ouch, this server did not declare what it sent...
# Let's guess it's JSON...
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/azure-core/azure/core/pipeline/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import aiohttp

from azure.core.configuration import ConnectionConfiguration
from azure.core.exceptions import ServiceRequestError
from azure.core.exceptions import ServiceRequestError, ServiceResponseError
from azure.core.pipeline import Pipeline

from requests.exceptions import (
Expand Down Expand Up @@ -181,7 +181,8 @@ async def send(self, request: HttpRequest, **config: Any) -> Optional[AsyncHttpR
await response.load_body()
except aiohttp.client_exceptions.ClientConnectorError as err:
error = ServiceRequestError(err, error=err)

except asyncio.TimeoutError as err:
error = ServiceResponseError(err, error=err)
if error:
raise error
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ async def __aenter__(self):
async def __aexit__(self, *exc_details): # pylint: disable=arguments-differ
return super(AsyncioRequestsTransport, self).__exit__()

async def sleep(self, duration):
await asyncio.sleep(duration)

async def send(self, request: HttpRequest, **kwargs: Any) -> AsyncHttpResponse: # type: ignore
"""Send the request using this HTTP sender.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def __aexit__(self, *exc_details): # pylint: disable=arguments-differ
return super(TrioRequestsTransport, self).__exit__()

async def sleep(self, duration):
await trio.sleep(duration)
return super(TrioRequestsTransport, self).sleep(duration)
Comment thread
lmazuel marked this conversation as resolved.
Outdated

async def send(self, request: HttpRequest, **kwargs: Any) -> AsyncHttpResponse: # type: ignore
"""Send the request using this HTTP sender.
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ async def test_basic_async_requests():

assert response.http_response.status_code == 200

@pytest.mark.asyncio
async def test_async_transport_sleep():

async with AsyncioRequestsTransport() as transport:
await transport.sleep(1)

async with AioHttpTransport() as transport:
await transport.sleep(1)

async with TrioRequestsTransport() as transport:
await transport.sleep(1)

@pytest.mark.asyncio
async def test_conf_async_requests():

Expand Down