In REST APIs, it is customary to make a POST
request to a collection's URI (for example, /v1/publishers/{publisher}/books
) in order to create a new resource within that collection.
Resource-oriented design (AIP-121) honors this pattern through the Create
method. These RPCs accept the parent collection and the resource to create (and potentially some other parameters), and return the created resource.
APIs should generally provide a create method for resources unless it is not valuable for users to do so. The purpose of the create method is to create a new resource in an already-existing collection.
Create methods are specified using the following pattern:
rpc CreateBook(CreateBookRequest) returns (Book) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*}/books"
body: "book"
};
option (google.api.method_signature) = "parent,book";
}
Create
. The remainder of the RPC name should be the singular form of the resource being created.Request
suffix.CreateBookResponse
.
google.longrunning.Operation
which resolves to the resource itself.POST
.parent
, and should be the only variable in the URI path.books
in the above example) must be a literal string.body
key in the google.api.http
annotation, and it must map to the resource field in the request message.
google.api.method_signature
annotation, with a value of "parent,{resource},{resource}_id"
, or ""parent,{resource}"
if the resource ID is not required.Create methods implement a common request message pattern:
message CreateBookRequest {
// The parent resource where this book will be created.
// Format: publishers/{publisher}
string parent = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
child_type: "library.googleapis.com/Book"
}];
// The ID to use for the book, which will become the final component of
// the book's resource name.
//
// This value should be 4-63 characters, and valid characters
// are /[a-z][0-9]-/.
string book_id = 2 [(google.api.field_behavior) = REQUIRED];
// The book to create.
Book book = 3 [(google.api.field_behavior) = REQUIRED];
}
parent
field must be included unless the resource being created is a top-level resource. It should be called parent
.
{resource}_id
field must be included for management plane resources, and should be included for data plane resources.Some resources take longer to create a resource than is reasonable for a regular API request. In this situation, the API should use a long-running operation (AIP-151) instead:
rpc CreateBook(CreateBookRequest) returns (google.longrunning.Operation) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*}/books"
body: "book"
};
option (google.longrunning.operation_info) = {
response_type: "Book"
metadata_type: "OperationMetadata"
};
}
response_type
and metadata_type
fields must be specified.Important: Declarative-friendly resources (AIP-128) should use long-running operations. The service may return an LRO that is already set to done if the request is effectively immediate.
User-specified IDsAn API must allow a user to specify the ID component of a resource (the last segment of the resource name) on creation if the API is operating on the management plane.
On the data plane, an API should allow a user to specify the ID. Exceptional cases should have the following behavior:
An API may allow the {resource}_id
field have the field_behavior OPTIONAL
, and generate a system-generated ID if one is not specified.
For example:
// Using user-specified IDs.
publishers/lacroix/books/les-miserables
// Using system-generated IDs.
publishers/012345678-abcd-cdef/books/12341234-5678-abcd
{resource}_id
field must exist on the request message, not the resource itself.
name
field on the resource must be ignored.google.api.method_signature
annotation on the RPC, with a value of "parent,{resource},{resource}_id"
if the resource being created is not a top-level resource, or with a value of "{resource},{resource}_id"
if the resource being created is a top-level resource.ALREADY_EXISTS
.
PERMISSION_DENIED
instead.Note: For REST APIs, the user-specified ID field, {resource}_id
, is provided as a query parameters on the request URI.
See errors, in particular when to use PERMISSION_DENIED and NOT_FOUND errors.
Further readingCreate
methods, see AIP-155.Declarative clients use the resource ID as a way to identify a resource for applying updates and for conflict resolution. The lack of a user-specified ID means a client is unable to find the resource unless they store the identifier locally, and can result in re-creating the resource. This in turn has a downstream effect on all resources that reference it, forcing them to update to the ID of the newly-created resource.
Having a user-specified ID also means the client can precalculate the resource name and use it in references from other resources.
ChangelogRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.5