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 __future__ import annotations 
    27from typing import TYPE_CHECKING, Union, Callable, TypeVar 
    28from typing_extensions import TypeGuard, ParamSpec 
    29 
    30if TYPE_CHECKING: 
    31    from azure.core.rest import HttpResponse, HttpRequest, AsyncHttpResponse 
    32 
    33 
    34P = ParamSpec("P") 
    35T = TypeVar("T") 
    36 
    37 
    38def await_result(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: 
    39    """If func returns an awaitable, raise that this runner can't handle it. 
    40 
    41    :param func: The function to run. 
    42    :type func: callable 
    43    :param args: The positional arguments to pass to the function. 
    44    :type args: list 
    45    :rtype: any 
    46    :return: The result of the function 
    47    :raises TypeError: If the function returns an awaitable object. 
    48    """ 
    49    result = func(*args, **kwargs) 
    50    if hasattr(result, "__await__"): 
    51        raise TypeError("Policy {} returned awaitable object in non-async pipeline.".format(func)) 
    52    return result 
    53 
    54 
    55def is_rest( 
    56    obj: object, 
    57) -> TypeGuard[Union[HttpRequest, HttpResponse, AsyncHttpResponse]]: 
    58    """Return whether a request or a response is a rest request / response. 
    59 
    60    Checking whether the response has the object content can sometimes result 
    61    in a ResponseNotRead error if you're checking the value on a response 
    62    that has not been read in yet. To get around this, we also have added 
    63    a check for is_stream_consumed, which is an exclusive property on our new responses. 
    64 
    65    :param obj: The object to check. 
    66    :type obj: any 
    67    :rtype: bool 
    68    :return: Whether the object is a rest request / response. 
    69    """ 
    70    return hasattr(obj, "is_stream_consumed") or hasattr(obj, "content") 
    71 
    72 
    73def handle_non_stream_rest_response(response: HttpResponse) -> None: 
    74    """Handle reading and closing of non stream rest responses. 
    75    For our new rest responses, we have to call .read() and .close() for our non-stream 
    76    responses. This way, we load in the body for users to access. 
    77 
    78    :param response: The response to read and close. 
    79    :type response: ~azure.core.rest.HttpResponse 
    80    """ 
    81    try: 
    82        response.read() 
    83    finally: 
    84        response.close()