When the GET endpoint is called, it returns a 200 status code with a response body. But on every third call to the API, a different status should be returned with empty response body. How to track the count on the API calls?
Response for first and second calls:
Status Code: 200
Response: {
message: "OK"
}
Response for third call to the API:
Status Code: 503
Response: {
}
Under normal circumstances, in order to meet security and business logic needs, we often set relevant access limit in the interface. However, access restrictions on interfaces usually not only limit the number of accesses, but you can also use a dictionary to store IP addresses and set the corresponding number of accesses to limit the number of accesses to user IP addresses and other identifiers, so that the system can be more comprehensive. and comprehensive. In your problem description, you can use a variable to save the number of calls. This count is incremented each time the API is called. When the count reaches 3 times, modify the returned status code and response body. The variable is incremented every time the API is called. Then, allow calling logic that returns a different status code depending on the value of the variable, and below is a use case that you can use as a reference:
If you're using a distributed system, you could store cached data in memory that is shared across all instances of your application cluster. Then you can use AddDistributedMemoryCache so that different application instances can share the same cached data. It uses ASP.NET Core's distributed caching mechanism and is suitable for distributed applications:
When I used the get method the first two times:
When I used the get method for the third time: