Beispiel #1
0
def route(path, methods=None):
    """
    Decorator to register a webhook route. You must specify a path
    regular expression, and optionally a list of HTTP methods to accept.
    If you do not specify a list of HTTP methods, only GET requests will
    be served. All regex matches must be named groups and they will be passed
    as keyword arguments. For those who are familiar with something
    like Flask, this will feel very similar. Example::

        @route(r'/users/(?P<user_id>[0-9]+)')
        def get_user(request, irc_client, user_id):
            pass

        @route(r'/foo/(?P<subpage>[a-z]+)/(?P<page_id>[0-9]+)', methods=['GET', 'POST'])
        def page(request, irc_client, subpage, page_id):
            pass

    Note that route callables (shown above) must accept as the first two
    positional arguments a request object, and the current irc client.
    """
    plugin = registry.get_plugin('webhooks')
    if methods is None:
        methods = ['GET']

    def wrapper(fn):
        plugin.add_route(fn, path, methods)
        return fn

    return wrapper
Beispiel #2
0
def route(path, methods=None):
    """
    Decorator to register a webhook route. This requires a path regular expression, and
    optionally a list of HTTP methods to accept, which defaults to accepting ``GET`` requests
    only. Incoming HTTP requests that use a non-allowed method will receive a 405 HTTP response.

    :param str path: a regular expression string for the URL path of the route
    :param methods: a list of accepted HTTP methods for this route, defaulting to ``['GET']``

    Decorated routes must follow this pattern:

    .. function:: func(request, client)
        :noindex:

        :param request: The incoming HTTP request, ``twisted.web.http.Request``
        :param client: The IRC client connection. An instance of :class:`helga.comm.Client`
        :returns: a string HTTP response
    """
    plugin = registry.get_plugin('webhooks')
    if methods is None:
        methods = ['GET']

    def wrapper(fn):
        if plugin is not None:
            plugin.add_route(fn, path, methods)
        return fn

    return wrapper
Beispiel #3
0
def route(path, methods=None):
    """
    Decorator to register a webhook route. You must specify a path
    regular expression, and optionally a list of HTTP methods to accept.
    If you do not specify a list of HTTP methods, only GET requests will
    be served. All regex matches must be named groups and they will be passed
    as keyword arguments. For those who are familiar with something
    like Flask, this will feel very similar. Example::

        @route(r'/users/(?P<user_id>[0-9]+)')
        def get_user(request, irc_client, user_id):
            pass

        @route(r'/foo/(?P<subpage>[a-z]+)/(?P<page_id>[0-9]+)', methods=['GET', 'POST'])
        def page(request, irc_client, subpage, page_id):
            pass

    Note that route callables (shown above) must accept as the first two
    positional arguments a request object, and the current irc client.
    """
    plugin = registry.get_plugin('webhooks')
    if methods is None:
        methods = ['GET']

    def wrapper(fn):
        if plugin is not None:
            plugin.add_route(fn, path, methods)
        return fn

    return wrapper
Beispiel #4
0
def route(path, methods=None):
    """
    Decorator to register a webhook route. This requires a path regular expression, and
    optionally a list of HTTP methods to accept, which defaults to accepting ``GET`` requests
    only. Incoming HTTP requests that use a non-allowed method will receive a 405 HTTP response.

    :param path: a regular expression string for the URL path of the route
    :param methods: a list of accepted HTTP methods for this route, defaulting to ``['GET']``

    Decorated routes must follow this pattern:

    .. function:: func(request, client)
        :noindex:

        :param request: The incoming HTTP request, ``twisted.web.http.Request``
        :param client: The client connection. An instance of :class:`helga.comm.irc.Client`
                       or :class:`helga.comm.xmpp.Client`
        :returns: a string HTTP response
    """
    plugin = registry.get_plugin('webhooks')
    if methods is None:
        methods = ['GET']

    def wrapper(fn):
        if plugin is not None:
            plugin.add_route(fn, path, methods)
        return fn

    return wrapper
Beispiel #5
0
 def test_get_plugin_returns_none(self):
     assert registry.get_plugin('foo') is None
Beispiel #6
0
 def test_get_plugin(self):
     registry.register('foo', self.valid_plugins[0])
     registry.register(self.snowman, self.valid_plugins[0])
     assert self.valid_plugins[0] == registry.get_plugin('foo')
     assert self.valid_plugins[0] == registry.get_plugin(self.snowman)
Beispiel #7
0
 def test_get_plugin_returns_none(self):
     assert registry.get_plugin('foo') is None
Beispiel #8
0
 def test_get_plugin(self):
     registry.register('foo', self.valid_plugins[0])
     registry.register(self.snowman, self.valid_plugins[0])
     assert self.valid_plugins[0] == registry.get_plugin('foo')
     assert self.valid_plugins[0] == registry.get_plugin(self.snowman)