List
In REST APIs, it is customary to make a GET request to a resource
collection’s URI (for example, /publishers/{publisher_id}/books) in order to
retrieve a list of the resources within that collection.
Resource-oriented design AEP-121 honors this pattern through the List method.
These RPCs accept a parent collection (if one exists), and return a list of
responses matching that input.
Guidance
Section titled “Guidance”APIs should provide a List method for resource collections.The purpose of
the List method is to return data from a finite collection (generally
singular unless the operation supports reading across collections).
When the GET method is used on a URI ending in a resource collection, the
result must be a list of resources.
Operation
Section titled “Operation”rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) { option (google.api.http) = {get: "/{parent=publishers/*}/books"};
option (google.api.method_signature) = "parent"; }-
The RPC’s name must begin with the word
List. The remainder of the RPC name should be the plural form of the resource’s message name. -
The request message must match the RPC name, with a
-Requestsuffix. -
The response message must match the RPC name, with a
-Responsesuffix.- The response should include fully-populated resources unless there is a reason to return a partial response (see AEP-157).
-
The HTTP verb must be
GET. -
If the collection has a parent resource, The URI must contain a field corresponding to the collection parent’s name.
- This field must be called
parent. - The URI must have a variable corresponding to this field.
- The
parentfield must be the only variable in the URI path. All remaining parameters must map to URI query parameters.
- This field must be called
-
There must not be a
bodykey in thegoogle.api.httpannotation. -
There should be exactly one
google.api.method_signatureannotation with a value of"parent"if a parent exists, and an empty string otherwise.
GET /v1/publishers/{publisher_id}/books HTTP/2Host: bookstore.example.comAccept: application/jsonList operations must be made by sending a GET request to the resource
collection’s URI:
- The HTTP method must be
GET.- The request must be safe and must not have side effects.
- There must not be a request body.
- If a
GETrequest contains a body, the body must be ignored, and must not cause an error.
- If a
- The request must not require any fields in the query string.
- The query string may include fields for common design patterns relevant
to list methods, such as
string filterandstring orderBy.
- The query string may include fields for common design patterns relevant
to list methods, such as
- The URI should contain a variable for each individual ID in the resource
hierarchy.
-
The path parameter for all resource IDs must be in the form
{resourceName}_id(such asbook_id), and path parameters representing the ID of parent resources must end withId.
-
Requests
Section titled “Requests”message ListBooksRequest { // A field for the parent of book string parent = 10013 [ (aep.api.field_info) = { field_behavior: [FIELD_BEHAVIOR_REQUIRED] }, (google.api.field_behavior) = REQUIRED ];
// The page token indicating the starting point of the page string page_token = 10010 [json_name = "page_token"];
// The maximum number of resources to return in a single page. int32 max_page_size = 10017 [json_name = "max_page_size"];}- A
parentfield must be included unless the resource being listed is a top-level resource. It should be calledparent.- The field must be annotated as required.
- The field must identify the resource type of the resource being
listed with a
aepi.api.field_info.resource_referenceannotation. - The field must accept the parent path, not the id
- The
max_page_sizeandpage_tokenfields, which support pagination, must be specified on all list request messages. For more information, see AEP-158.
Note: The parent field in the request object corresponds to the parent
variable in the google.api.http annotation on the RPC. This causes the
parent field in the request to be populated based on the value in the URL
when the REST/JSON interface is used.
paths: /publishers/{publisher_id}/books: get: parameters: - in: path name: publisher_id required: true schema: type: string - in: query name: max_page_size schema: type: integer - in: query name: page_token schema: type: stringResponses
Section titled “Responses”List operations must return a page of results, with each individual
result being a resource.
- The array of resources must be named
resultsand contain resources with no additional wrapping. - The
string nextPageTokenfield must be included in the list response schema. It must be set if there are subsequent pages, and must not be set if the response represents the final page. For more information, see AEP-158. - The response struct may include a
int32 total_size(orint64 total_size) field with the number of items in the collection.- The value may be an estimate (the field should clearly document this if so).
- If filtering is used, the
total_sizefield should reflect the size of the collection after the filter is applied.
- The response message must include a field corresponding to the resources being returned, named for the English plural term for the resource, and should not include any other repeated fields.
- Fields providing metadata about the list request (such as
string next_page_tokenorint32 total_size) must be included on the response (not as part of the resource itself).
message ListBooksResponse { // A list of books repeated Book results = 10016;
// The page token indicating the ending point of this response. string next_page_token = 10011 [json_name = "next_page_token"];
// A list of books that were not reachable. repeated Book unreachable = 10019;}paths: /publishers/{publisher_id}/books: get: responses: '200': content: application/json: schema: properties: next_page_token: type: string results: items: $ref: '#/components/schemas/book' type: array unreachable: items: type: string type: array type: object description: Successful response-
The field “results” must be an array of resources, with the schema as a reference to the resource (e.g.
#/components/schemas/Book). -
The field “nextPageToken” must be a string that contains the token to retrieve the next page. This must not be set if the response represents the final page.
Errors
Section titled “Errors”If the user does not have sufficient permission to know that the collection exists, the service should reply with an HTTP 404 error, regardless of whether or not the collection exists. Permission must be checked prior to checking whether the collection exists.
If the user does have proper permission, but the requested collection does not exist (generally because the parent does not exist), the service must reply with an HTTP 404 error.
Advanced operations
Section titled “Advanced operations”List methods may allow an extended set of functionality to allow a user
to specify the resources that are returned in a response.
The following table summarizes the applicable AEPs, ordered by the precedence of the operation on the results.
| Operation | AEP |
|---|---|
| filter | AEP-160 |
ordering (order_by) | AEP-132 |
pagination (next_page_token) | AEP-158 |
| skip | AEP-158 |
For example, if both the filter and skip fields are specified, then the
filter would be applied first, then the resulting set would be the results that
skip N entries of the filtered result.
Ordering
Section titled “Ordering”List methods may allow clients to specify sorting order; if they do, the
request message should contain a string order_by field.
- Values should be a comma separated list of fields. For example:
"foo,bar". - The default sorting order is ascending. To specify descending order for a
field, users append a
-prefix; for example:"foo,-bar","-foo,bar". - Redundant space characters in the syntax are insignificant.
"foo, -bar"," foo , -bar ", and"foo,-bar"are all equivalent. - Subfields are specified with a
.character, such asfoo.baroraddress.street.
Soft-deleted resources
Section titled “Soft-deleted resources”Some APIs need to “soft-delete” resources, marking them as deleted or pending deletion (and optionally purging them later).
APIs that do this should not include deleted resources by default in list
requests. APIs with soft deletion of a resource should include a
bool show_deleted field in the list request that, if set, will cause
soft-deleted resources to be included.
Interface Definitions
Section titled “Interface Definitions”rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) { option (google.api.http) = {get: "/{parent=publishers/*}/books"};
option (google.api.method_signature) = "parent"; }message ListBooksRequest { // A field for the parent of book string parent = 10013 [ (aep.api.field_info) = { field_behavior: [FIELD_BEHAVIOR_REQUIRED] }, (google.api.field_behavior) = REQUIRED ];
// The page token indicating the starting point of the page string page_token = 10010 [json_name = "page_token"];
// The maximum number of resources to return in a single page. int32 max_page_size = 10017 [json_name = "max_page_size"];}message ListBooksResponse { // A list of books repeated Book results = 10016;
// The page token indicating the ending point of this response. string next_page_token = 10011 [json_name = "next_page_token"];
// A list of books that were not reachable. repeated Book unreachable = 10019;}paths: /publishers/{publisher_id}/books: get: description: List method for book operationId: ListBook parameters: - in: path name: publisher_id required: true schema: type: string - in: query name: max_page_size schema: type: integer - in: query name: page_token schema: type: string responses: '200': content: application/json: schema: properties: next_page_token: type: string results: items: $ref: '#/components/schemas/book' type: array unreachable: items: type: string type: array type: object description: Successful response