def __init__(self, app, manager): self.app = app self.highlight = Highlight() self.manager = manager route("/json", "GET", self.api_index) self.manager.add_api_route( "GET /test/<test variable>", """A test route. Doesn't actually exist, just here for illustration purposes. """) self.manager.add_api_route( "GET /json", """<p> The list of routes in JSON format. You could use this for service or capability discovery, for example. </p> <div class="ui horizontal icon divider"> <i class="circular code icon"></i> </div> {0} """.format( self.highlight.highlight( """ { "routes": { "GET /test/<test variable>": "A test route.", "GET /json": "The list of routes in JSON format. You could use this for service or capability discovery, for example." } } """, "json")))
def __init__(self, app, manager): self.app = app self.highlight = Highlight() self.manager = manager route("/highlight.css", ["GET", "POST"], self.highlight_css) route("/static/<path:path>", ["GET", "POST"], self.static) route("/static/", ["GET", "POST"], self.static_403) route("/static", ["GET", "POST"], self.static_403) route("/.well-known/keybase.txt", ["GET", "POST"], self.static_keybase)
class ApiManager(object, metaclass=Singleton): apis = {} highlighter = None markdowner = None def __init__(self): self.highlighter = Highlight() self.markdowner = markdown2.Markdown(extras=MARKDOWN_EXTRAS) def add_route(self, route: "The API route itself", method: "The HTTP method for this route", text: "Text description for JSON, should be short", markdown: "Name of Markdown file for HTML description"): api_route = {} if route in self.apis: api_route = self.apis[route] if method in api_route: return False # Already exists if not os.path.exists("markdown/{0}.md".format(markdown)): return False description = self.markdowner.convert( open("markdown/{0}.md".format(markdown), "r").read() ) code = "" if (os.path.exists("markdown/json/{0}.json".format(markdown)) and os.path.exists("markdown/xml/{0}.xml".format(markdown))): json_h = self.highlighter.highlight( open("markdown/json/{0}.json".format(markdown), "r").read(), "json" ) xml_h = self.highlighter.highlight( open("markdown/xml/{0}.xml".format(markdown), "r").read(), "xml" ) code = CODE_TEMPLATE.format(json=json_h, xml=xml_h) api_route[method] = { "text": text, "html": TEMPLATE.format(description=description, code=code) } self.apis[route] = api_route return True
class Routes(object): def __init__(self, app, manager): self.app = app self.highlight = Highlight() self.manager = manager route("/highlight.css", ["GET", "POST"], self.highlight_css) route("/static/<path:path>", ["GET", "POST"], self.static) route("/static/", ["GET", "POST"], self.static_403) route("/static", ["GET", "POST"], self.static_403) route("/.well-known/keybase.txt", ["GET", "POST"], self.static_keybase) def highlight_css(self): response.add_header("Content-Type", "text/css") return self.highlight.get_css() def static(self, path): return static_file(path, root="static") def static_403(self): abort(403, "You may not list the static files.") def static_keybase(self): return static_file("keybase.txt", root="static")
class Routes(object): def __init__(self, app, manager): self.app = app self.highlight = Highlight() self.manager = manager route("/json", "GET", self.api_index) self.manager.add_api_route( "GET /test/<test variable>", """A test route. Doesn't actually exist, just here for illustration purposes. """, ) self.manager.add_api_route( "GET /json", """<p> The list of routes in JSON format. You could use this for service or capability discovery, for example. </p> <div class="ui horizontal icon divider"> <i class="circular code icon"></i> </div> {0} """.format( self.highlight.highlight( """ { "routes": { "GET /test/<test variable>": "A test route.", "GET /json": "The list of routes in JSON format. You could use this for service or capability discovery, for example." } } """, "json", ) ), ) def api_index(self): return {"routes": self.manager.api_routes}
def __init__(self): self.highlighter = Highlight() self.markdowner = markdown2.Markdown(extras=MARKDOWN_EXTRAS)