Method Decorators¶
toto.invocation contains many decorators that may be applied to the invoke(handler, parameters) functions in method modules in order to modify their behavior.
- toto.invocation.asynchronous(fn)[source]¶
Invoke functions with the @asynchronous decorator will not cause the request handler to finish when they return. Use this decorator to support long running operations like tasks sent to workers or long polling.
Sessions¶
- toto.invocation.authenticated(fn)[source]¶
Invoke functions marked with the @authenticated decorator will attempt to load the current session (either referenced by the x-toto-session-id request header or cookie). If no session is found, or if the current session is anonymous, a “Not authorized” error will be returned to the client.
- toto.invocation.anonymous_session(fn)[source]¶
Invoke functions marked with the @anonymous_session decorator will attempt to load the current session (either referenced by the x-toto-session-id request headers or cookie). If no session is found, an anonymous session will be created.
Note: If the user was previously authenticated, the authenticated session will be loaded.
- toto.invocation.optionally_authenticated(fn)[source]¶
Invoke functions marked with the @optionally_authenticated decorator will attempt to load the current session (either referenced by the x-toto-session-id request header or cookie). If no session is found, the request proceeds as usual.
- toto.invocation.authenticated_with_parameter(fn)[source]¶
Invoke functions marked with the @authenticated_with_parameter decorator will behave like functions decorated with @authenticated but will use the session_id parameter to find the current session instead of the x-toto-session-id header or cookie.
Parameters¶
- toto.invocation.requires(*args)[source]¶
Invoke functions marked with the @requires decorator will error if any of the parameters passed to the decorator are missing. The following example will error if either “param1” or “param2” is not included in the request:
@requires('param1', 'param2') def invoke(handler, parameters): pass
Response¶
- toto.invocation.raw_response(fn)[source]¶
Invoke functions marked with the @raw_response decorator will not be serialized before response to the client. This can be used to send text, html or other binary data without the usual JSON (or other) processing. The handler.response_type property will be used to set the response “Content-Type” header. By default this will be “application/octet-stream”.
- toto.invocation.jsonp(callback_name='jsonp')[source]¶
Invoke functions marked with the @jsonp decorator will return a wrapper response that will call a client-side javascript function. This decorator requires a “jsonp” parameter set to the name of the javascript callback function to be passed with the request. If no “jsonp” parameter is passed, the request will respond like any other Toto request. The decorator can be applied with an optional callback_name argument to specify a parameter to use instead of “jsonp”, e.g.:
@jsonp('callback') def invoke(handler, parameters): #do stuff
Will allow JSONP requests that call the function specified by “callback” in their response. Applying the decorator without the callback_name parameter will use the default “jsonp”:
@jsonp def invoke(handler, parameters): #do stuff
Note: JSONP requests will only be affected by decorators before @jsonp in the decorator chain.
- toto.invocation.error_redirect(redirect_map, default=None)[source]¶
Invoke functions marked with the @error_redirect decorator will redirect according to the redirect_map dictionary. redirect_map should consist of status_code, url pairs. This decorator will check the code then status_code properties of the raised error for matches in the redirect map before falling back to the usual error behavior. The optional default parameter can be used to specify a url to redirect to if there are no matches in redirect_map.
The following code will redirect to “not_found.html” on 404, and “error.html” otherwise:
@error_redirect({'404': 'not_found.html'}, 'error.html') def invoke(handler, parameters): pass
Adding Decorators¶
- toto.invocation._copy_attributes(fn, wrapper, doc=None, attributes=['asynchronous', '__doc__', '__module__', '__name__', '__repr__'])[source]¶
A convenience method for copying certain attributes to a decorated method. Optionally, doc can be passed to append to a the method’s docstring. To specify which attributes to copy, pass an iterable as the attributes argument.