1# -------------------------------------------------------------------------- 
    2# 
    3# Copyright (c) Microsoft Corporation. All rights reserved. 
    4# 
    5# The MIT License (MIT) 
    6# 
    7# Permission is hereby granted, free of charge, to any person obtaining a copy 
    8# of this software and associated documentation files (the ""Software""), to 
    9# deal in the Software without restriction, including without limitation the 
    10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
    11# sell copies of the Software, and to permit persons to whom the Software is 
    12# furnished to do so, subject to the following conditions: 
    13# 
    14# The above copyright notice and this permission notice shall be included in 
    15# all copies or substantial portions of the Software. 
    16# 
    17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
    20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
    23# IN THE SOFTWARE. 
    24# 
    25# -------------------------------------------------------------------------- 
    26from typing import TYPE_CHECKING, Callable, TypeVar, Awaitable, Union, overload 
    27from typing_extensions import ParamSpec 
    28 
    29if TYPE_CHECKING: 
    30    from ..rest import AsyncHttpResponse as RestAsyncHttpResponse 
    31 
    32P = ParamSpec("P") 
    33T = TypeVar("T") 
    34 
    35 
    36@overload 
    37async def await_result(func: Callable[P, Awaitable[T]], *args: P.args, **kwargs: P.kwargs) -> T: ... 
    38 
    39 
    40@overload 
    41async def await_result(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ... 
    42 
    43 
    44async def await_result(func: Callable[P, Union[T, Awaitable[T]]], *args: P.args, **kwargs: P.kwargs) -> T: 
    45    """If func returns an awaitable, await it. 
    46 
    47    :param func: The function to run. 
    48    :type func: callable 
    49    :param args: The positional arguments to pass to the function. 
    50    :type args: list 
    51    :rtype: any 
    52    :return: The result of the function 
    53    """ 
    54    result = func(*args, **kwargs) 
    55    if isinstance(result, Awaitable): 
    56        return await result 
    57    return result 
    58 
    59 
    60async def handle_no_stream_rest_response(response: "RestAsyncHttpResponse") -> None: 
    61    """Handle reading and closing of non stream rest responses. 
    62    For our new rest responses, we have to call .read() and .close() for our non-stream 
    63    responses. This way, we load in the body for users to access. 
    64 
    65    :param response: The response to read and close. 
    66    :type response: ~azure.core.rest.AsyncHttpResponse 
    67    """ 
    68    try: 
    69        await response.read() 
    70    finally: 
    71        await response.close()