def get(self, *args, **kwargs):
     api_list = list()
     for rule in self.application.wildcard_router.rules:
         if not issubclass(rule.target, ApiHandler):
             continue
         data = {
             'class_name':
             rule.target.tonadoapi_get_class_name(),
             'name':
             rule.name or rule.target.get_handler_name(),
             'path':
             rule.matcher.regex.pattern[:-1] if isinstance(
                 rule.matcher, PathMatches) else str(rule.matcher),
             'support_methods':
             rule.target.support_methods(),
             'field_info':
             rule.target.tonadoapi_field_info(),
             'return_sample':
             rule.target.get_return_sample()
         }
         api_list.append(data)
     ret_sample = {
         CodeData.get_code_tag(): '错误码',
         CodeData.get_message_tag(): '错误描述',
         CodeData.get_data_tag(): '数据'
     }
     html = get_resource_template_html(
         'doc.html',
         namespace=self.get_template_namespace(),
         err_codes=[getattr(ErrCode, tag) for tag in ErrCode.get_tags()],
         ret_sample_data=ret_sample,
         api_list=api_list,
     )
     self.set_header("Content-Type", "text/html; charset=UTF-8")
     self.finish(html)
 def get_traceback_text(self):
     """
     Return plain text version of debug 500 HTTP error page.
     """
     from tornadoapi.template import get_resource_template_html
     return get_resource_template_html('technical_500.txt',
                                       **self.get_traceback_data())
    def write_api(self, obj, no_fail=False, fmt=None, **kwargs):
        if isinstance(obj, (CustomError, CodeData)):
            obj = obj.get_res_dict()
        elif not isinstance(obj, dict) or CodeData.get_code_tag() not in obj:
            obj = ErrCode.SUCCESS.get_res_dict(
                **{CodeData.get_data_tag(): obj})
        if not fmt:
            fmt = self.get_format()
        support_format = (API_FORMAT_JSON, API_FORMAT_JSONP,
                          API_FORMAT_PREVIEW)
        if not self.debug:
            support_format = (API_FORMAT_JSON, API_FORMAT_JSONP)

        if fmt not in support_format and no_fail:
            fmt = API_FORMAT_JSON
        if fmt == API_FORMAT_PREVIEW and self.debug:
            html = get_resource_template_html(
                'apiview.html',
                namespace=self.get_template_namespace(),
                res_data=obj,
                field_info=self.tonadoapi_field_info(),
                handler_name=self.get_handler_name(),
                url=self.request.uri,
                method=self.request.method,
                return_sample=self.get_return_sample(),
                description=self.get_handler_description(),
                remark=self.get_handler_remark(),
                support_methods=self.support_methods())
            self.set_header("Content-Type", "text/html; charset=UTF-8")
            self.finish(html)
        elif fmt == API_FORMAT_JSON:
            self.set_header("Content-Type", "application/json; charset=UTF-8")
            self.finish(json_dumps(obj))
        elif fmt == API_FORMAT_JSONP:
            self.set_header("Content-Type", "application/javascript")
            callback = self.get_argument('callback', 'callback')
            self.finish('%s(%s);' % (callback, json_dumps(obj)))
        else:
            self.log.error("format error %s" % fmt)
            raise CustomError(ErrCode.ERR_COMMON_BAD_PARAM)