相当于 Falcon 的 Flask "url_for"
Flask "url_for" equivalent for Falcon
我是 Falcon 的新手,我想知道该框架是否有类似 Flask 的 "url_for" 解决方案。我已经搜索了文档,但似乎找不到与 google/stack 搜索相关的任何内容。
为了向没有使用过 Flask 的 Falcon 用户说明,我想动态获取已定义资源的 URL。我特别试图通过在我的有效负载中为我的资源包含一个 link 来实现资源扩展,这样前端就不必构建任何 URLs.
代码:
class PostResource(object):
def on_get(self, req, resp, post_id):
"""Fetch single post resource."""
resp.status = falcon.HTTP_200
post_dto = post_to_dto(get_post(post_id))
# TODO: find url_to alternative for falcon: specify post resource location
post_dto.href = ''
resp.body = to_json(PostDtoSerializer, post_dto)
class PostCollectionResource(object):
def on_get(self, req, resp):
"""
Fetch grid view for all post resources.
Note: This endpoint support pagination, pagination arguments must be provided via query args.
"""
resp.status = falcon.HTTP_200
# TODO: add hrefs for each post for end ui
post_collection_dto = PostCollectionDto(
posts=[post_to_dto(post, comments=False) for post in get_posts(
start=req.params.get('start', None), count=req.params.get('count', None)
)])
resp.body = to_json(PostCollectionDtoSerializer, post_collection_dto)
def on_post(self, req, resp):
"""Create a new post resource."""
resp.status = falcon.HTTP_201
payload = req.stream.read()
user = req.context.get('user')
create_post(user._id, from_json(PostFormDtoSerializer, payload))
# TODO: find url_to alternative for falcon: redirect to on_get
resp.set_header('Location', '')
Post 集合示例:
[
{
"href": ".../post/000000/",
"links": [
"rel": "like",
"href": ".../post/000000/like"
],
"title": "Foobar",
...
}
]
我希望能够生成 link 到 PostResource
。
为了关闭此线程,我现在使用此处详述的方法 https://github.com/neetjn/py-blog/issues/16。
Falcon 不 维护者确认支持这一点,我的工作是创建一个带有静态路由和子方法的基础资源来构造一个 link使用来自请求的 req
参数的信息到给定资源。
示例:
class BaseResource(object):
route = ''
@classmethod
def url_to(cls, host, **kwargs) -> str:
return f'{host}{cls.route.format(**kwargs)}'
...
class PostResource(BaseResource):
route = '/v1/post/{post_id}'
def on_get(self, req, res):
pass
class PostCollectionResource(BaseResource):
route = '/v1/posts/'
def on_get(self, req, res):
posts = get_posts()
for post in posts:
post.href = PostResource.url_to(req.netloc, post_id=post.id)
我是 Falcon 的新手,我想知道该框架是否有类似 Flask 的 "url_for" 解决方案。我已经搜索了文档,但似乎找不到与 google/stack 搜索相关的任何内容。
为了向没有使用过 Flask 的 Falcon 用户说明,我想动态获取已定义资源的 URL。我特别试图通过在我的有效负载中为我的资源包含一个 link 来实现资源扩展,这样前端就不必构建任何 URLs.
代码:
class PostResource(object):
def on_get(self, req, resp, post_id):
"""Fetch single post resource."""
resp.status = falcon.HTTP_200
post_dto = post_to_dto(get_post(post_id))
# TODO: find url_to alternative for falcon: specify post resource location
post_dto.href = ''
resp.body = to_json(PostDtoSerializer, post_dto)
class PostCollectionResource(object):
def on_get(self, req, resp):
"""
Fetch grid view for all post resources.
Note: This endpoint support pagination, pagination arguments must be provided via query args.
"""
resp.status = falcon.HTTP_200
# TODO: add hrefs for each post for end ui
post_collection_dto = PostCollectionDto(
posts=[post_to_dto(post, comments=False) for post in get_posts(
start=req.params.get('start', None), count=req.params.get('count', None)
)])
resp.body = to_json(PostCollectionDtoSerializer, post_collection_dto)
def on_post(self, req, resp):
"""Create a new post resource."""
resp.status = falcon.HTTP_201
payload = req.stream.read()
user = req.context.get('user')
create_post(user._id, from_json(PostFormDtoSerializer, payload))
# TODO: find url_to alternative for falcon: redirect to on_get
resp.set_header('Location', '')
Post 集合示例:
[
{
"href": ".../post/000000/",
"links": [
"rel": "like",
"href": ".../post/000000/like"
],
"title": "Foobar",
...
}
]
我希望能够生成 link 到 PostResource
。
为了关闭此线程,我现在使用此处详述的方法 https://github.com/neetjn/py-blog/issues/16。
Falcon 不 维护者确认支持这一点,我的工作是创建一个带有静态路由和子方法的基础资源来构造一个 link使用来自请求的 req
参数的信息到给定资源。
示例:
class BaseResource(object):
route = ''
@classmethod
def url_to(cls, host, **kwargs) -> str:
return f'{host}{cls.route.format(**kwargs)}'
...
class PostResource(BaseResource):
route = '/v1/post/{post_id}'
def on_get(self, req, res):
pass
class PostCollectionResource(BaseResource):
route = '/v1/posts/'
def on_get(self, req, res):
posts = get_posts()
for post in posts:
post.href = PostResource.url_to(req.netloc, post_id=post.id)