使用travers在金字塔中构建restapi

2024-10-03 15:27:04 发布

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

我尝试使用Python金字塔框架构建一个简单的restapi。当我有嵌套类时,我很难执行一个可调用的视图。(auth类内的webkey类)

我这样定义我的资源

from __future__ import absolute_import
from pyramid.traversal import find_root

import logging
log = logging.getLogger(__name__)


class Resource(dict):
    def __init__(self, ref, parent):
        self.__name__ = ref
        self.__parent__ = parent

    def __repr__(self):
        # use standard object representation (not dict's)
        return object.__repr__(self)

    def add_child(self, ref, klass):
        resource = klass(ref=ref, parent=self)
        self[ref] = resource

class Root(Resource):
    def __init__(self, request):
        Resource.__init__(self, ref='', parent=None)
        self.request = request
        self.add_child('auth', auth)
        self['auth'].add_child('webkey', webkey)

class auth(Resource):
    def __init__(self, ref, parent):
        self.__name__ = ref
        self.__parent__ = parent            

    def collection(self):
        root = find_root(self)
        request = root.request
        return request.db[self.collection_name]

    def retrieve(self):
        return [elem for elem in self.collection.find()]

    def create(self, document):
        object_id = self.collection.insert(document)

        return self.resource_name(ref=str(object_id), parent=self)

    def __getitem__(self, ref):
        return auth(ref, self)

    def getDesc(self):
        return 'Base authentication class'

class webkey(Resource):

    def __init__(self, ref, parent):
        self.__name__ = ref
        self.__parent__ = parent

    def collection(self):
        root = find_root(self)
        request = root.request
        return request.db[self.collection_name]

    def retrieve(self):
        return [elem for elem in self.collection.find()]

    def create(self, document):
        object_id = self.collection.insert(document)

        return self.resource_name(ref=str(object_id), parent=self)

    def __getitem__(self, ref):
        return webkey(ref, self)

    def getDesc(self):
        return 'Web key authentication class'

我的观点

^{pr2}$

初始化也是非常基本的

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings, root_factory=Root)

    config.scan('.views')
    return config.make_wsgi_app()

当我使用curl获取webkey URI时本地主机:6543/auth/webkey 执行getBasic视图可调用。我试图弄清楚为什么getWeb视图callable没有执行。我是否在资源脚本中以错误的方式嵌套类?当我在pshell中查看字典结构时,它如下所示:

    print(root.keys())
    dict_keys(['auth'])
    print(root['auth'].keys())
    dict_keys(['webkey'])
    print(root['auth']['webkey'].keys())
    dict_keys([])

Tags: nameselfauthrefreturnobjectrequestdef
1条回答
网友
1楼 · 发布于 2024-10-03 15:27:04

在查找操作类auth上,每次都返回其自身的一个新实例,这就是为什么从未调用getweb视图的原因。在

class auth(Resource):
    ...
    def __getitem__(self, ref):
        # it always returns a new instance of auth class, 
        # and that is why context will always by `auth`
        return auth(ref, self)
    ...

另外,在查找操作中创建类的新实例也不合适。Pyramid's traversal tutorial提供了非常好且简单的示例,说明了如何构建遍历应用程序。对于复杂的例子,我建议检查Kotti source code。在

相关问题 更多 >