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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

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 TypeVar, Any 

27from azure.core.pipeline import PipelineRequest, PipelineResponse 

28from azure.core.pipeline.transport import HttpResponse as LegacyHttpResponse, HttpRequest as LegacyHttpRequest 

29from azure.core.rest import HttpResponse, HttpRequest 

30from ._base import SansIOHTTPPolicy 

31 

32HTTPResponseType = TypeVar("HTTPResponseType", HttpResponse, LegacyHttpResponse) 

33HTTPRequestType = TypeVar("HTTPRequestType", HttpRequest, LegacyHttpRequest) 

34 

35 

36class CustomHookPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType]): 

37 """A simple policy that enable the given callback 

38 with the response. 

39 

40 :keyword callback raw_request_hook: Callback function. Will be invoked on request. 

41 :keyword callback raw_response_hook: Callback function. Will be invoked on response. 

42 """ 

43 

44 def __init__(self, **kwargs: Any): # pylint: disable=unused-argument,super-init-not-called 

45 self._request_callback = kwargs.get("raw_request_hook") 

46 self._response_callback = kwargs.get("raw_response_hook") 

47 

48 def on_request(self, request: PipelineRequest[HTTPRequestType]) -> None: 

49 """This is executed before sending the request to the next policy. 

50 

51 :param request: The PipelineRequest object. 

52 :type request: ~azure.core.pipeline.PipelineRequest 

53 """ 

54 request_callback = request.context.options.pop("raw_request_hook", None) 

55 if request_callback: 

56 request.context["raw_request_hook"] = request_callback 

57 request_callback(request) 

58 elif self._request_callback: 

59 self._request_callback(request) 

60 

61 response_callback = request.context.options.pop("raw_response_hook", None) 

62 if response_callback: 

63 request.context["raw_response_hook"] = response_callback 

64 

65 def on_response( 

66 self, request: PipelineRequest[HTTPRequestType], response: PipelineResponse[HTTPRequestType, HTTPResponseType] 

67 ) -> None: 

68 """This is executed after the request comes back from the policy. 

69 

70 :param request: The PipelineRequest object. 

71 :type request: ~azure.core.pipeline.PipelineRequest 

72 :param response: The PipelineResponse object. 

73 :type response: ~azure.core.pipeline.PipelineResponse 

74 """ 

75 response_callback = response.context.get("raw_response_hook") 

76 if response_callback: 

77 response_callback(response) 

78 elif self._response_callback: 

79 self._response_callback(response)