OPSI.web2.resource module

I hold the lowest-level L{Resource} class and related mix-in classes.

class OPSI.web2.resource.RenderMixin

Bases: object

Mix-in class for L{iweb.IResource} which provides a dispatch mechanism for handling HTTP methods.

allowedMethods()

@return: A tuple of HTTP methods that are allowed to be invoked on this resource.

checkPreconditions(request)

Checks all preconditions imposed by this resource upon a request made against it. @param request: the request to process. @raise http.HTTPError: if any precondition fails. @return: C{None} or a deferred whose callback value is C{request}.

http_GET(request)

Respond to a GET request.

This implementation validates that the request body is empty and then dispatches the given C{request} to L{render} and returns its result.

@param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

http_HEAD(request)

Respond to a HEAD request. @param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

http_OPTIONS(request)

Respond to a OPTIONS request. @param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

http_TRACE(request)

Respond to a TRACE request. @param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

render(request)

Subclasses should implement this method to do page rendering. See L{http_GET}. @param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

renderHTTP(request)

See L{iweb.IResource.renderHTTP}.

This implementation will dispatch the given C{request} to another method of C{self} named C{http_}METHOD, where METHOD is the HTTP method used by C{request} (eg. C{http_GET}, C{http_POST}, etc.).

Generally, a subclass should implement those methods instead of overriding this one.

C{http_*} methods are expected provide the same interface and return the same results as L{iweb.IResource}C{.renderHTTP} (and therefore this method).

C{etag} and C{last-modified} are added to the response returned by the C{http_*} header, if known.

If an appropriate C{http_*} method is not found, a L{responsecode.NOT_ALLOWED}-status response is returned, with an appropriate C{allow} header.

@param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

class OPSI.web2.resource.Resource

Bases: OPSI.web2.resource.RenderMixin

An L{iweb.IResource} implementation with some convenient mechanisms for locating children.

addSlash = False
child_(request)

This method locates a child with a trailing C{“/”} in the URL. @param request: the request to process.

http_GET(request)
locateChild(request, segments)

Locates a child resource of this resource. @param request: the request to process. @param segments: a sequence of URL path segments. @return: a tuple of C{(child, segments)} containing the child of this resource which matches one or more of the given C{segments} in sequence, and a list of remaining segments.

putChild(path, child)

Register a static child.

This implementation registers children by assigning them to attributes with a C{child_} prefix. C{resource.putChild(“foo”, child)} is therefore same as C{o.child_foo = child}.

@param path: the name of the child to register. You almost certainly
don’t want C{“/”} in C{path}. If you want to add a “directory” resource (e.g. C{/foo/}) specify C{path} as C{“”}.

@param child: an object adaptable to L{iweb.IResource}.

class OPSI.web2.resource.PostableResource

Bases: OPSI.web2.resource.Resource

A L{Resource} capable of handling the POST request method.

http_POST(request)

Respond to a POST request. Reads and parses the incoming body data then calls L{render}. @param request: the request to process. @return: an object adaptable to L{iweb.IResponse}.

class OPSI.web2.resource.LeafResource

Bases: OPSI.web2.resource.RenderMixin

A L{Resource} with no children.

locateChild(request, segments)
class OPSI.web2.resource.WrapperResource(resource)

Bases: object

An L{iweb.IResource} implementation which wraps a L{RenderMixin} instance and provides a hook in which a subclass can implement logic that is called before request processing on the contained L{Resource}.

hook(request)

Override this method in order to do something before passing control on to the wrapped resource’s C{renderHTTP} and C{locateChild} methods. @return: None or a L{Deferred}. If a deferred object is

returned, it’s value is ignored, but C{renderHTTP} and C{locateChild} are chained onto the deferred as callbacks.
locateChild(request, segments)
renderHTTP(request)