Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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}
Ejemplo n.º 3
0
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}