(This is a copy of a the same question posted on github about the awesome endpoints-proto-datastore library)
I am trying to implement my API so that the client can pass a '?fields=' url parameter in the api request and then I can instruct the query to build the response and return only the requested collection_fileds.
However, I don't know how to pass url parameters to the @query_method decorator; here's my code:
@Contact.query_method(query_fields=('limit', 'order', 'pageToken'),
                      collection_fields=('name', 'birthday'),
                      path='contacts',
                      name='contacts.list')
def contacts_list(self, query):
    return query
How can I pass the fields param from the request to the collection_fields= named param in the decorator?
                        
The answer to this is somewhat similar to Simple Access API (Developer Key) with Google Cloud Endpoint (Python)
In
users_id_token.py, therequestobject is a ProtoRPC object that parsed an incoming request. However, even if the actual user did not specify on of the keys'bearer_token'or'access_token'as part of the ProtoRPC message definition, if they were passed in the request as query parameters, they will be stored on the ProtoRPC object that is created.To access them, the not-so-well-known method is used:
Why is this important? To access
fieldson an object, let's assume the attributefieldswas passed in through the request. Then if you have a request parsed intomy_message, you can access thefieldsviaIn
ndb/model.py, thequery_methodmethod has a locally scoped function defined there which is calledQueryFromRequestMethod. In it, a ProtoRPC object is created directly:It is there where you'd want to use
I would suggest either
endpoints_proto_datastore.ndb.EndpointsModeland overriding thequery_methodmethod to do your special bidding.