diff --git a/firefly/app.py b/firefly/app.py index a284ac0..c9920be 100644 --- a/firefly/app.py +++ b/firefly/app.py @@ -1,6 +1,7 @@ import cgi from webob import Request, Response from webob.exc import HTTPNotFound +from jinja2 import PackageLoader, Environment import json import logging from .validator import validate_args, ValidationError @@ -22,6 +23,9 @@ ctx = threading.local() ctx.request = None +env = Environment(loader=PackageLoader('firefly', 'templates')) +template = env.get_template('index.html') + class Firefly(object): def __init__(self, auth_token=None): self.mapping = {} @@ -47,6 +51,20 @@ def generate_index(self): } return help_dict + def render_docs(self, **kwargs): + functions = [ + {'name': name, 'path': spec['path'], 'doc': spec['doc'], 'parameters': spec['parameters']} + for name, spec in self.generate_function_list().items() + ] + html = template.render({ + 'host_url': kwargs['host_url'], + 'functions': functions + }) + response = Response(content_type='text/html') + response.status = 200 + response.text = html + return response + def __call__(self, environ, start_response): request = Request(environ) response = self.process_request(request) @@ -73,7 +91,9 @@ def process_request(self, request): ctx.request = request path = request.path_info - if path in self.mapping: + if path == "/docs": + return self.render_docs(host_url=request.environ['HTTP_HOST']) + elif path in self.mapping: func = self.mapping[path] response = func(request) else: diff --git a/firefly/templates/index.html b/firefly/templates/index.html new file mode 100644 index 0000000..fe4f703 --- /dev/null +++ b/firefly/templates/index.html @@ -0,0 +1,78 @@ + + + + + + + {% if name %}{{ name }} | {% else %}{% endif %}firefly | API Browser + + + + +
+
+

{% if name %}{{ name }} | {% else %}{% endif %}firefly | API Browser

+ {% if version %} +

Version: {{ version }}

+ {% endif %} +
+
+
+ {% for function in functions %} +
+
+
+

+
{{ function.path }}
+ {{ function.name }} +

+
+
+
+

+ {% if function.doc %} + {{ function.doc }} + {% else %} + No docstring provided + {% endif %} +

+

Parameters

+
    + {% for param in function.parameters %} +
  • {{ param.name }}
  • + {% endfor %} +
+
+
+
+
+ {% endfor %} +
+
+
+

The API can be used by using firefly-client

+

Install it using:

+
+ + $ pip install firefly + +
+

Usage:

+
+ + >>> import firefly
+ >>> client = firefly.Client('{{ host_url }}')
+ >>> client.function_name(<parameters>)
+ <function_response> +
+
+
+
+ + + + diff --git a/requirements.txt b/requirements.txt index 15d89b2..87205b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ WebOb==1.7.2 requests>=2.18.1 PyYAML==3.12 funcsigs==1.0.2 ; python_version < '3' +Jinja2==2.10 diff --git a/setup.py b/setup.py index 1b81eb1..87143a4 100644 --- a/setup.py +++ b/setup.py @@ -77,7 +77,8 @@ def get_version(): 'gunicorn==19.7.1', 'WebOb==1.7.2', 'requests==2.18.1', - 'PyYAML==3.12' + 'PyYAML==3.12', + 'Jinja2==2.10' ] if PY2: