Handling HTTP Requests


Basic HTTP API example can be browsed and downloaded at https://github.com/ajenti/demo-plugins/tree/master/demo_4_http

Plugins can provide their own HTTP endpoints by extending the aj.api.http.HttpPlugin abstract class.


import time
from jadi import component

from aj.api.http import url, HttpPlugin

from aj.api.endpoint import endpoint, EndpointError, EndpointReturn

class Handler(HttpPlugin):
    def __init__(self, context):
        self.context = context

    def handle_api_calculate(self, http_context, operation=None, a=None, b=None):
        start_time = time.time()

            if operation == 'add':
                result = int(a) + int(b)
            elif operation == 'divide':
                result = int(a) / int(b)
                raise EndpointReturn(404)
        except ZeroDivisionError:
            raise EndpointError('Division by zero')

        return {
            'value': result,
            'time': time.time() - start_time

@endpoint(api=True) mode provides automatic JSON encoding of the responses and error handling.

If you need lower-level access to the HTTP response, use @endpoint(page=True):

def handle_api_calculate(self, http_context):
    http_context.add_header('Content-Type', '...')
    content = "Hello!"
    #return http_context.respond_not_found()
    #return http_context.respond_forbidden()
    #return http_context.file('/some/path')
    return content

See aj.http.HttpContext for the available http_context methods.


comments powered by Disqus