From af8c23a332ce342d78aae77fbd29e97e5a2c61e9 Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Mon, 28 May 2012 17:46:37 -0500 Subject: [PATCH] Started building the hosting and consuming handlers. - Remove todo, the idea's there. --- .../santiago/protocols/https/controller.py | 14 -- .../protocols/https/controller_test.py | 158 ++++++++++++++++++ 2 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 ugly_hacks/santiago/protocols/https/controller_test.py diff --git a/ugly_hacks/santiago/protocols/https/controller.py b/ugly_hacks/santiago/protocols/https/controller.py index 37b3527b1..350e621c3 100644 --- a/ugly_hacks/santiago/protocols/https/controller.py +++ b/ugly_hacks/santiago/protocols/https/controller.py @@ -1,19 +1,5 @@ """The HTTPS Santiago listener and sender. -FIXME: use a reasonable RESTful API. - -- https://appmecha.wordpress.com/2008/10/27/cherrypy-gae-routing-2/ -- http://docs.cherrypy.org/dev/progguide/REST.html -- http://docs.cherrypy.org/dev/concepts/dispatching.html -- http://tools.cherrypy.org/wiki/RestfulDispatch -- http://docs.cherrypy.org/dev/refman/_cpdispatch.html -- http://www.infoq.com/articles/rest-introduction -- http://www.infoq.com/articles/rest-anti-patterns -- http://stackoverflow.com/a/920181 - -It's been about five times too long since I've looked at this sort of -thing. Stupid everything-is-GET antipattern. - """ diff --git a/ugly_hacks/santiago/protocols/https/controller_test.py b/ugly_hacks/santiago/protocols/https/controller_test.py new file mode 100644 index 000000000..c4aae1bb9 --- /dev/null +++ b/ugly_hacks/santiago/protocols/https/controller_test.py @@ -0,0 +1,158 @@ +"""The HTTPS Santiago listener and sender. + +TODO: Build out /consuming too. +TODO: build out the rest of the actions. + +""" + + +from Cheetah.Template import Template +import cherrypy +import httplib, urllib, urlparse +import sys +import logging +import wsgiref.handlers + + +mykey = str(8) +hosting_data = { mykey: { "santiago": set( ["https://localhost:8080"] )}} +consuming_data = { "santiago": { mykey: set( ["https://localhost:8080"] )}} + + +def setup(santiago): + # TODO call this bugger to prep the dispatcher, objects, etc. + pass + +class RestController(object): + + def PUT(self, *args, **kwargs): + raise NotImplemented("RestController.PUT") + + def GET(self, *args, **kwargs): + raise NotImplemented("RestController.GET") + + def POST(self, *args, **kwargs): + raise NotImplemented("RestController.POST") + + def DELETE(self, *args, **kwargs): + raise NotImplemented("RestController.DELETE") + + +class Hosting(RestController): + exposed = True + + def __init__(self, data): + self.hosting = data + + def GET(self): + try: + clients = [x for x in self.hosting] + message = "success" + except KeyError: + clients = [] + message = "Error." + finally: + return [str(Template( + file="templates/hosting-get.tmpl", + searchList=[{ + "clients": clients, + "message": message }]))] + +class Client(object): + exposed = True + + def __init__(self, data): + self.hosting = data + + def GET(self, client=0): + try: + services = [x for x in self.hosting[client]] + message = "success" + except KeyError: + services = [] + message = "Error." + finally: + return [str(Template( + file="templates/client-get.tmpl", + searchList=[{ + "client": client, + "services": services, + "message": message }]))] + +class HostedService(object): + exposed = True + + def __init__(self, data): + self.hosting = data + + def GET(self, client=0, service=""): + try: + locations = [x for x in self.hosting[client][service]] + message = "success" + except KeyError: + locations = [] + message = "Error." + finally: + return [str(Template( + file="templates/hostedService-get.tmpl", + searchList=[{ + "client": client, + "service": service, + "locations": locations, + "message": message }]))] + +class Location(object): + exposed = True + + def __init__(self, data): + self.hosting = data + + def GET(self, client=0, service=0, location=0): + try: + location = self.hosting[client][service].find(location) + message = "success" + except KeyError: + location = "" + message = "error" + return [str(Template( + file="templates/location-get.tmpl", + searchList=[{ + "client": client, + "service": service, + "location": location, + "message": message, }]))] + +def rest_connect(dispatcher, location, controller, trailing_slash=True): + """Simple REST connector for object/location mapping.""" + + if trailing_slash: + location = location.rstrip("/") + location = [location, location + "/"] + else: + location = [location] + + for place in location: + for a_method in ("PUT", "GET", "POST", "DELETE"): + dispatcher.connect(controller.__class__.__name__ + a_method, + place, controller=controller, action=a_method, + conditions={ "method": [a_method] }) + + return dispatcher + + +if __name__ == "__main__": + + hosting = Hosting(hosting_data) + client = Client(hosting_data) + service = HostedService(hosting_data) + location = Location(hosting_data) + d = cherrypy.dispatch.RoutesDispatcher() + + rest_connect(d, '/hosting/:client/:service/:location', location) + rest_connect(d, '/hosting/:client/:service', service) + rest_connect(d, '/hosting/:client', client) + rest_connect(d, '/hosting/', hosting) + + cherrypy.config.update({"server.socket_host": "0.0.0.0"}) + + cherrypy.quickstart(hosting, "/", { "/": {'request.dispatch': d, }})