Google Cloud Endpoints Tips #5 : Using Cursor and Limit parameters
December 2017: Please note that this post has not been updated for a while and there could be differences in terms of commands, screenshots and problems in running the code.
One of the design decisions that you need to take with your API is how much of data to return. This is especially important for the list*() methods in your Endpoints implementation.
For e.g. if your API is dealing with Blog comments, there is a possibility of a few blog posts being popular and having hundreds of comments. The correct approach for the API would be to provide the client application using the API, with mechanisms to control the following two aspects of any list*() call:
- Specify the number of results per API call. This is similar to results per page.
- Allow subsequent calls to specify the particular page or location from where to return the next N results of the API.
Google Cloud Endpoints addresses this via the cursor and limit parameters. The limit parameter tells how many results to retrieve and the cursor parameter tells the API, from which point should it retrieve the results.
To better understand this, we will need to work with a simple example. It will also demonstrate to us that Google API Explorer via which we can test our API, makes it easier for us to understand these parameters.
Let us start with an empty App Engine project and create an Entity say Task as shown below:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Task {@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
Long id;@Persistent
String description;@Persistent
String status;// Getter / Setter methods}
Now, generate the Cloud Endpoints code for this Task class via the Eclipse plugin (or maven / command line — depending on your preference). The Endpoint class generated with the TaskEndpoint.java class. Simply focus on the listTask signature that is shown below:
public CollectionResponse<Task> listTask(
@Nullable @Named(“cursor”) String cursorString,
@Nullable @Named(“limit”) Integer limit)
You will notice the 2 parameters that we have discussed before i.e. cursor and limit.
Go ahead and start the application. Go to the API Explorer locally via the /_ah/api/explorer url and insert at least 4–5 Task Records.
Now, go to the API Explorer and listTask method. The form is shown below:
If you simply click Execute here without entering any values in the cursor and limit fields, then you will get the result shown below. In my case , I have create 4 Task Records and hence all the 4 records are returned.
Now, let’s say that we want to just retrieve 2 records per request and not more. Simply provide the value in the limit parameter as shown below and click on Execute.
This will return you just the first two records. It starts from the beginning since we have not provided any value in the cursor i.e. from where we should start.
Notice in the above screen i.e. the API response, that a value nextPageToken is passed. This is the value that you need to use in your subsequent calls to the listTask method, to get the next set of records as per the limit and from which point. So, if we provide that value in the cursor as shown below and click Execute.
We get the next 2 records as shown below:
Remember that the cursor and limit parameters are optional. So if you do not provide these values, the Endpoint generated code takes care of it in the listTask implementation.
Hope this makes things clear on how you can ask only for partial results per API Request.
List of Cloud Endpoints Tips