Scenario: You want to send custom message using fault rules in case of Quota error
Steps:
1. Add an instance of Quota in Preflow for incoming request.
2. Modify the code as per requirement.
Code (Allow 2 calls/1 min.)
<Quota async="false" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<Allow count="2"/>
<Interval>1</Interval>
<Distributed>true</Distributed>
<Synchronous>true</Synchronous>
<TimeUnit>minute</TimeUnit>
</Quota>
3. Add a policy for Assign Message and delete it from flow. Though it is deleted from flow, it will remain under Created Policies.
<!-- This policy can be used to create or modify the standard HTTP request and response messages -->
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<!-- Sets a new value to the existing parameter -->
<Set>
<Payload contentType="application/json">{
"errorCode": "500",
"errorMessage": "Quota exceeded. Try again later"}
</Payload>
</Set>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="response"/>
</AssignMessage>
5. Now export the API and Edit APIProxy -> APIProxyEndPoint -> default.xml and add the fault rules as below.
<faultRules>
<faultRule>
<name>Quota Error</name>
<condition>(fault.name = "QuotaViolation")</condition>
<steps>
<step>
<policy_name>AssignMessage</policy_name>
#condition is specified as fault variable - ratelimit.policy_name.failed
<condition>ratelimit.QuotaPolicy.failed=true</condition>
<sequence>1</sequence>
</step>
</steps>
</faultRule>
</faultRules>
6. Now zip the APIProxy folder structure and Import in SAP APIM.
7. Test the API.


No comments:
Post a Comment