Example #1
0
File: core.py Project: crito/purest
    def __init__(self):
        self._map = Map()

        # Just for convenience, needs to be moved into the app modules
        self._map.add(r'^/collectd/data', {'POST': collectd.Collectd.post})
        self._map.add(r'^/metrics/all_dbs', {'GET': metrics.Collection.all})
        self._map.add(
                r'^/metrics/(?P<host>[\w]+)/(?P<plugin>[\w]+)/(?P<plugin_instance>[\w]+)/(?P<type>[\w]+)/(?P<type_instance>[\w]+)/$',
                {'GET': metrics.Collection.filter})
Example #2
0
File: core.py Project: crito/purest
class URIHandler(object):
    """Manage all uri/method routes."""
    def __init__(self):
        self._map = Map()

        # Just for convenience, needs to be moved into the app modules
        self._map.add(r'^/collectd/data', {'POST': collectd.Collectd.post})
        self._map.add(r'^/metrics/all_dbs', {'GET': metrics.Collection.all})
        self._map.add(
                r'^/metrics/(?P<host>[\w]+)/(?P<plugin>[\w]+)/(?P<plugin_instance>[\w]+)/(?P<type>[\w]+)/(?P<type_instance>[\w]+)/$',
                {'GET': metrics.Collection.filter})

    def parse(self, request):
        """Take a uri and method and parse the the Map object for the right handler.

        :param request: The HTTP request.
        :type request: dictionary
        :rtype: A tupel containing the handler and regex groups (handler, kwargs).
        """
        #try:
        match = self._map.resolve(request['path'], request['method'])
        #except TypeError:
        #return URIHandler.fourofour

        return match

    def map(self, uri, method, handler):
        """Map a request using its uri and http method to the right handler.

        :param uri: a path of a uri expressed as a regex
        :type uri: string
        :param method: the http method
        :type string: string
        """
        self._map.add(uri, method, handler)

    @classmethod
    def fourofour(cls, start_response):
        """Return a 404 status code."""
        response_headers = [('Content-type', 'text/plain')]
        status = '404 Not Found'

        start_response(status, response_headers)
        yield ''