使用遍历在 Pyramid 中构建 REST API
Building a REST API in Pyramid using traversal
我正在尝试使用 Python 金字塔框架构建一个简单的 REST API。当我嵌套 classes 时,我无法执行可调用视图。 (webkey class inside the auth class)
我这样定义我的资源
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 视图可调用未执行。我在资源脚本中以错误的方式嵌套了 classes 吗?当我在 pshell 中查看字典结构时,它看起来像这样:
print(root.keys())
dict_keys(['auth'])
print(root['auth'].keys())
dict_keys(['webkey'])
print(root['auth']['webkey'].keys())
dict_keys([])
在查找操作中 class auth
每次 returns 一个新的自身实例,这就是为什么 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)
...
同时在查找操作中创建 class 的新实例感觉不对。 Pyramid's traversal tutorial has very nice and simple examples of how to build a traversal application. For complex examples I would recommend to check Kotti source code.
我正在尝试使用 Python 金字塔框架构建一个简单的 REST API。当我嵌套 classes 时,我无法执行可调用视图。 (webkey class inside the auth class)
我这样定义我的资源
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 视图可调用未执行。我在资源脚本中以错误的方式嵌套了 classes 吗?当我在 pshell 中查看字典结构时,它看起来像这样:
print(root.keys())
dict_keys(['auth'])
print(root['auth'].keys())
dict_keys(['webkey'])
print(root['auth']['webkey'].keys())
dict_keys([])
在查找操作中 class auth
每次 returns 一个新的自身实例,这就是为什么 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)
...
同时在查找操作中创建 class 的新实例感觉不对。 Pyramid's traversal tutorial has very nice and simple examples of how to build a traversal application. For complex examples I would recommend to check Kotti source code.