使用endpoints\u proto时如何获取请求对象_数据存储.ndb?

2024-03-28 19:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一种方法,如果没有提供共享id,那么就拒绝对gaeapi的请求,正如我在SO questionhere中所讨论的那样。这涉及到使用bossylobster建议的hiddenProperty方法。你知道吗

我在GAE上的演示代码如下所示:

import endpoints

from endpoints_proto_datastore.ndb import EndpointsAliasProperty
from endpoints_proto_datastore.ndb import EndpointsModel
from google.appengine.ext import ndb
from protorpc import remote

DEFAULT_ORDER = 'created, sid'

class TestModel(EndpointsModel):
    _message_fields_schema = ('sid', 'a1', 'a2', 'created')
    a1 = ndb.StringProperty()
    a2 = ndb.StringProperty()
    created = ndb.DateTimeProperty(auto_now_add=True)

    def SidSet(self, value):
        if not isinstance(value, basestring):
            raise TypeError('SID must be a string.')
        self.UpdateFromKey(ndb.Key(TestModel, value))

    @EndpointsAliasProperty(setter=SidSet, required=True)
    def sid(self):
        if self.key is not None:
            return self.key.string_id()

    @EndpointsAliasProperty(setter=EndpointsModel.OrderSet,
                            default=DEFAULT_ORDER)
    def order(self):
        return super(TestModel, self).order

@endpoints.api(name='demogaeapi', version='v1',       
               scopes=[endpoints.EMAIL_SCOPE],
               description='My Demo API')
class MyApi(remote.Service):

    @TestModel.method(http_method='GET',
                      path='testmodel/{sid}',
                      request_fields=('sid',),
                      name='testmodel.get')
    def testModelGet(self, test_model):

        appId,keytype = request.get_unrecognized_field_info('hiddenProperty')
        if appId != 'shared-id':
           raise endpoints.UnauthorizedException('No, you dont!')

        if not test_model.from_datastore:
            raise endpoints.NotFoundException('testModel not found.')
        return test_model

application = endpoints.api_server([MyApi], restricted=False)

我的问题是:如何获取此行中的请求对象?

  • pwd,keytype=请求。获取未识别的字段信息('hiddenProperty')

Tags: fromimportselfidifdefnotdatastore