0

我正在尝试简单地获取端点原型数据存储几天。

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

它有效,但它总是返回未找到。

这是通过 id 获取的 api。

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

这是我的模型代码。

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)

这是我的api代码

@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

我错过了什么吗?谢谢你的建议。

4

1 回答 1

0

我在这里做了你的例子,它可以很好地进行一些更改:

@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

我没有改变关于模型的任何事情,只是改变了 api 方法。

只需执行一个项目插入,获取刚刚插入的项目的 ID,然后使用提供的 ID 执行 ItemGet。

对于 get 我更喜欢这种方式(不使用 /{id},但要求用户执行 GET 查询 - 即ah/api/path?id= __ ,这对我来说似乎更正确)。如果您有任何问题,请在下面提问。

于 2014-02-09T14:35:24.087 回答