Question

I'm defining an endpoint modeling a user as below. I can query a user by id. But how can I get that id when inserting the user? My client is written in java but I'd like to know how to retrive the id both with python as well as with the generated java client libs.

from google.appengine.ext import endpoints
from google.appengine.ext import ndb 
from protorpc import remote
from endpoints_proto_datastore.ndb import EndpointsModel

class User(EndpointsModel):
    email = ndb.StringProperty(required=True)
    phone_number = ndb.StringProperty()
    reg_id = ndb.StringProperty()
    created = ndb.DateTimeProperty(auto_now_add=True)

@endpoints.api(name='whereareyoudroid', 
               version='v1', description='Where Are You Users')
class UserApi(remote.Service):

    @User.method(name='user.insert', path='user')
    def insert(self, user):
        user.put()
        return user

    @User.method(request_fields=('id',), 
                 path='user/{id}', 
                 http_method='GET', name='user.get')
    def get(self, user):
        if not User.from_datastore:
            raise endpoints.NotFoundException('User not found.')
        return user
Was it helpful?

Solution

First, to answer your question:

By default, the message schema for your requests and response will be the fields on your model: email, phone_number, reg_id, and created. As is done in the simple get sample, you can change this default to include id via

class User(EndpointsModel):
    _message_fields_schema = ('id', 'email', 'phone_number', 
                              'reg_id', 'created')
    email = ndb.StringProperty(required=True)
    ...

or by specifying the response fields you want returned in the insert method:

    @User.method(response_fields=('id', 'email', 'phone_number', 
                                  'reg_id', 'created'),
                 name='user.insert', path='user')
    def insert(self, user):
        user.put()
        return user

Second, you have a typo/mistake in get:

The statement if not User.from_datastore: will check if the User.from_datastore property object on the User class is "truthy", which it is. You want the actual value of this property on the user object: user.from_datastore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top