devilry.rest — General purpose RESTful tools

http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services

This API will replace Low level RESTful web service API.

API

class devilry.rest.restview.RestView(restapicls, apiname, apiversion, suffix_to_content_type_map={'xml': 'application/xml', 'yaml': 'application/yaml', 'json': 'application/json', 'html': 'text/html', 'extjs.json': 'application/extjsjson'}, input_data_preprocessors=[], output_data_postprocessors=[<function extjs at 0x10d6a05f0>], output_content_type_detectors=[<function devilry_accept_querystringparam at 0x10d697de8>, <function suffix at 0x10d6975f0>, <function from_acceptheader at 0x10d697d70>], input_content_type_detectors=[<function from_content_type_header at 0x10d697140>, <function use_output_content_type at 0x10d6971b8>], inputdata_handlers=[<function getqrystring_inputdata_handler at 0x10d697f50>, <function rawbody_inputdata_handler at 0x10d6a0050>], dataconverters={'application/extjsjson': <class 'devilry.dataconverter.jsondataconverter.JsonDataConverter'>, 'application/json': <class 'devilry.dataconverter.jsondataconverter.JsonDataConverter'>, 'application/yaml': <class 'devilry.dataconverter.yamldataconverter.YamlDataConverter'>, 'text/html': <class 'devilry.dataconverter.htmldataconverter.HtmlDataConverter'>, 'application/xml': <class 'devilry.dataconverter.xmldataconverter.XmlDataConverter'>}, restmethod_routers=[<function post_to_create at 0x10d6a0140>, <function get_with_id_to_read at 0x10d6a01b8>, <function put_with_id_to_update at 0x10d6a02a8>, <function delete_to_delete at 0x10d6a0398>, <function get_without_id_to_list at 0x10d6a0230>, <function put_without_id_to_batch at 0x10d6a0320>], response_handlers=[<function stricthttp at 0x10d6a00c8>])[source]

Django view that handles input/output to devilry.rest.restbase.RestBase.

Parameters:
  • restapicls – A class implementing devilry.rest.restbase.RestBase.
  • suffix_to_content_type_map – Maps suffix to content type. Used to determine content-type from url-suffix.
  • input_data_preprocessors

    List of input data post-processor callbacks. The callbacks have the following signature:

    match, data = f(request, output_data)
    

    The data of the first matching callback will be used. If no processor matches, the unchanged data will be used.

    Together with output_data_postprocessors this allows for wrapping certain content-types with extra data. Example of use is to add data that is required by a javascript library, such as the successful attribute required by ExtJS.

  • output_data_postprocessors – List of input data post-processor callbacks. See input_data_preprocessors for more details. Note that the callbacks take one additional argument, a boolean telling if the restful method completed without error.
  • output_content_type_detectors

    Input content type detectors detect the content type of the request data. Must be a list of callables with the following signature:

    content_type  = f(request, suffix)
    
    The first content_type that is not bool(content_type)==False will
    be used.
  • input_content_type_detectors

    Similar to output_content_type_detectors, except for input/request instead of for output/response. Furthermore, the the callbacks take the output content-type as the third argument:

    content_type  = f(request, suffix, output_content_type)
    

    This is because few clients send the CONTENT_TYPE header, and falling back on output content-type is a mostly sane default.

  • inputdata_handlers

    Input data handlers convert input data into a dict. Input data can come from several sources:

    • Querystring
    • Paramater in querystring
    • Request body

    Therefore, we need to check for data in several places. Instead of hardcoding this checking, we accept a list of callables that does the checking.

    Must be a list of callables with the following signature:

    match, data = f(request, input_content_type, dataconverters)
    

    The first input data handler returning match==True is be used.

    See devilry.rest.inputdata_handlers for implementations.

    Input data can come in many different formats and from different sources. Examples are such XML in request body, query string and JSON embedded in a query string parameter.

  • dataconverters

    A dict of implementations of devilry.dataconverter.dataconverter.DataConverter. The key is a content-type. Data converters convert between python and some other format, such as JSON or XML.

    Typically used by input_datahandlers and response_handlers to convert data
    input the content_type detected by one of the output_content_type_detectors.
  • restmethod_routers

    A list of callables with the following signature:

    restapimethodname, args, kwargs = f(request, id, input_data)
    

    None must be returned if the route does not match.

    Restmetod routes takes determines which method in the devilry.rest.restbase.RestBase interface to call, and the arguments to use for the call.

  • response_handlers

    Response handlers are responsible for creating responses. Signature:

    reponse = f(request, restapimethodname, output_content_type, encoded_output)
    

    The first response handler returning bool(response) == True is used.

Table Of Contents

Previous topic

Low level RESTful web service API

Next topic

devilry.apps.extjshelpers — ExtJS helpers

This Page