检测 Google Maps API 配额限制 [英] Detect Google Maps API quota limit

查看:24
本文介绍了检测 Google Maps API 配额限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果达到 Google Maps Javascript API V3 的配额,有人知道通过 Javascript 或 HTTP 请求进行测试的方法吗?获取为用户显示的错误消息就足够了.

我们启用了计费功能,并为 Google Maps Javascript API v3 设置了大约 100,000 个配额,但有时我们会破坏它,有时甚至没有意识到这一点.现在我们喜欢使用 nagios 之类的工具(可能通过使用 phantomjs)来监控我们的 api 访问以获得即时警告.但是我找不到任何关于如何测试的信息,如果我们的配额已超过.

更新 2

此处发现了类似的请求:

TLDR:发出请求,如果收到 OVER_QUERY_LIMIT 响应,请在两秒后重试.如果响应仍然相同,则您已达到每日限制.

Does anyone know a way to test via Javascript or a HTTP-Request, if the quota of Google Maps Javascript API V3 is reached? Getting the error-message, which is displayed for the user, would be sufficient.

We enabled billing and have a quota for Google Maps Javascript API v3 on around 100,000, but sometimes we break it and sometimes even do not realize it. Now we like to monitor our api access with a tool like nagios (perhaps by using phantomjs) to get an instant warning. But I could not find any information on how to test, if our quota has exceeded.

Update2

A similar request ist found here: How to detect that the 25000 request per day limit has been reached in Google Maps?

But there is only a reference to the API-Console. If someone managed to do a fallback to static maps using Javascript, I could modify the script for my case.

Update

Here is an example of the localized quota error-message by google maps, which appears for the user instead of a map. I would prefer to remove the maps container if something like this happens:

解决方案

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 = False

while success != True and 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)
    # retry
    continue
  success = True

if attempts == 3:
  # send an alert as this means that the daily limit has been reached
  print "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.

这篇关于检测 Google Maps API 配额限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