我使用端点和端点Proto数据存储构建API并使用App Engine Launcher进行测试。下面的代码是我用来构建两层深层嵌套实体的代码(祖父母/父/子)。
变量_child
返回'无' (在第98行设置)似乎是随机的,这会在139和143行打破ndb.Key
并导致BadArgumentError: Incomplete Key entry must be last
。
import endpoints
from google.appengine.ext import ndb
from protorpc import remote
from endpoints_proto_datastore.ndb import EndpointsAliasProperty
from endpoints_proto_datastore.ndb import EndpointsModel
import logging
project_api = endpoints.api(name='project', version='v1', description='Project API')
""" Grandparent """
class GrandparentModel(EndpointsModel):
_message_fields_schema = ('grandparent',)
def set_grandparent(self, value):
if not isinstance(value, basestring):
raise TypeError('Grandparent name must be a string.')
self.UpdateFromKey(ndb.Key(GrandparentModel, value))
@EndpointsAliasProperty(setter=set_grandparent, required=True)
def grandparent(self):
if self.key is not None:
return self.key.string_id()
""" Parent """
class ParentModel(EndpointsModel):
_message_fields_schema = ('grandparent', 'parent',)
_grandparent = None
_parent = None
def set_key(self):
if self._grandparent is not None and self._parent is not None:
key = ndb.Key(GrandparentModel, self._grandparent, ParentModel, self._parent)
self.UpdateFromKey(key)
def set_parts(self):
if self.key is not None:
grandparent_pair, parent_pair = self.key.pairs()
self._grandparent = grandparent_pair[1]
self._parent = parent_pair[1]
# Grandparent
def set_grandparent(self, value):
if not isinstance(value, basestring):
raise TypeError('Grandparent name must be a string.')
self._grandparent = value
if ndb.Key(GrandparentModel, value).get() is None:
raise endpoints.NotFoundException('Grandparent %s does not exist.' % value)
self.set_key()
self._endpoints_query_info.ancestor = ndb.Key(GrandparentModel, value)
@EndpointsAliasProperty(setter=set_grandparent, required=True)
def grandparent(self):
if self._grandparent is None:
self.set_parts()
return self._grandparent
# Parent
def set_parent(self, value):
if not isinstance(value, basestring):
raise TypeError('Parent must be a string.')
self._parent = value
self.set_key()
@EndpointsAliasProperty(setter=set_parent, required=True)
def parent(self):
if self._parent is None:
self.set_parts()
return self._parent
""" Child """
class ChildModel(EndpointsModel):
_message_fields_schema = ('grandparent', 'parent', 'child',)
_grandparent = None
_parent = None
_child = None
def set_key(self):
if self._grandparent is not None and self._parent is not None and self._child is not None:
key = ndb.Key(GrandparentModel, self._grandparent, ParentModel, self._parent, ChildModel, self._child)
self.UpdateFromKey(key)
def set_parts(self):
if self.key is not None:
grandparent_pair, parent_pair, child_pair = self.key.pairs()
self._grandparent = grandparent_pair[1]
self._parent = parent_pair[1]
self._child = child_pair[1]
# Grandparent
def set_grandparent(self, value):
if not isinstance(value, basestring):
raise TypeError('Grandparent must be a string.')
self._grandparent = value
self.set_key()
@EndpointsAliasProperty(setter=set_grandparent, required=True)
def grandparent(self):
if self._grandparent is None:
self.set_parts()
return self._grandparent
# Parent
def set_parent(self, value):
if not isinstance(value, basestring):
raise TypeError('Parent name must be a string.')
self._parent = value
logging.warning('Check _grandparent value: %s' % self._grandparent)
logging.warning('Check _parent value: %s' % self._parent)
if ndb.Key(GrandparentModel, self._grandparent, ParentModel, value).get() is None:
raise endpoints.NotFoundException('Key %s does not exist.' % value)
self.set_key()
self._endpoints_query_info.ancestor = ndb.Key(GrandparentModel, self._grandparent, ParentModel, value)
@EndpointsAliasProperty(setter=set_parent, required=True)
def parent(self):
if self._parent is None:
self.set_parts()
return self._parent
# Child
def set_child(self, value):
if not isinstance(value, basestring):
raise TypeError('Child must be a string.')
self._child = value
self.set_key()
@EndpointsAliasProperty(setter=set_child, required=True)
def child(self):
if self._child is None:
self.set_parts()
return self._child
@project_api.api_class(resource_name='grandparent')
class Grandparent(remote.Service):
@GrandparentModel.method(name='insert', path='grandparent')
def grandparent_insert(self, grandparent):
grandparent.put()
return grandparent
@GrandparentModel.query_method(name='list', path='grandparent')
def grandparent_list(self, query):
return query
@project_api.api_class(resource_name='parent', path='grandparent')
class Parent(remote.Service):
@ParentModel.method(name='insert', path='{grandparent}/parent')
def parent_insert(self, parent):
parent.put()
return parent
@ParentModel.query_method(name='list', path='{grandparent}/parent', query_fields=('grandparent',))
def parent_list(self, query):
return query
@project_api.api_class(resource_name='child', path='grandparent')
class Child(remote.Service):
@ChildModel.method(name='insert', path='{grandparent}/parent/{parent}/child')
def child_insert(self, child):
child.put()
return child
@ChildModel.query_method(name='list', path='{grandparent}/parent/{parent}/child',
query_fields=('grandparent', 'parent',))
def child_list(self, query):
return query
application = endpoints.api_server([project_api], restricted=False)
这里是破纪录时的日志:
INFO 2015-08-16 10:26:50,503 module.py:809] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 8577 INFO 2015-08-16 09:26:52,099 main.py:136] Check _grandparent value: g INFO 2015-08-16 09:26:52,099 main.py:137] Check _parent value: p INFO 2015-08-16 10:26:52,108 module.py:809] default: "POST /_ah/spi/Child.child_list HTTP/1.1" 200 2 INFO 2015-08-16 10:26:52,109 module.py:809] default: "GET /_ah/api/project/v1/grandparent/g/parent/p/child HTTP/1.1" 200 2 INFO 2015-08-16 10:26:59,165 module.py:809] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 8577 INFO 2015-08-16 09:26:59,168 main.py:136] Check _grandparent value: None INFO 2015-08-16 09:26:59,168 main.py:137] Check _parent value: p ERROR 2015-08-16 09:26:59,168 service.py:191] Encountered unexpected error from ProtoRPC method implementation: BadArgumentError (Incomplete Key entry must be last) Traceback (most recent call last): File "C:\Development\Google\google_appengine\lib\protorpc-1.0\protorpc\wsgi\service.py", line 181, in protorpc_service_app response = method(instance, request) File "C:\Development\Google\google_appengine\lib\endpoints-1.0\endpoints\api_config.py", line 1332, in invoke_remote return remote_method(service_instance, request) File "C:\Development\Google\google_appengine\lib\protorpc-1.0\protorpc\remote.py", line 414, in invoke_remote_method response = method(service_instance, request) File "C:\Development\_Projects\API\project\endpoints_proto_datastore\ndb\model.py", line 1574, in QueryFromRequestMethod request_entity = cls.FromMessage(request) File "C:\Development\_Projects\API\project\endpoints_proto_datastore\ndb\model.py", line 1245, in FromMessage setattr(entity, name, value) File "C:\Development\_Projects\API\project\main.py", line 139, in set_parent if ndb.Key(GrandparentModel, self._grandparent, ParentModel, value).get() is None: File "C:\Development\Google\google_appengine\google\appengine\ext\ndb\key.py", line 220, in __new__ self.__namespace) = self._parse_from_args(**kwargs) File "C:\Development\Google\google_appengine\google\appengine\ext\ndb\key.py", line 245, in _parse_from_args 'Incomplete Key entry must be last') BadArgumentError: Incomplete Key entry must be last INFO 2015-08-16 10:26:59,174 module.py:809] default: "POST /_ah/spi/Child.child_list HTTP/1.1" 500 512 INFO 2015-08-16 10:26:59,174 module.py:809] default: "GET /_ah/api/project/v1/grandparent/g/parent/p/child HTTP/1.1" 503 196
到目前为止,我一直无法弄明白,所以任何帮助都会受到赞赏。
谢谢!
答案 0 :(得分:1)
更新:您在sort_keys
中假设从parent
已解析grandparent
的路径解析 if self._grandparent is None:
raise Exception('set_parent: self._grandparent is None')
时:
grandparent
要避免此问题,请不要依赖parent
和endpoints
设置的顺序。
解释:每次运行流程时,字段可能会以不同的顺序出现。当CombinedContainer
库检测到路径中有字段时,CombinedContainer
消息类为created on the fly:此类的字段按任意顺序设置。 (用于创建Message.all_fields()
的字段来自输出Message
。但endpoints-proto-datastore
使用dictionary按名称存储字段,然后使用all_fields()
uses dict.values()
获取值在Python中,字典顺序是非确定性的,因此这些顺序可能以任何可能的顺序返回。)
然后在CombinedContainer
中,CombinedContainer
中的字段按字段编号为sorted。此排序顺序在用户定义的类中很重要,但在用户无法控制IdSet
的创建时会被“忽略”。
请注意,在keys with ancestors example SetKey
方法调用SetKey
,但if self._parent is not None and self._id is not None:
...
确保已设置父级和ID
'grandparent/{grandparent}/parent/{parent}/child'
简答:使用
'{grandparent}/parent/{parent}/child'
作为路径模板。
长答案:发送的请求与PATH模板不匹配。
您的URI模板是
/_ah/api/project/v1/grandparent/g/parent/p/child
但你的路径是
grandparent/g/parent/p/child
,对应于
的相对路径{grandparent}
根据匹配的工作原理(我不确定),您将完全不匹配grandparent/g
将匹配{{1}}。
从您的堆栈跟踪中,看起来根本就没有匹配。