Quota API Policy
Upasana Ghosh
346
Reputation points
As per https://learn.microsoft.com/en-us/rest/api/apimanagement/current-ga/quota-by-counter-keys/list-by-service?tabs=HTTP this document , we can have counter keys in Authorization header while calling
I so , i want to get alert when the quota reaches 90%
I have implemented the below policy but that is not working fine as expected
So i want to send the authorization request url, fetch the value from counter key and then if the counter is less 10% , then quota alert should be there
what exactly am i missing in this policy ?
<rate-limit-by-key calls="10" renewal-period="1" counter-key="@( "ABC0034" + context.Request.Headers.GetValueOrDefault("ConsumerID").ToString())" />
<rate-limit-by-key calls="10" renewal-period="1" counter-key="11111-ETE" />
<quota-by-key calls="10000" renewal-period="86400" counter-key="@( "22222" + context.Request.Headers.GetValueOrDefault("ConsumerID").ToString())" />
<set-backend-service base-url="https://apim-hub-.azure-api.net/00001/api/Todo/{id}" />
<send-request ignore-error="true" timeout="20" response-variable-name="spref" mode="new">
<set-url>https://management.azure.com/subscriptions/7111/resourceGroups/rg-apim-/providers/Microsoft.ApiManagement/service/apim-hub/quotas/12345?api-version=2021-08-01</set-url>
<set-method>GET</set-method>
<set-header name="Authorization" exists-action="override">
<value>counter-key</value>
</set-header>
</send-request>
<set-variable name="ConsumerIDfromJWT" value="@((String)((IResponse)context.Variables["spref"]).Body.As<JObject>()["counter-key"])" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault("counter-key", 0) < 10)">
<return-response>
<set-status code="429" reason="Quota Alert" />
<set-header name="Quota-Alert-Reason" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>"Developer overrate"</set-body>
</return-response>
</when>