App Engine Cloud
Endpoints is a
great way to quickly and easily create JSON API endpoints. What’s not clear is
how to structure your
Message code to support a RESTful
create-read-update-delete (CRUD) API. This article will show the basic CRUD
operations for one Resource. The results can easily be adapted to support a full
To support this discussion let’s use a concrete resource for our API – a
resource. We can give our
User model a few simple attributes.
A CRUD API for this resource would support a URL structure and HTTP verbs for each operation.
Given this model we can define a basic Cloud Endpoints message representing a
Now we can write the C (create) portion of the CRUD API using HTTP POST
ResourceContainer to hold the message we wish to submit to the API.
Similarly we can define the R (read) portion of the API using an HTTP GET
method. To parameterize our cloud endpoint we need to add the parameter to our
ResourceContainer. I’ll call it
id here. The actual message type is
VoidMessage because we are not passing any information in our request to the
API endpoint other than the
The response retrieves the entity from the datastore and returns it as a message.
The U (update) operation uses a similar parameterized
access a User given an id. We augment this request with the
defines the content of the body of the message. The endpoint takes the content
of the message and updates the entity with that content.
Lastly, the D (delete) endpoint takes an identifier which we have previously
ID_RESOURCE. The endpoint deletes the entity referred to by that
identifier and returns a
VoidMessage which is converted to an
HTTP 204 No Content response by the cloud endpoints API.
This basic pattern can be used with any resource that your API wishes to support and gives a basic pattern with which to build out your full API.
If you have any questions please send me an email or let me know in the comments!