Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions sdk/core/azure-core/azure/core/pipeline/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
#
# --------------------------------------------------------------------------
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Union, Any
from typing import Any
from azure.core.rest import HttpResponse as RestHttpResponse
from azure.core.pipeline.transport import HttpResponse as PipelineTransportHttpResponse
HTTPResponseType = Union[RestHttpResponse, PipelineTransportHttpResponse]

def await_result(func, *args, **kwargs):
"""If func returns an awaitable, raise that this runner can't handle it."""
Expand Down
6 changes: 0 additions & 6 deletions sdk/core/azure-core/azure/core/pipeline/_tools_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@
# --------------------------------------------------------------------------
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import TypeVar, Union
from ..pipeline.transport import AsyncHttpResponse as PipelineTransportAsyncHttpResponse
from ..rest import AsyncHttpResponse as RestAsyncHttpResponse

HTTPResponseType = Union[
PipelineTransportAsyncHttpResponse,
RestAsyncHttpResponse,
]

async def await_result(func, *args, **kwargs):
"""If func returns an awaitable, await it."""
Expand Down
47 changes: 44 additions & 3 deletions sdk/core/azure-core/azure/core/pipeline/transport/_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#
# --------------------------------------------------------------------------
import sys
from typing import Any, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING
from typing import (
Any, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING, overload
)
from collections.abc import AsyncIterator
try:
import cchardet as chardet
Expand All @@ -50,7 +52,10 @@
from .._tools import is_rest as _is_rest
from .._tools_async import handle_no_stream_rest_response as _handle_no_stream_rest_response
if TYPE_CHECKING:
from .._tools_async import HTTPResponseType
from ...rest import (
HttpRequest as RestHttpRequest,
AsyncHttpResponse as RestAsyncHttpResponse,
)

# Matching requests, because why not?
CONTENT_CHUNK_SIZE = 10 * 1024
Expand Down Expand Up @@ -139,7 +144,43 @@ def _get_request_data(self, request): #pylint: disable=no-self-use
return form_data
return request.data

async def send(self, request: HttpRequest, **config: Any) -> Optional["HTTPResponseType"]:
@overload
async def send(self, request: HttpRequest, **config: Any) -> Optional[AsyncHttpResponse]:
Copy link
Member

Choose a reason for hiding this comment

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

Have you tried it?

Does it give more information in intellisense (curious)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, basically instead of having one typehint, we get two. (see the 1/2 / 2/2 in the lower left of the typehint)

Screen Shot 2021-09-28 at 3 51 09 PM

Screen Shot 2021-09-28 at 3 51 14 PMI also made sure to put the rest requests and responses one second, since it's still provisional

"""Send the request using this HTTP sender.

Will pre-load the body into memory to be available with a sync method.
Pass stream=True to avoid this behavior.

:param request: The HttpRequest object
:type request: ~azure.core.pipeline.transport.HttpRequest
:param config: Any keyword arguments
:return: The AsyncHttpResponse
:rtype: ~azure.core.pipeline.transport.AsyncHttpResponse

:keyword bool stream: Defaults to False.
:keyword dict proxies: dict of proxy to used based on protocol. Proxy is a dict (protocol, url)
:keyword str proxy: will define the proxy to use all the time
"""

@overload
async def send(self, request: "RestHttpRequest", **config: Any) -> Optional["RestAsyncHttpResponse"]:
"""Send the `azure.core.rest` request using this HTTP sender.

Will pre-load the body into memory to be available with a sync method.
Pass stream=True to avoid this behavior.

:param request: The HttpRequest object
:type request: ~azure.core.rest.HttpRequest
:param config: Any keyword arguments
:return: The AsyncHttpResponse
:rtype: ~azure.core.rest.AsyncHttpResponse

:keyword bool stream: Defaults to False.
:keyword dict proxies: dict of proxy to used based on protocol. Proxy is a dict (protocol, url)
:keyword str proxy: will define the proxy to use all the time
"""

async def send(self, request, **config):
"""Send the request using this HTTP sender.

Will pre-load the body into memory to be available with a sync method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from collections.abc import AsyncIterator
import functools
import logging
from typing import Any, Union, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING
from typing import (
Any, Union, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING, overload
)
import urllib3 # type: ignore

import requests
Expand All @@ -48,7 +50,10 @@
from .._tools_async import handle_no_stream_rest_response as _handle_no_stream_rest_response

if TYPE_CHECKING:
from .._tools_async import HTTPResponseType
from ...rest import (
HttpRequest as RestHttpRequest,
AsyncHttpResponse as RestAsyncHttpResponse
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -86,7 +91,35 @@ async def __aexit__(self, *exc_details): # pylint: disable=arguments-differ
async def sleep(self, duration): # pylint:disable=invalid-overridden-method
await asyncio.sleep(duration)

async def send(self, request: HttpRequest, **kwargs: Any) -> "HTTPResponseType": # type: ignore # pylint:disable=invalid-overridden-method
@overload # type: ignore
async def send(self, request: HttpRequest, **kwargs: Any) -> AsyncHttpResponse: # pylint:disable=invalid-overridden-method
"""Send the request using this HTTP sender.

:param request: The HttpRequest
:type request: ~azure.core.pipeline.transport.HttpRequest
:return: The AsyncHttpResponse
:rtype: ~azure.core.pipeline.transport.AsyncHttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

