Я разрабатываю простой API со стандартом GAE и Cloud Endpoint, чтобы протестировать доступную функцию квот.
Вот код Java с использованием аннотации EndPoint Framework:
package com.example.skeleton;
import com.google.api.server.spi.config.*;
@Api(
name = "simpleapi",
version = "v1",
limitDefinitions = {
@ApiLimitMetric(
name = "read-requests",
displayName = "Read requests",
limit = 10
)
}
)
public class MyApi {
@ApiMethod(
name = "hellosecuredWithQuotas",
path = "hellosecuredWithQuotas",
httpMethod = ApiMethod.HttpMethod.GET,
apiKeyRequired= AnnotationBoolean.TRUE,
metricCosts = {
@ApiMetricCost(
name ="read-requests",
cost = 1
)
}
)
public Message helloSecuredWithQuotas(@Named("name") String name) {
return new Message("hello " + name);
}
}
Таким образом, учитывая аннотации @Api, квоты составляют 10 запросов в минуту.
Я развернул приложение в GAE.
Я сгенерировал jAP-файл OpenAPI (см. Ниже сгенерированный контент) и развернул его в Cloud Endpoint с помощью CLI gcloud.
Наконец, я использую сгенерированный клиент для вызова конечной точки в цикле, который вызывает конечную точку более 10 раз в минуту.
... но, к сожалению, я никогда не получаю ожидаемый «код состояния HTTP 429 Too Many Requests».
public class App {
public static void main(String []args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Simpleapi simpleapi = new Simpleapi.Builder(httpTransport, jsonFactory, null)
.setApplicationName("test")
.build();
for (int i = 0; i < 1000 ; i++) {
Message titi = simpleapi.hellosecuredWithQuotas("foobar" + System.currentTimeMillis()).setKey("my-api-key-here").execute();
System.out.println(titi.getMessage());
}
}
}
Вот сгенерированный файл openapi.json:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "my-project-name-here.appspot.com"
},
"host": "my-project-name-here.appspot.com",
"basePath": "/_ah/api",
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/simpleapi/v1/hellosecuredWithQuotas": {
"get": {
"operationId": "SimpleapiHelloSecuredWithQuotas",
"parameters": [
{
"name": "name",
"in": "query",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "A successful response",
"schema": {
"$ref": "#/definitions/Message"
}
}
},
"security": [
{
"api_key": []
}
],
"x-google-quota": {
"metricCosts": {
"read-requests": 10
}
}
}
}
},
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "key",
"in": "query"
}
},
"definitions": {
"Message": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
},
"x-google-management": {
"metrics": [
{
"name": "read-requests",
"valueType": "INT64",
"metricKind": "GAUGE"
}
],
"quota": {
"limits": [
{
"name": "read-requests",
"metric": "read-requests",
"values": {
"STANDARD": 10
},
"unit": "1/min/{project}",
"displayName": "Read requests"
}
]
}
}
}