Skip to content

Commit 4e2323d

Browse files
lmazuelpvaneck
andauthored
Add sample for per_call_policies (Azure#33048)
* Add sample for per_call_policies * Clean import * cspell * Black * Update sdk/core/azure-core/samples/test_example_sansio.py Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com> * Update sdk/core/azure-core/samples/test_example_sansio.py Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com> * Update sdk/core/azure-core/samples/test_example_sansio.py Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com> --------- Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent 645c667 commit 4e2323d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sdk/core/azure-core/samples/test_example_sansio.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# --------------------------------------------------------------------------
2626

2727
import sys
28+
from azure.core.pipeline import PipelineRequest
2829
from azure.core.rest import HttpRequest, HttpResponse
2930
from azure.core import PipelineClient
3031
from azure.core.pipeline.policies import RedirectPolicy
@@ -159,3 +160,30 @@ def example_proxy_policy():
159160
# You can also configure proxies by setting the environment variables
160161
# HTTP_PROXY and HTTPS_PROXY.
161162
# [END proxy_policy]
163+
164+
165+
def test_example_per_call_policy():
166+
"""Per call policy example.
167+
168+
This example shows how to define your own policy and inject it with the "per_call_policies" parameter.
169+
"""
170+
from azure.core.pipeline.policies import SansIOHTTPPolicy
171+
172+
# Define your own policy
173+
class MyPolicy(SansIOHTTPPolicy[HttpRequest, HttpResponse]):
174+
def on_request(self, request: PipelineRequest[HttpRequest]) -> None:
175+
# Simple hook that redirect google calls to bing :).
176+
current_url = request.http_request.url
177+
request.http_request.url = current_url.replace("google", "bing")
178+
179+
# Replace "PipelineClient" by your actual client (KeyVault, Storage, etc.)
180+
# "per_call_policies" is available on any client this team produces.
181+
client: PipelineClient[HttpRequest, HttpResponse] = PipelineClient(
182+
base_url="https://google.com", per_call_policies=MyPolicy()
183+
)
184+
# This part will be done by your client
185+
request = HttpRequest("GET", "https://google.com/")
186+
response: HttpResponse = client.send_request(request)
187+
188+
# Checking that the response is coming from bing.
189+
assert "bing" in response.url

0 commit comments

Comments
 (0)