简体   繁体   中英

GAE: Can't use imported class with Endpoints API method

I have a class containing a method that writes to my datastore. I am able to use this to write to my datastore from my web, but I can't figure out how to use it for my API POST as well.

DatastoreWrite.py:

from endpoints_proto_datastore.ndb import EndpointsModel
from google.appengine.ext import ndb

def person_timestamp_key(person_timestamp):
return ndb.Key('Person Timestamp', person_timestamp)

class Person(EndpointsModel):
    name = ndb.StringProperty(indexed=False)
    timestamp = ndb.DateTimeProperty(auto_now_add=True)

def do_insert(self, name):
    person_timestamp = 'person_timestamp'
    # Sets entity
    person = Person(parent=person_timestamp_key(person_timestamp))

    person.name = name

    person.put()

webapp.py - This successfully adds an entry into my datastore when submitted from the webapp:

def post(self):

    name = self.request.get('name')

    person = DatastoreWrite.Person()

    if name and not name.isspace():
        person.do_insert(name)

        self.redirect('/')

api.py - I've tried a hundred different things here. This is what I have currently. Results in "BadValueError: Expected string, got Person()":

class Api(remote.Service):

  @Person.method(path='api', http_method='POST', name='person.insert')
  def PersonInsert(self, person):

      person = DatastoreWrite.Person()

      person.do_insert(person)

      return person

UPDATE: Another attempt - fails with error "BadValueError: Expected string, got Person(name=u'Atlas')":

@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
  def PersonInsert(self, name):

      person = DatastoreWrite.Person()

      person.do_insert(name)

      return person

do_insert function has a signature def do_insert(self, name) which requires name as parameter that is a string and you are sending person as a parameter person.do_insert(person) .

Solution :

@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
def PersonInsert(self, person):

  person.do_insert(person.name)

  return person

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