Package wsgiwebapi :: Module decorators
[frames] | no frames]

Module decorators

source code

Decorators of WSGIWebAPI callables.
Functions
 
jsonreturning(fn)
Decorator to wrap function's return value as JSON.
source code
 
jsonpreturning(paramname='jsonp', valid=r'^[a-zA-Z._\[\]]*$')
Decorator to add JSONP support to an API function.
source code
 
allow_method(method_type, *other_methods)
Decorator to restrict the methods allowed to a specific set.
source code
 
param(paramname, minreps=None, maxreps=None, pattern=None, default=None, doc=None)
Decorator to add parameter validation.
source code
 
noparams(fn)
Decorator to indicate that no parameters may be supplied.
source code
 
pathinfo(*args, **kwargs)
Decorator to indicate the parameter names allowed in pathinfo.
source code
 
copyprops(original_fn, decorated_fn)
Copy the WSGIWebAPI properties from a function to a decorated function.
source code
 
decorate(decorator)
Apply a decorator, but also copy the WSGIWebAPI properties across.
source code
Variables
  allow_GET = allow_method('GET')
  allow_HEAD = allow_method('HEAD')
  allow_GETHEAD = allow_method('GET', 'HEAD')
  allow_POST = allow_method('POST')
Function Details

jsonreturning(fn)

source code 

Decorator to wrap function's return value as JSON.

Before the decorator is applied, the function should return a structure to be converted to JSON. The decorator will set the content_type automatically (to text/javascript).

jsonpreturning(paramname='jsonp', valid=r'^[a-zA-Z._\[\]]*$')

source code 

Decorator to add JSONP support to an API function.

This adds a query parameter (by default, jsonp, but this may be altered with the paramname parameter) to the function. If the parameter is not supplied, the return value is a plain JSON object, otherwise the return value is preceded by the value in the parameter and an open bracket, and followed by a close bracket. The parameter must not be specified multiple times in a single request.

By default, the jsonp parameter may only contain upper and lowercase ASCII alphabetic characters (A-Z, a-z), numbers (0-9), full stops (.), underscores (_), and brackets ([ and ]), but this may be altered by setting the valid parameter to a string containing a regular expression matching the valid parameter values. To avoid performing any validation of the value of the jsonp parameter, set the valid parameter to None.

See http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ for some of the rationale behind JSONP support.

allow_method(method_type, *other_methods)

source code 

Decorator to restrict the methods allowed to a specific set.

May be applied multiple times, to allow more than one method to be allowed.

If applied at all, any methods other than those specified will result in a 405 or 501 error (depending whether the exception is one of the standard known methods).

(This stores the allowed methods in an attribute of the decorated function, so tht repeated application can allow

param(paramname, minreps=None, maxreps=None, pattern=None, default=None, doc=None)

source code 

Decorator to add parameter validation.

If this is used at all, the noparams decorator may not also be used.

This decorator may only be used once for each parameter name.

  • paramname is required - the name of the parameter in request.params.
  • minreps: the minimum number of times the parameter must be specified. If omitted or None, the parameter need not be specified. (Note that, if default is specified, it is always valid to supply the parameter exactly 0 times, regardless of the setting of this parameter.)
  • maxreps: the maximum number of times the parameter may be specified. If omitted or None, there is no limit on the number of times the parameter may be specified.
  • pattern: a (python regular expression) pattern which the parameter must match. If None, no validation is performed.
  • default: a default value for the parameter list. Note - this is used only if the parameter doesn't occur at all, and is simply entered into request.params instead of the parameter (it should usually be a sequence, to make the normal values placed into request.params).
  • doc: a documentation string for the parameter.

copyprops(original_fn, decorated_fn)

source code 

Copy the WSGIWebAPI properties from a function to a decorated function.

If you write your own decorators and apply them to WSGIWebAPI decorated functions, you should call this method in your decorator to copy the WSGIWebAPI properties into your decorated function. If you don't do this, you may get confusing failures, such as pathinfo not being allowed.

decorate(decorator)

source code 

Apply a decorator, but also copy the WSGIWebAPI properties across.

To use this, pass the decorator you wish to apply as a parameter to this decorator. The returned decorator will apply this decorator, and then copy the WSGIWebAPI properties across.