使用遍历在Pyramid中构建REST API

时间:2018-02-16 12:31:47

标签: python pyramid

我正在尝试使用Python金字塔框架构建一个简单的REST API。我有嵌套类时遇到可调用的视图时遇到麻烦。 (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'

我的观点为

from pyramid.view import view_config

from patientapi.resources.resources import Root, auth, webkey

import logging
log = logging.getLogger(__name__)

@view_config(renderer='json', context=Root)
def home(context, request):
    return {'info': 'DigiDoc API'}

@view_config(renderer='json', context=auth)
def getbasic(context, request):
    return {'AuthModule': context.getDesc()}

@view_config(renderer='json', context=webkey)
def getweb(context, request):
    return {'WEBAuthModule': context.getDesc()}

初始化也非常基础

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 localhost:6543 / auth / webkey获取webkey URI时 执行getBasic视图可调用。我试图找出为什么不执行getWeb视图可调用。我是否在资源脚本中以错误的方式嵌套了类?当我在pshell中查看字典结构时,它看起来像这样:

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

1 个答案:

答案 0 :(得分:1)

在查找操作类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) ... 视图的原因。

v-model

在查找操作上创建类的新实例也感觉不对。 flexbox utils有非常好的简单示例,说明如何构建遍历应用程序。对于复杂的示例,我建议您查看Pyramid's traversal tutorial