Skip to content

Commit 86f265a

Browse files
authored
Merge pull request #134 from sushaanttb/feature/policy-examples
Policy example for Replaying request
2 parents 6ce6d56 + 6a31fbc commit 86f265a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Overview
3939
| <a href="Query CosmosDB.policy.xml">Query CosmosDB</a> | Query CosmosDB |
4040
| <a href="Random load balancer.policy.xml">Random load balancer</a> | Randomly routes (load balances) to one of the two backends |
4141
| <a href="Random load balancer simpler.policy.xml">Random load balancer simpler</a> | Randomly overrides API base url using weighted value to effectively traffic split for canary deployments |
42+
| <a href="Replay request on error.policy.xml">Replay request on error</a> | Demonstrates a pattern of replaying the same request again in case of any error.
4243
| <a href="Request OAuth2 access token from SAP using AAD JWT token.xml">Request OAuth2 access token from SAP using AAD JWT token</a> | Use this policy to perform SAP Principal Propagation (exchange AAD issued JWT token for SAP issued Bearer token), deal with X-CSRF and cache token till SAP defined expiry. It applies to all NetWeaver based systems and relies on the SAP OAuth server. Find the associated community post [here](https://blogs.sap.com/2021/08/12/.net-speaks-odata-too-how-to-implement-azure-app-service-with-sap-odata-gateway/). |
4344
| <a href="Request OAuth2 access token from SuccessFactors using AAD JWT token.xml">Request OAuth2 access token from SAP SuccessFactors using AAD JWT token</a> | Use this policy to perform SAP Principal Propagation (exchange Entra ID issued JWT token for SAP SuccessFactors issued Bearer token), deal with X-CSRF and cache token till SAP defined expiry. Find the associated community post [here](https://community.powerplatform.com/blogs/post/?postid=c6a609ab-3556-ef11-a317-6045bda95bf0). |
4445
| <a href="Return HTTP 405 if the HTTP Method of the request is not defined.xml">Return HTTP 405 if the HTTP Method of the request is not defined</a> | Use this policy to translates the HTTP status code from 404 to 405 when the HTTP Method of the request doesn't match the one defined in the corresponding Operation. |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--
2+
This policy sample demonstrates a pattern of replaying a request.
3+
For eg: one scenario could be when we are using quota and rate limit policies and for some special tier consumers,
4+
we would like to serve the request even if there was a quota error but with a different backend.
5+
In such cases, we can replay the request to a different backend with the same request headers and body.
6+
The "example-policy-name" string in the when condition is just for example and needs to be replaced with the actual policy name accordingly.
7+
8+
Important Note:
9+
Although this pattern works, an important point to note here is since we are now replaying the request
10+
we would end up having 2 log entries for the same request in the APIM logs.
11+
-->
12+
<policies>
13+
<inbound>
14+
<base />
15+
</inbound>
16+
<backend>
17+
<base />
18+
</backend>
19+
<outbound>
20+
<base />
21+
</outbound>
22+
<on-error>
23+
<base />
24+
<choose>
25+
<when condition="@(context.LastError.Source == "example-policy-name")">
26+
<set-variable name="replay-url" value="@("http://" + context.Request.Headers.GetValueOrDefault("X-Original-Host") + context.Request.Headers.GetValueOrDefault("X-Original-Url"))" />
27+
<set-variable name="original-error-source" value="@(context.LastError.Source)" />
28+
<set-variable name="original-error-reason" value="@(context.LastError.Reason)" />
29+
30+
<!-- Following snipped demonstrates the replya pattern,
31+
For this, we are using send request's "copy" mode which would copy the original request's headers and body accoridngly.
32+
Policy Documentation link https://learn.microsoft.com/en-us/azure/api-management/send-request-policy#attributes
33+
-->
34+
<!-- Note:
35+
As per the policy documentation, the send request method will copy the request headers and body accordingly
36+
but we should always validate this and we can expclitly set the the request body and headers as needed.
37+
-->
38+
<send-request mode="copy" response-variable-name="replay-response" timeout="60" ignore-error="false">
39+
<set-url>@(context.Variables.GetValueOrDefault<string>("replay-url"))</set-url>
40+
<!-- While replaying the request this time, we might need to pass some additional information
41+
this time in our request with corresponding logic in the inbound section to process such requests
42+
The string "skip-example-policy-error" here is just an example of additional header we are sending here
43+
-->
44+
<set-header name="skip-example-policy-error" exists-action="override">
45+
<value>true</value>
46+
</set-header>
47+
</send-request>
48+
<!-- THIS IS AN IMPORTANT STEP i.e. the response captured in the send-request policy attribute
49+
"response-variable-name" defined above has to be explicity sent back
50+
-->
51+
<return-response response-variable-name="replay-response">
52+
<!-- Any additional headers like below can further be added as part of response tracking -->
53+
<set-header name="x-request-replayed" exists-action="override">
54+
<value>true</value>
55+
</set-header>
56+
<set-header name="x-original-error-source" exists-action="override">
57+
<value>@(context.Variables.GetValueOrDefault<string>("original-error-source"))</value>
58+
</set-header>
59+
<set-header name="x-original-error-reason" exists-action="override">
60+
<value>@(context.Variables.GetValueOrDefault<string>("original-error-reason"))</value>
61+
</set-header>
62+
</return-response>
63+
</when>
64+
<otherwise>
65+
<!-- Any additional headers like below can further be added as part of response tracking -->
66+
<set-header name="x-request-replayed" exists-action="override">
67+
<value>"false"</value>
68+
</set-header>
69+
</otherwise>
70+
</choose>
71+
</on-error>
72+
</policies>

0 commit comments

Comments
 (0)