Decorate a method to inject an HTTPRequest.

    Generates an HTTPRequest using the given HTTP method and relative
    URI.  If additional positional arguments are present, they are
    expected to be strings that name function arguments that should be
    included as the query parameters of the URL.  If additional
    keyword arguments are present, the keywords are expected to name
    function arguments and the values are expected to name headers to
    set from those values.  The request is injected as the first
    function argument after the 'self' argument.

    Note that two attributes must exist on the object the method is
    called on: the '_baseurl' attribute specifies the URL that reluri
    is relative to; and the '_make_req' attribute specifies a method
    that instantiates an HTTPRequest from a method and full url (which
    will include query arguments).

def restmethod(method, reluri, *qargs, **headers):
    """Decorate a method to inject an HTTPRequest.

    Generates an HTTPRequest using the given HTTP method and relative
    URI.  If additional positional arguments are present, they are
    expected to be strings that name function arguments that should be
    included as the query parameters of the URL.  If additional
    keyword arguments are present, the keywords are expected to name
    function arguments and the values are expected to name headers to
    set from those values.  The request is injected as the first
    function argument after the 'self' argument.

    Note that two attributes must exist on the object the method is
    called on: the '_baseurl' attribute specifies the URL that reluri
    is relative to; and the '_make_req' attribute specifies a method
    that instantiates an HTTPRequest from a method and full url (which
    will include query arguments).
    """

    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Process the arguments against the original function
            argmap, theSelf, req_name = _getcallargs(func, args, kwargs)

            # Build the URL
            url = _urljoin(theSelf._baseurl, reluri.format(**argmap))

            # Build the query string, as needed
            if qargs:
                query = dict([(k, argmap[k]) for k in qargs
                              if argmap[k] is not None])
                if query:
                    url += '?%s' % urllib.urlencode(query)

            # Build the headers, if needed
            hlist = None
            if headers:
                hlist = hdrs.HeaderDict()
                for aname, hname in headers.items():
                    if argmap[aname]:
                        hlist[hname] = argmap[aname]
                if not hlist:
                    # If there are no headers, don't send any
                    hlist = None

            # Now, build the request and pass it to the method
            argmap[req_name] = theSelf._make_req(method, url,
                                                 func.__name__, hlist)

            # Call the method
            return func(**argmap)

        # Return the function wrapper
        return wrapper

    # Return the actual decorator
    return decorator