#!/usr/bin/env python3

import cherrypy

class JSONDataService(object):

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @cherrypy.tools.json_in()
    def process(self):
        request = cherrypy.request.json
        if not "handler" in request.keys():
            raise cherrypy.HTTPError(406, "handler undefined")
        if not "component" in request.keys():
            raise cherrypy.HTTPError(406, "component undefined")
        if not "goal" in request.keys():
            raise cherrypy.HTTPError(406, "goal undefined")
        else:
            with cherrypy.HTTPError.handle(Exception):
                return self.handle(request["handler"], request["component"], request["goal"])

    def handle(self, handler, component, goal):
        print("handler: %s, component: %s, goal: %s" % (handler, component, goal))
        return "data printed successfully"

if __name__ == '__main__':
    config = {'server.socket_host': '0.0.0.0',
              # 'server.ssl_module': 'builtin',
              # 'server.ssl_certificate': '/tmp/some.cer',
              # 'server.ssl_private_key': '/tmp/some.key'
    }
    cherrypy.config.update(config)
    cherrypy.quickstart(JSONDataService())
