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

[azure-core] Small fixes for aiohttp #6490

Merged
merged 12 commits into from
Jul 26, 2019
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(resonse):
# 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