pyfarm.agent.http.core.resource module

Resource

Base resources which can be used to build top leve documents, pages, or other types of data for the web.

class pyfarm.agent.http.core.resource.Resource[source]

Bases: twisted.web.resource.Resource

Basic subclass of _Resource for passing requests to specific methods. Unlike _Resource however this will will also handle:

  • Templates
  • Content type discovery and validation
  • Handling of deferred responses
  • Validation of POST/PUT data against a schema
Variables:
  • TEMPLATE (string) – The name of the template this class will use when rendering an html view.
  • SCHEMAS

    A dictionary of schemas to validate the data of an incoming request against. The structure of this dictionary is:

    {http method: <instance of voluptuous.Schema>}
    

    If the schema validation fails the request will be rejected with 400 BAD REQUEST.

  • ALLOWED_CONTENT_TYPE

    An instance of frozenset which describes what this resource is going to allow in the Content-Type header. The request and this instance must share at least on entry in common. If not, the request will be rejected with 415 UNSUPPORTED MEDIA TYPE.

    This must be defined in subclass

  • ALLOWED_ACCEPT

    An instance of frozenset which describes what this resource is going to allow in the Accept header. The request and this instance must share at least one entry in common. If not, the request will be rejected with 406 NOT ACCEPTABLE.

    This must be defined in subclass

  • DEFAULT_ACCEPT – If Accept header is not present in the request, use this as the value instead. This defaults to frozenset(["*/*"])
  • DEFAULT_CONTENT_TYPE – If Content-Type header is not present in the request, use this as the value instead. This defaults to frozenset([""])
TEMPLATE = NotImplemented
SCHEMAS = {}
ALLOWED_ACCEPT = NotImplemented
ALLOWED_CONTENT_TYPE = NotImplemented
DEFAULT_ACCEPT = frozenset(['*/*'])
DEFAULT_CONTENT_TYPE = frozenset([None])
template

Loads the template provided but the partial path in TEMPLATE on the class.

methods()[source]

Returns a tuple of methods which an instance of this class implements

get_content_type(request)[source]

Return the Content-Type header(s) in the request or DEFAULT_CONTENT_TYPE if the header is not set.

get_accept(request)[source]

Return the Accept header(s) in the request or DEFAULT_ACCEPT if the header is not set.

putChild(path, child)[source]

Overrides the builtin putChild() so we can return the results for each call and use them externally.

error(request, code, message)[source]

Writes the proper out an error response message depending on the content type in the request

set_response_code_if_not_set(request, code)[source]

Sets the response code if one has not already been set

render_tuple(request, response)[source]

Takes a response tuple of (body, code, headers) or (body, code) and renders the resulting data onto the request.

render_deferred(*args, **kwargs)[source]

An inline callback used to unpack a deferred response object.

render(request)[source]