androidgoogle-cloud-platformbackendclientgoogle-cloud-endpoints

How to pass data from backend to client in google endpoints?


In my endpoint class in my android app, I return a Profile object which is an entity. I want to pass the data back to the frontend.

What is the best way of doing this and where should the model be stored, in the frontend or in the backend, or perhaps in both?

EDIT:

For example in an async task I do the following:

try {
    return mApi.saveProfile(firstName, lastName, birthday, userId).execute().getFirstName();
} catch (IOException e) {
    return e.getMessage();
}

This calls the endpoints backend method saveProfile which is located in a separate module in the android app. The method does this:

@ApiMethod(name = "saveProfile")
    public Profile saveProfile(@Named("firstName") String firstName,
                               @Named("lastName") String lastName,
                               @Named("birthday") String birthday,
                               @Named("userId") String userId) {
        Profile profile = ofy().load().key(Key.create(Profile.class, userId)).now();
        if (profile == null) {
            profile = new Profile(userId, firstName, lastName, birthday);
        } else {
            profile.updateProfile(firstName, lastName, birthday);
        }
        ofy().save().entity(profile).now();
        return profile;
    }

This method returns a profile entity from the backend endpoints module to where it was called from, in this case, an async task located in the app module (client).

Is it ok for me to use the profile object and add a dependency on the backend module, in order for me to use this object in my client or should I be returning a different object in the backend?

In the method saveProfile there are many parameters, I would like to pass in an object which contains all these fields directly but how do I do this using endpoints as when I do I would need to add a dependency on the backend module so that the class is recognised? So should the profile entity be stored in the backend and should the profile form (saveProfile parameter) be located in the backend or client?


Solution

  • Instead of passing your parameters one by one to the saveProfile method (as non-entity parameters identified via the @Named annotation) you can directly pass a Profile object. Your method would look like:

    @ApiMethod(name = "saveProfile")
    public Profile saveProfile(Profile sentProfile) {
    

    Then, in your endpoint, you just get the properties of sentProfile through corresponding getters.

    The serialization/deserialization will be done automatically by the endpoints "framework".

    Have a look at this tutorial from Romin Irani for more details on how to send an entity from an Android App to the endpoints back-end and vice versa: https://rominirani.com/google-cloud-endpoints-tutorial-part-3-f8a632fa18b1#.ydrtdy17k


    For another way of doing that, you could have a look a this Udacity MOOC: https://www.udacity.com/course/progress#!/c-ud859

    They use extra entities called XXXXForm to convey data from the front-end to the back-end.