Started adding JSON output.

This commit is contained in:
Nick Daly 2012-06-26 20:53:18 -05:00
parent a6cb8bf916
commit 388d6a4d51
2 changed files with 55 additions and 22 deletions

View File

@ -212,26 +212,58 @@ class Monitor(santiago.SantiagoMonitor):
class RestMonitor(santiago.RestController): class RestMonitor(santiago.RestController):
# FIXME filter input and escape output properly.
# FIXME build tests for this.
# TODO http://tools.cherrypy.org/wiki/ParameterDemonstration
# TODO http://docs.cherrypy.org/dev/concepts/dispatching.html
# TODO https://appmecha.wordpress.com/2008/11/08/variable-url-parameters/
# TODO https://duckduckgo.com/?q=cherrypy+combine+positional+and+keyword+parameters
def __init__(self, aSantiago): def __init__(self, aSantiago):
super(RestMonitor, self).__init__() super(RestMonitor, self).__init__()
self.santiago = aSantiago self.santiago = aSantiago
self.relative_path = "protocols/https/templates/http/" self.relative_path = "protocols/https/templates"
def _parse_query(self, query_input):
"""
Might raise any of: ValueError, TypeError, NameError
"""
query = ""
if query_input:
query = dict([item.split("=") for item in query_input.split("&")])
return query
def respond(self, template, values, encoding="html"):
try:
query = self._parse_query(cherrypy.request.query_string)
except (ValueError, TypeError, NameError):
return
if query:
try:
encoding = query["encoding"]
except KeyError:
pass
def respond(self, template, values):
return [str(Template( return [str(Template(
file=self.relative_path + template, file="/".join((self.relative_path, encoding, template)),
searchList = [dict(values)]))] searchList = [dict(values)]))]
class HostedService(RestMonitor): class HostedService(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self, client, service): def GET(self, client, service, **kwargs):
return self.respond("hostedService.tmpl", { return self.respond("hostedService.tmpl", {
"service": service, "service": service,
"client": client, "client": client,
"locations": self.santiago.hosting[client][service] }) "locations": self.santiago.hosting[client][service] })
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, client="", service="", put="", delete=""): def POST(self, client="", service="", put="", delete="", **kwargs):
if put: if put:
self.PUT(client, service, put) self.PUT(client, service, put)
elif delete: elif delete:
@ -251,13 +283,13 @@ class HostedService(RestMonitor):
class HostedClient(RestMonitor): class HostedClient(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self, client): def GET(self, client, **kwargs):
return self.respond("hostedClient.tmpl", return self.respond("hostedClient.tmpl",
{ "client": client, { "client": client,
"services": self.santiago.hosting[client] }) "services": self.santiago.hosting[client] })
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, client="", put="", delete=""): def POST(self, client="", put="", delete="", **kwargs):
if put: if put:
self.PUT(client, put) self.PUT(client, put)
elif delete: elif delete:
@ -276,12 +308,12 @@ class HostedClient(RestMonitor):
class Hosting(RestMonitor): class Hosting(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self): def GET(self, **kwargs):
return self.respond("hosting.tmpl", return self.respond("hosting.tmpl",
{"clients": [x for x in self.santiago.hosting]}) {"clients": [x for x in self.santiago.hosting]})
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, put="", delete=""): def POST(self, put="", delete="", **kwargs):
if put: if put:
self.PUT(put) self.PUT(put)
elif delete: elif delete:
@ -300,7 +332,7 @@ class Hosting(RestMonitor):
class ConsumedService(RestMonitor): class ConsumedService(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self, host, service): def GET(self, host, service, **kwargs):
return self.respond("consumedService.tmpl", return self.respond("consumedService.tmpl",
{ "service": service, { "service": service,
"host": host, "host": host,
@ -308,7 +340,7 @@ class ConsumedService(RestMonitor):
self.santiago.consuming[host][service] }) self.santiago.consuming[host][service] })
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, host="", service="", put="", delete=""): def POST(self, host="", service="", put="", delete="", **kwargs):
if put: if put:
self.PUT(host, service, put) self.PUT(host, service, put)
elif delete: elif delete:
@ -328,13 +360,13 @@ class ConsumedService(RestMonitor):
class ConsumedHost(RestMonitor): class ConsumedHost(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self, host): def GET(self, host, **kwargs):
return self.respond("consumedHost.tmpl", return self.respond("consumedHost.tmpl",
{ "services": self.santiago.consuming[host], { "services": self.santiago.consuming[host],
"host": host }) "host": host })
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, host="", put="", delete=""): def POST(self, host="", put="", delete="", **kwargs):
if put: if put:
self.PUT(host, put) self.PUT(host, put)
elif delete: elif delete:
@ -353,12 +385,12 @@ class ConsumedHost(RestMonitor):
class Consuming(RestMonitor): class Consuming(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self): def GET(self, **kwargs):
return self.respond("consuming.tmpl", return self.respond("consuming.tmpl",
{ "hosts": [x for x in self.santiago.consuming]}) { "hosts": [x for x in self.santiago.consuming]})
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self, put="", delete=""): def POST(self, put="", delete="", **kwargs):
if put: if put:
self.PUT(put) self.PUT(put)
elif delete: elif delete:
@ -377,15 +409,15 @@ class Consuming(RestMonitor):
class Root(RestMonitor): class Root(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self): def GET(self, **kwargs):
return self.respond("root.tmpl", {}) return self.respond("root.tmpl", {})
class Stop(RestMonitor): class Stop(RestMonitor):
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def POST(self): def POST(self, **kwargs):
self.santiago.live = 0 self.santiago.live = 0
raise cherrypy.HTTPRedirect("/") raise cherrypy.HTTPRedirect("/")
@cherrypy.tools.ip_filter() @cherrypy.tools.ip_filter()
def GET(self): def GET(self, **kwargs):
self.POST() # FIXME cause it's late and I'm tired. self.POST() # FIXME cause it's late and I'm tired.

View File

@ -0,0 +1 @@
$hosts