Skip to content

Commit 85cf69c

Browse files
authored
add sample for raw_response_hook (#19985)
* add sample for raw_response_hook * update
1 parent 9f110dd commit 85cf69c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# 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 FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
# --------------------------------------------------------------------------
26+
27+
def test_example_raw_response_hook():
28+
def callback(response):
29+
response.http_response.status_code = 200
30+
response.http_response.headers["custom_header"] = "CustomHeader"
31+
32+
from azure.core.pipeline import Pipeline
33+
from azure.core.pipeline.policies import RedirectPolicy, UserAgentPolicy
34+
from azure.core.pipeline.transport import RequestsTransport, HttpRequest
35+
from azure.core.pipeline.policies import CustomHookPolicy
36+
37+
request = HttpRequest("GET", "https://bing.com")
38+
policies = [
39+
CustomHookPolicy(raw_response_hook=callback)
40+
]
41+
42+
with Pipeline(transport=RequestsTransport(), policies=policies) as pipeline:
43+
response = pipeline.run(request)
44+
assert response.http_response.status_code == 200
45+
assert response.http_response.headers["custom_header"] == "CustomHeader"

0 commit comments

Comments
 (0)