Improve API Performance using policies (Part 2)
In the previous blog, I wrote about using Cache feature to improve the performance of APIs. I am going to share some more information around performance improvements of APIs. Generally we would have an API which services a wide range of requirements. However, when it comes to consumption by a particular vendor or person, not all properties/fields are required. Restricting the amount of data retrieved from the backend system for a particular vendor or person can significantly reduce the load on the backend SAP systems.
In this blog, I am going to use the same GWDEMO API which was used earlier and introduce a new API policy called “Assign Message”.
“Assign Message” policy allows to either create a new HTTP request/response message or modify existing ones. In this blog, I am going to show how you can modify an existing HTTP request.
Navigate to the resources tab and click on the “ProductCollection” entity set.
This will execute the API to retrieve all the Products in the backend system. To make things easy to read, I have added a JSON format in the URL
The system returns hundreds of products with each of them having over 25 properties and I might have a consumer who is only interested in few fields (those highlighted) and particularly interested in products which have a unit price less than 1000$.
To enforce this setting, navigate back to the API and click on the “policies” link
On the left hand side, select the “ProductCollection” flow under “ProyEndPoint”. Click on the + sign next to Assign Message policy on the right hand side to add this new policy. This policy will be specifically triggered everytime “ProductCollection” is requested through the API.
Provide the policy name and select the Stream as “Incoming request”
Remove the XML code and paste the below code.
<!-- 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'> <Add> <QueryParams> <QueryParam name="$select">ProductKey,ProductName,UnitPrice,SupplierID</QueryParam> <QueryParam name="$filter">UnitPrice lt 1000</QueryParam> </QueryParams> </Add> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="request"></AssignTo> </AssignMessage>
We are adding two Query parameters $select (to list the properties which are being fetched) and $filter (to restrict the output with a condition – Unit Price less than 100$) to the existing request type.
Update the policy and save the API. Navigate to the resources tab and click on the “ProductCollection” link.
This will open a new browser window and execute the API call. To make this easy to read, I have added format as JSON parameter.
Notice that the response contains only four properties and the response is filtered to only provide products with Unit Price less than a value of 1000$.
Using “Assign Message” policy you can easily change your incoming HTTP requests and also assign values to custom variables which can be used by other policies in the flow.
Hope you found this useful.