Question

tl;dr: is it possible, with endpoints-proto-datastore, to receive a list with objects from a POST and insert it in the db?

Following the samples, when building my API i didn't got how could i let the users POST a list of objects so that i could be more efficient about putting a bunch of data in the db using ndb.put_multi, for example.

From this comment here at endpoints_proto_datastore.ndb.model i imagine that it is not possible with how it is designed. Am i right or i am missing something?

Extending the sample provided by endpoints achieved the desired with:

class Greeting(messages.Message):
    message = messages.StringField(1)

class GreetingCollection(messages.Message):
    items = messages.MessageField(Greeting, 1, repeated=True)

# then inside the endpoints.api class

    @endpoints.method(GreetingCollection, GreetingCollection,
                      path='hellogretting', http_method='POST',
                      name='greetings.postGreeting')
    def greetings_post(self, request):
        result = [item for item in request.items]
        return GreetingCollection(items=result)

-- edit --

Was it helpful?

Solution

See the docs about POSTing into the datastore, your only issue is that your models aren't EndpointsModels. Instead define a datastore model for both your Greeting and GreetingCollection:

from endpoints_proto_datastore.ndb import EndpointsModel

class Greeting(EndpointsModel):
    message = ndb.StringProperty()

class GreetingCollection(EndpointsModel):
    items = ndb.StructuredProperty(Greeting, repeated=True)

Once you've done this, you can use

class MyApi(remote.Service):
  # ...

  @GreetingCollection.method(path='hellogretting', http_method='POST',                               
                             name='greetings.postGreeting')
  def greetings_post(self, my_collection):
      ndb.put_multi(my_collection.items)
      return my_collection
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top