Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/azure/core/pipeline/_tools_async.py: 46%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 06:33 +0000

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 

41@overload 

42async def await_result(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: 

43 ... 

44 

45 

46async def await_result(func: Callable[P, Union[T, Awaitable[T]]], *args: P.args, **kwargs: P.kwargs) -> T: 

47 """If func returns an awaitable, await it. 

48 

49 :param func: The function to run. 

50 :type func: callable 

51 :param args: The positional arguments to pass to the function. 

52 :type args: list 

53 :rtype: any 

54 :return: The result of the function 

55 """ 

56 result = func(*args, **kwargs) 

57 if isinstance(result, Awaitable): 

58 return await result 

59 return result 

60 

61 

62async def handle_no_stream_rest_response(response: "RestAsyncHttpResponse") -> None: 

63 """Handle reading and closing of non stream rest responses. 

64 For our new rest responses, we have to call .read() and .close() for our non-stream 

65 responses. This way, we load in the body for users to access. 

66 

67 :param response: The response to read and close. 

68 :type response: ~azure.core.rest.AsyncHttpResponse 

69 """ 

70 try: 

71 await response.read() 

72 await response.close() 

73 except Exception as exc: 

74 await response.close() 

75 raise exc