Reputation: 484
I'm accessing an API from an android app and I pay per call. Its a word dictionary API so the requests usually change each time. What would be a good method to restrict the number of calls per user (say by day or hour) so I can be sure costs do not explode?
The way I am making the API call is:
import javax.net.ssl.HttpsURLConnection;
URL url = new URL(urlString);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
// read the output from the server and do things
}
I think I could use a variable in sharedPreferences
where I count the number of API calls, block the calls after a certain amount, and reset the count each day. But I wonder if there is a best practices method for this sort of thing that I have missed in my Googling.
I would be happy to use another library like Volley if it has functionality to make restricting API calls easier.
Upvotes: 0
Views: 1117
Reputation: 41
Few thoughts on this:
1) Drive the limit using a server-configurable value. Get this value during one of your initial API calls (E.g: during app launch) and then save it in SharedPreferences. This is because in future in case you would like to remove or adjust the limit you can do so without changing your app code.
2) Analyze how this is going to impact the user behavior. Are you planning on notifying the user that the functionality is not working due to API limitations? Or ask them to upgrade for a no-limit functionality ?
3) Also caching the previous results (as pointed out in previous answer)
Upvotes: 1