AIP-231

Batch methods: Get

Some APIs need to allow users to get a specific set of resources at a consistent time point (e.g. using a read transaction). A batch get method provides this functionality.

Guidance

APIs may support Batch Get using the following pattern:

rpc BatchGetBooks(BatchGetBooksRequest) returns (BatchGetBooksResponse) {
  option (google.api.http) = {
    get: "/v1/{parent=publishers/*}/books:batchGet"
  };
}
  • The RPC’s name must begin with BatchGet. The remainder of the RPC name should be the plural form of the resource being retrieved.
  • The request and response messages must match the RPC name, with -Request and -Response suffixes.
  • The HTTP verb must be GET.
  • The HTTP URI must end with :batchGet.
  • The URL path should represent the collection for the resource, matching the collection used for simple CRUD operations. If the operation spans parents, a dash (-) may be accepted as a wildcard.
  • There must not be a body key in the google.api.http annotation.
  • The operation must be atomic: it must fail for all resources or succeed for all resources (no partial success). For situations requiring partial failures, List (AIP-132) methods should be used.
    • If the operation covers multiple locations and at least one location is down, the operation must fail.

Request message

The request for a batch get method should be specified with the following pattern:

message BatchGetBooksRequest {
  // The parent resource shared by all books being retrieved.
  // Format: publishers/{publisher}
  // If this is set, the parent of all of the books specified in `names`
  // must match this field.
  string parent = 1;
  // The names of the books to retrieve.
  // A maximum of 1000 books can be retrieved in a batch.
  // Format: publishers/{publisher}/books/{book}
  repeated string names = 2;
}
  • A parent field should be included to facilitate inclusion in the URI as well to permit a single permissions check. If a caller sets this field, and the parent collection in the name of any resource being retrieved does not match, the request must fail.
    • This field should be required if only 1 parent per request is allowed.
  • The request message must include a repeated field which accepts the resource names specifying the resources to retrieve. The field should be named names.
  • Other fields besides name may be “hoisted” from the standard Get request. There is no way to allow for these fields to accept different values for different resources; if this is needed, use the alternative request message form.
  • Batch get should not support pagination because transactionality across API calls would be extremely difficult to implement or enforce, and the request defines the exact scope of the response anyway.
  • The request message must not contain any other required fields, and should not contain other optional fields except those described in this or another AIP.
  • The comment above the names field should document the maximum number of requests allowed.

Response message

The response for a batch get method should be specified with the following pattern:

message BatchGetBooksResponse {
  // Books requested.
  repeated Book books = 1;
}
  • The response message must include one repeated field corresponding to the resources being retrieved.
  • The order of books in the response must be the same as the names in the request.

Nested request objects

If the standard Get request message contains a field besides the resource name that needs to be different between different resources being requested, the batch message may alternatively hold a repeated field of the standard Get request message. This is generally discouraged unless your use case really requires it.

The request for a batch get method using this approach should be specified with the following pattern:

message BatchGetBooksRequest {
  // The parent resource shared by all books being retrieved.
  // Format: publishers/{publisher}
  // If this is set, the parent field in the GetBookRequest messages
  // must either be empty or match this field.
  string parent = 1;

  // The requests specifying the books to retrieve.
  // A maximum of 1000 books can be retrieved in a batch.
  repeated GetBookRequest requests = 2;
}
  • A parent field should be included. If a caller sets this field, and the parent collection in the name of any resource being retrieved does not match, the request must fail.
    • This field should be required if only 1 parent per request is allowed.
  • The request message must include a repeated field which accepts the request messages specifying the resources to retrieve, as specified for standard Get methods. The field should be named requests.
  • Other fields may be “hoisted” from the standard Get request, which means that the field can be set at either the batch level or child request level. Similar to parent, if both the batch level and child request level are set for the same field, the values must match.
  • Batch get should not support pagination because transactionality across API calls would be extremely difficult to implement or enforce, and the request defines the exact scope of the response anyway.
  • The request message must not contain any other required fields, and should not contain other optional fields except those described in this or another AIP.
  • The comment above the requests field should document the maximum number of requests allowed.

Changelog

  • 2019-09-11: Changed the primary recommendation to specify a repeated string instead of a repeated standard Get request message. Moved the original recommendation into its own section.
  • 2019-08-01: Changed the examples from “shelves” to “publishers”, to present a better example of resource ownership.