google appengine endpoint proto datastore simple get id example return not found

517 views Asked by At

I am trying the simple get at endpoint proto datastore for few days.

http://endpoints-proto-datastore.appspot.com/examples/simple_get.html

It works, but it always return not found.

Here is the get by id api.

https://pttbuying.appspot.com/_ah/api/pttbuying/v1/items/4504690549063680

Here is my model code.

class Item(EndpointsModel):
  _message_fields_schema = ('id', 'item_title', 'item_link', 'item_price', 'item_description_strip', 'datetime')

  item_title = ndb.StringProperty(indexed=False)
  item_author_name = ndb.StringProperty(indexed=False)
  item_link = ndb.StringProperty(indexed=True)
  item_description_strip = ndb.StringProperty(indexed=False)
  item_price = ndb.StringProperty(indexed=False)
  datetime = ndb.DateTimeProperty(auto_now_add=True)

Here is my api code

@endpoints.api(name='pttbuying', version='v1',
               allowed_client_ids=[WEB_CLIENT_ID, ANDROID_CLIENT_ID,
                                   IOS_CLIENT_ID, endpoints.API_EXPLORER_CLIENT_ID],
               audiences=[ANDROID_AUDIENCE],
               scopes=[endpoints.EMAIL_SCOPE])

class PttBuyingApi(remote.Service):
    """PttBuying API v1."""
    @Item.method(request_fields=('id',),
                  path='items/{id}', http_method='GET', name='item.MyModelGet')
    def MyModelGet(self, my_item):

      if not my_item.from_datastore:
        raise endpoints.NotFoundException('Item not found.')
      return my_item


    @Item.query_method(query_fields=('limit', 'order', 'pageToken'), path='items', name='item.list')
    def MyModelList(self, query):
      return query

Am i missing something? Thanks for advice.

1

There are 1 answers

0
Ciro Costa On

I did your example here and it works nicely with some changes:

@Item.query_method(query_fields=('limit', 'pageToken',),
                    path='items',
                    http_method='GET',
                    name='items.list')
def ItemsList(self, query):
    return query


@Item.method(request_fields=('id',),
            path='item',
            http_method='GET',
            name='item.get')
def ItemGet(self, item):
    if not item.from_datastore:
        raise endpoints.NotFoundException('item not found')
    return item

@Item.method(path='item',
            http_method='POST',
            name='item.post')
def ItemPost(self, item):
    item.put()
    return item

I didn't change a thing regarding the model, just the api methods.

Just perform an item insertion, get the ID of the item that was just inserted and then perform the ItemGet with the ID provided.

For the get i prefer this way (not using the /{id}, but requiring the user to do a GET Query - i.e, ah/api/path?id=__ , which seems more correct for me). If you have any questions, ask bellow.