Flask/Flask Restful获取端点/资源方法docstring

2024-04-26 03:07:35 发布

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

在烧瓶restful中,我创建了一个端点,它列出了其他端点,与其他许多端点一样也做了。我可以获得资源类的docstring,但我无法确定如何在资源中获取get/post/put/delete方法的docstring。例如,我想记录GET的可用查询参数和PUT/POST的主体。这就是我所拥有的:

class EndpointListResource(Resource):
    """
    List of endpoint descriptions
    """

    def get(self):
        """
        Generate Endpoints documentation
        :return: endpoint list
        """
        endpoints = []
        for rule in web_app.url_map.iter_rules():
            if rule.endpoint in ["static", "api.endpoints"]:
                continue

            options = {}
            for arg in rule.arguments:
                options[arg] = "[{0}]".format(arg)
            url = url_for(rule.endpoint, **options)

            methods = filter(lambda method: method not in ["OPTIONS", "HEAD"], rule.methods)
            method_descriptions = map(lambda method: {"method": method, "doc": "?"}, methods)
            endpoints.append({
                "endpoint": unquote(url),
                "doc": web_app.view_functions[rule.endpoint].__doc__,
                "methods": list(method_descriptions)
            })
        return endpoints

Tags: inurlfordocarg资源端点rule