【问题标题】:google appengine endpoint proto datastore simple get id example return not foundgoogle appengine 端点原型数据存储简单获取 id 示例返回未找到
【发布时间】:2013-12-29 04:08:27
【问题描述】:

我在端点原型数据存储区尝试了几天的简单获取。

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

它可以工作,但它总是返回 not found。

这是通过 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

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

【问题讨论】:

    标签: google-app-engine python-2.7 google-cloud-datastore google-cloud-endpoints endpoints-proto-datastore


    【解决方案1】:

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

    @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=__ ,这似乎对我来说更正确)。如果您有任何问题,请在下面提问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多