Resource validation

A collection of functions used in both server and client reference implementations.

RestAuthCommon.CONTENT_HANDLERS

Warning

This variable will be moved to RestAuthCommon.handlers soon.

Mapping of MIME types to their respective handler implemenation. You can use this dictionary to dynamically look up a content handler if you do not know the requested content type in advance.

MIME type handler notes
application/json handlers.json default
application/x-www-form-urlencoded handlers.form Only use this for testing
application/xml handlers.xml not yet implemented

If you want to provide your own implementation of a content_handler, you can add it to this dictionary with the appropriate MIME type as the key.

RestAuthCommon.marshal(content_type, obj)[source]

Marshal the object obj into a string of the MIME type content_type.

This method is just intended as a shortcut for content_handler.unmarshal(). If you intend to use a handler multiple times, it is better to instantiate a specific handler directly to save dictionary lookups and object instantiations.

Warning

This method will be deprecated soon.

Parameters:
  • content_type (str) – The format that the object should be marshalled into. This has to be one of the keys defined in CONTENT_HANDLERS.
  • obj – The object to marshal.
Returns:

The string representation of the object.

Return type:

str

Raises error.MarshalError:
 

When the handler could not marshal the object.

See also:

content_handler.marshal()

RestAuthCommon.resource_validator(name)[source]

Warning

This code will be moved to its own submodule soon.

Check the name of a resource for some really bad characters that shouldn’t be used anywhere in RestAuth.

This filters names containing a slash (“/”) or colon (”:”) and those starting with ‘.’. It also filters control characters etc., including those from unicode.

Parameters:name (str) – The name to validate
Returns:False if the name contains any invalid characters, True otherwise.
Return type:bool
RestAuthCommon.unmarshal(content_type, raw_data, typ)[source]

Unmarshal the string raw_data into an object of type typ. The string is assumed to be of the MIME type content_type.

This method is just intended as a shortcut for content_handler.unmarshal(). If you intend to use a handler multiple times, it is better to instantiate a specific handler directly to save dictionary lookups and object instantiations.

Warning

This method will be deprecated soon.

Parameters:
  • content_type (str) – The format that the object should be marshalled into. This has to be one of the keys defined in CONTENT_HANDLERS.
  • raw_data (str) – The raw string that should be unmarshalled.
  • typ – The type the unmarshaled object should be of.
Return type:

typ

Returns:

The unmarshalled data. The object has the type specified by the I{typ} parameter.

Raises error.UnmarshalError:
 

When the handler was unable unmarshal the object.

See also:

content_handler.unmarshal()

Previous topic

Content handlers

Next topic

Error handling

This Page