Detect Google Maps Api Quota Limit
Solution 1:
I assume you want to find out if you've exceeded the Google Maps API Web Services request limit. Here's what the official documentation says:
Usage limits exceeded
If you exceed the usage limits you will get an OVER_QUERY_LIMIT status code as a response.
This means that the web service will stop providing normal responses and switch to returning only status code OVER_QUERY_LIMIT until more usage is allowed again. This can happen:
- Within a few seconds, if the error was received because your application sent too many requests per second.
- Some time in the next 24 hours, if the error was received because your application sent too many requests per day. The time of day at which the daily quota for a service is reset varies between customers and for each API, and can change over time.
Upon receiving a response with status code OVER_QUERY_LIMIT, your application should determine which usage limit has been exceeded. This can be done by pausing for 2 seconds and resending the same request. If status code is still OVER_QUERY_LIMIT, your application is sending too many requests per day. Otherwise, your application is sending too many requests per second.
And a code sample:
url = "MAPS_API_WEBSERVICE_URL"
attempts = 0
success = Falsewhile success != Trueand attempts < 3:
raw_result = urllib.urlopen(url).read()
attempts += 1# The GetStatus function parses the answer and returns the status code# This function is out of the scope of this example (you can use a SDK).
status = GetStatus(raw_result)
if status == "OVER_QUERY_LIMIT":
time.sleep(2)
# retrycontinue
success = Trueif attempts == 3:
# send an alert as this means that the daily limit has been reachedprint"Daily limit has been reached"
Quoted from the Google Maps documentation: https://developers.google.com/maps/documentation/business/articles/usage_limits#limitexceeded
TLDR: Make a request, if you get OVER_QUERY_LIMIT response, try again in two seconds. If the response is still the same, you've hit the daily limit.
Solution 2:
I came up with a solution based on the observation that the Google error message contains an element with the dismissButton
class attribute. With jQuery, it is something like
var quota_exceeded = false;
$(document).ready( function() {
setTimeout(check_quota, 1000);
setTimeout(check_quota, 3000);
setTimeout(check_quota, 10000);
});
functioncheck_quota() {
if (quota_exceeded) return;
if (typeof($("button.dismissButton")[0]) === "undefined") return;
quota_exceeded = true;
// you can even hijack the box and add to Google's message
$("button.dismissButton").parents("td").html(
"We have exceeded our quota!!!!!<p>" +
"<img src='https://c1.staticflickr.com/8/7163/6852688771_c90c9887c3.jpg'>" +
"<p><button class='dismissButton'>OK</button>");
$("button.dismissButton").click( function() {
$("button.dismissButton").parentsUntil("div").parent().hide();
});
}
Post a Comment for "Detect Google Maps Api Quota Limit"