This post finishes with the tutorial about introducing Google Cloud Endpoints. As explained in the previous two parts, we plan to teach you how to construct a simple web backend storing data about posts and then expose it by means of a public REST-based API. We explained in the first post of the tutorial how to set up the environment. Then, we detailed how to create the core application with the endpoints to deal with posts objects. Finally, in this third post in the series we will explain how to interact with our application.
First, we want to locally test the endpoints operations created in the second part of the tutorial.
To do that, we right click the project name in the project explorer (see next screenshot), then select the Run As option in the contextual menu, and finally click on the Web Application option.
If everything worked fine now we see our local development time server up and running through Eclipse. Also, we obtain the right URL to use for opening the admin console where to manage the objects in the local datastore: http://localhost:8888/_ah/admin
Note that the URL we need to use to access Google Cloud Endpoints locally starts with the base URL http://localhost:8888. Then we use the _ah namespace which is the standard App Engine reserved namespace. This ensures there are not conflicts with any web pages in your app. Then we use the /api/myposts/v1 path. myposts comes from the @Api() attribute at the top of the PostEndpoint.java file. So we have the URL http://localhost:8888/_ah/api/myposts/v1. Then we access each endpoint operation by including the specific path we defined in the @ApiMethod() annotation at the top of each operation.
As an example, if we want to obtain the current list of posts the right URL is: http://localhost:8888/_ah/api/myposts/v1/post. To test the calls we use cURL. cURL provides a library and command-line tool for transferring data using various protocols. Basic use of cURL involves simply typing curl at the command line, followed by the URL of the output to retrieve. Also, almost every programming language provides an implementation of the cURL library. Therefore, you can easily adapt the calls shown here to your favorite piece of code.
The next screenshot shows 4 cURL calls to our endpoint operations. The first one obtains the list of posts with a GET call to the URL http://localhost:8888/_ah/api/myposts/v1/post. The result list is empty since no post was previously created. Then we want to create two posts. To do that, we need two HTTP POST calls passing a jSON encoded post object where we set the title, content, and author attributes of the post. The result is a jSON object of the post after it has been added to the datastore. Notice it has an id within the Key set which we can use to query for it later. The last call repeats the first GET call to obtain the list of posts. Now it obtains the two posts created.
Remember that visiting the local URL http://localhost:8888/_ah/admin you can explore and manage the entities created in the local datastore.
Next step is to deploy the current code to AppEngine in order to access the endpoints globally. We need to visit Google Cloud, log in, and then create a new Project. Next screenshot show you the way. Indicate the project name and the project ID. Note that the project ID must be unique.
Now we need to set up Eclipse to be ready to deploy the code. First of all, log in with your Google account in Eclipse by clicking in the bottom right corner, where you’ll find the Google logo. Once you are logged, right click on our myposts project and select Properties option. Within the Options panel, select Google, and inside it, App Engine (see next screenshot). Here you find a field named Application ID where you have to type the ID of the application you created before in Google Cloud (posts-example, in our case). Accept and then we are ready to deploy.
Select the project in the Project Explorer panel and then click on the Google icon (see next screenshot). Here we need to select Deploy to App Engine… to start the magic.
The Console shows the logs of the deployment (see next screenshot). Here you can find if something went wrong during this process. Once it finishes, we are ready to use our endpoints located at App Engine.
We can also use cURL calls. Note that the URL now has changed to a global URL starting with https://appid.appspot.com, where appid is the application ID we set before. Next screenshot shows the two POST calls to the URL to create a post. This way we can create two Post objects in Google’s App Engine Datastore.
To manage the characteristics of our Google Cloud Application, we can select our application in Google Cloud and then select App Engine. Here we find the App Engine dashboard with a Datastore Viewer where we can manage the entities of our application. We also can control the Instances that contain our code and answer the external calls, see their logs and set their characteristics. There are a lot of options that go beyond this tutorial and you can explore by yourself.
Remember that there is a free quota to test App Engine that includes (among others) several hours of running instances. If you need additional service, you can pay for it.
Thanks for reading! There is a lot more to do with Google Cloud Endpoints and Google’s AppEngine. I recommend you the tutorial by Ido Green from Google, the official documentation, and this book from Dan Sanderson. If you have any comment, I’ll be glad to answer you!