@overload # type: ignore
async def send(self, request: "RestHttpRequest", **kwargs: Any) -> "RestAsyncHttpResponse": # pylint:disable=invalid-overridden-method
"""Send a `azure.core.rest` request using this HTTP sender.

:param request: The HttpRequest
:type request: ~azure.core.rest.HttpRequest
:return: The AsyncHttpResponse
:rtype: ~azure.core.rest.AsyncHttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

async def send(self, request, **kwargs): # pylint:disable=invalid-overridden-method
"""Send the request using this HTTP sender.

:param request: The HttpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# --------------------------------------------------------------------------
from __future__ import absolute_import
import logging
from typing import Iterator, Optional, Any, Union, TypeVar, TYPE_CHECKING
from typing import Iterator, Optional, Any, Union, TypeVar, overload, TYPE_CHECKING
import urllib3 # type: ignore
from urllib3.util.retry import Retry # type: ignore
from urllib3.exceptions import (
Expand All @@ -49,7 +49,7 @@
from .._tools import is_rest as _is_rest, handle_non_stream_rest_response as _handle_non_stream_rest_response

if TYPE_CHECKING:
from .._tools import HTTPResponseType
from ...rest import HttpRequest as RestHttpRequest, HttpResponse as RestHttpResponse

PipelineType = TypeVar("PipelineType")

Expand Down Expand Up @@ -245,8 +245,37 @@ def close(self):
self._session_owner = False
self.session = None

@overload
def send(self, request, **kwargs):
# type: (HttpRequest, Any) -> HttpResponse
"""Send a rest request and get back a rest response.

:param request: The request object to be sent.
:type request: ~azure.core.pipeline.transport.HttpRequest
:return: An HTTPResponse object.
:rtype: ~azure.core.pipeline.transport.HttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

@overload
def send(self, request, **kwargs):
# type: (RestHttpRequest, Any) -> RestHttpResponse
"""Send an `azure.core.rest` request and get back a rest response.

:param request: The request object to be sent.
:type request: ~azure.core.rest.HttpRequest
:return: An HTTPResponse object.
:rtype: ~azure.core.rest.HttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

def send(self, request, **kwargs): # type: ignore
# type: (HttpRequest, Any) -> HTTPResponseType
"""Send request object according to configuration.

:param request: The request object to be sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from collections.abc import AsyncIterator
import functools
import logging
from typing import Any, Callable, Union, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING
from typing import (
Any, Callable, Union, Optional, AsyncIterator as AsyncIteratorType, TYPE_CHECKING, overload
)
import trio
import urllib3

Expand All @@ -47,7 +49,10 @@
from .._tools import is_rest as _is_rest
from .._tools_async import handle_no_stream_rest_response as _handle_no_stream_rest_response
if TYPE_CHECKING:
from .._tools_async import HTTPResponseType
from ...rest import (
HttpRequest as RestHttpRequest,
AsyncHttpResponse as RestAsyncHttpResponse,
)


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -136,7 +141,35 @@ async def __aexit__(self, *exc_details): # pylint: disable=arguments-differ
async def sleep(self, duration): # pylint:disable=invalid-overridden-method
await trio.sleep(duration)

async def send(self, request: HttpRequest, **kwargs: Any) -> "HTTPResponseType": # type: ignore # pylint:disable=invalid-overridden-method
@overload # type: ignore
async def send(self, request: HttpRequest, **kwargs: Any) -> AsyncHttpResponse: # pylint:disable=invalid-overridden-method
"""Send the request using this HTTP sender.

:param request: The HttpRequest
:type request: ~azure.core.pipeline.transport.HttpRequest
:return: The AsyncHttpResponse
:rtype: ~azure.core.pipeline.transport.AsyncHttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

@overload # type: ignore
async def send(self, request: "RestHttpRequest", **kwargs: Any) -> "RestAsyncHttpResponse": # pylint:disable=invalid-overridden-method
"""Send an `azure.core.rest` request using this HTTP sender.

:param request: The HttpRequest
:type request: ~azure.core.rest.HttpRequest
:return: The AsyncHttpResponse
:rtype: ~azure.core.rest.AsyncHttpResponse

:keyword requests.Session session: will override the driver session and use yours.
Should NOT be done unless really required. Anything else is sent straight to requests.
:keyword dict proxies: will define the proxy to use. Proxy is a dict (protocol, url)
"""

async def send(self, request, **kwargs: Any): # pylint:disable=invalid-overridden-method
"""Send the request using this HTTP sender.

:param request: The HttpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

if TYPE_CHECKING:
from typing import List
from ..pipeline._tools_async import HTTPResponseType
from ..pipeline.policies import SansIOHTTPPolicy

class _PartGenerator(AsyncIterator):
Expand All @@ -21,7 +20,7 @@ class _PartGenerator(AsyncIterator):
:param parts: An iterable of parts
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to update this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do an async generator instead of having a class compatible with 3.5? I had the same thoughts, I can open an issue to do it in another PR if we think it's necessary

"""

def __init__(self, response: "HTTPResponseType", default_http_response_type: Any) -> None:
def __init__(self, response, default_http_response_type: Any) -> None:
self._response = response
self._parts = None
self._default_http_response_type = default_http_response_type
Expand Down