简体   繁体   中英

Python API Query on Google App Engine Datastore

I am having trouble with the getting the correct response for a Python Query on Google App Engine.

Here is the LOG from GAE, it successfully prints out the object that matches the query parameters, but now I am trying to send that objects data as JSON back. I am creating a rudimentary user auth system that queries for a USER object using email and password, and if it exists than it returns all the data back.

How can I break down this query to return the data that was found?

E 00:21:06.352 Encountered unexpected error from ProtoRPC method implementation: AttributeError ('list' object has no attribute 'email')
  Traceback (most recent call last):
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
      response = method(instance, request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
      return remote_method(service_instance, request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
      response = method(service_instance, request)
    File "/base/data/home/apps/s~caramel-theory-800/1.381271940209425490/photoswap_api.py", line 34, in user_auth
      return UserCreateResponseMessage(email=query.email, password=query.password, username=query.username,
  AttributeError: 'list' object has no attribute 'email'
E 00:21:06.360 [User(key=Key('User', 5086441721823232), email=u'pop', password=u'top', username=u'yop')]

Here is the ENDPOINT API

I believe the issue is within the last line of code where it trys to return the results of the query (UserAuthResponseMessage)..

@endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage,
                  path='user', http_method='GET',
                  name='user.auth')
def user_auth(self, request):
    # create some type of query to check for email address, and than check to see if passwords match
    query = User.query(User.email == request.email, User.password == request.password).fetch()
    print query

    # return the info from the server
    return UserCreateResponseMessage(email=query[0].email, password=query[0].password, username=query[0].username,
                                     id=query[0].key.id())


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False)

You problem is your are performing a query and expecting to reference the email property of the query which of course it doesn't have one.

If you expect only one result from the query then you should be using get rather than index addressing.

The other problem is the user_auth method included doesn't match your stack trace. So there is a problem here.

With the help of Tim Hoffman this is the solution that I was able to come up with.

I was returning the wrong ResponseMessage, and I was better off using .get instead of .fetch as there should only be 1 result returned.

@endpoints.api(name='photoswap', version='v1')
class PhotoswapAPI(remote.Service):
    @endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage,
                      path='user', http_method='POST',
                      name='user.create')
    def user_create(self, request):
        entity = User(email=request.email, password=request.password, username=request.username)
        entity.put()
        return UserCreateResponseMessage(email=entity.email, password=entity.password, username=entity.username,
                                         id=entity.key.id())

    @endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage,
                      path='user', http_method='GET',
                      name='user.auth')
    def user_auth(self, request):
        # create some type of query to check for email address, and than check to see if passwords match
        query = User.query(User.email == request.email, User.password == request.password).get()
        print query

        # return the info from the server
        return UserAuthResponseMessage(email=query.email, password=query.password, username=query.username, id=query.key.id())


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM