如何将api请求中的collection_fields传递给@query_method装饰器?

Cat*_*ato 3 google-app-engine google-cloud-endpoints endpoints-proto-datastore

(这是在github上发布的关于令人敬畏的端点 - 原型数据存储库的相同问题的副本)

我正在尝试实现我的API,以便客户端可以在api请求中传递'?fields ='url参数,然后我可以指示查询构建响应并仅返回请求的collection_fileds.

但是,我不知道如何将url参数传递给@query_method装饰器; 这是我的代码:

@Contact.query_method(query_fields=('limit', 'order', 'pageToken'),
                      collection_fields=('name', 'birthday'),
                      path='contacts',
                      name='contacts.list')
def contacts_list(self, query):
    return query
Run Code Online (Sandbox Code Playgroud)

如何fields将请求中的参数传递给装饰器中的collection_fields = named param?

bos*_*ter 6

对此的回答与使用Google Cloud Endpoint(Python)的Simple Access API(开发人员密钥)有些相似

users_id_token.py,request对象是一个解析传入请求的ProtoRPC对象.但是,即使实际用户没有指定键'bearer_token''access_token'作为ProtoRPC消息定义的一部分,如果它们作为查询参数在请求中传递,它们将存储在创建的ProtoRPC对象上.

要访问它们,使用了不太知名的方法:

request.get_unrecognized_field_info(key)
Run Code Online (Sandbox Code Playgroud)

为什么这很重要?要访问fields对象,我们假设该属性fields是通过请求传递的.然后,如果您已解析请求my_message,则可以访问fieldsvia

my_message.get_unrecognized_field_info('fields')
Run Code Online (Sandbox Code Playgroud)

ndb/model.py,该query_method方法具有在那里定义的本地范围的函数,该函数被调用QueryFromRequestMethod.在其中,直接创建ProtoRPC对象:

request_entity = cls.FromMessage(request)
Run Code Online (Sandbox Code Playgroud)

它是你想要使用的地方

request_entity.get_unrecognized_field_info('fields')
Run Code Online (Sandbox Code Playgroud)

我也建议

  1. 使用仅适用于您的用例的修补版本的库
  2. 提交一个修复程序库,允许以轻量级方式在方法中进行此自定义
  3. 对您的特殊出价进行子类化endpoints_proto_datastore.ndb.EndpointsModel和覆盖query_method方法.