tornadowebapi package¶
Subpackages¶
Submodules¶
tornadowebapi.authenticator module¶
-
class
tornadowebapi.authenticator.
NullAuthenticator
[source]¶ Bases:
tornadowebapi.authenticator.Authenticator
Authenticator class for the web handlers that does nothing and returns None
-
classmethod
authenticate
(handler)[source]¶ Called by the handler to authenticate the user. The handler passes itself as an argument, and expects a valid handler.current_user value, or None.
Note that returning None does not mean that the API will reject the request. Just that the current_user is unrecognized. Individual Resources must then adapt their behavior according to this information
-
classmethod
tornadowebapi.exceptions module¶
-
exception
tornadowebapi.exceptions.
BadRepresentation
(message=None, **kwargs)[source]¶ Bases:
tornadowebapi.exceptions.WebAPIException
Exception raised when the resource representation is invalid or does not contain the appropriate keys. Raise this exception in your handlers when the received representation is ill-formed
-
http_code
= 400¶
-
-
exception
tornadowebapi.exceptions.
BadRequest
(message=None, **kwargs)[source]¶ Bases:
tornadowebapi.exceptions.WebAPIException
Deprecated. Kept for compatibility. Use BadRepresentation.
-
http_code
= 400¶
-
-
exception
tornadowebapi.exceptions.
Exists
(message=None, **kwargs)[source]¶ Bases:
tornadowebapi.exceptions.WebAPIException
Represents a case where the resource could not be created because it already exists. This is generally raised in the create() method if the resource has uniqueness constraints on things other than the exposed id.
-
http_code
= 409¶
-
-
exception
tornadowebapi.exceptions.
NotFound
(message=None, **kwargs)[source]¶ Bases:
tornadowebapi.exceptions.WebAPIException
Exception raised when the resource is not found. Raise this exception in your handlers when you can’t find the resource the identifier refers to.
-
http_code
= 404¶
-
-
exception
tornadowebapi.exceptions.
Unable
(message=None, **kwargs)[source]¶ Bases:
tornadowebapi.exceptions.WebAPIException
Exception raised when the request cannot be performed for whatever reason that is not dependent on the client.
-
http_code
= 500¶
-
-
exception
tornadowebapi.exceptions.
WebAPIException
(message=None, **kwargs)[source]¶ Bases:
Exception
Base exception for the REST infrastructure These are exceptions that can be raised by the handlers.
-
http_code
= 500¶ HTTP code generally associated to this exception. Missing any better info, default is a server error.
-
tornadowebapi.handler module¶
-
class
tornadowebapi.handler.
BaseHandler
(application, request, **kwargs)[source]¶ Bases:
tornado.web.RequestHandler
-
api_version
¶ Returns the API version this handler is taking care of
-
base_urlpath
¶ Returns the Base urlpath as from initial setup
-
get_resource_handler_or_404
(collection_name)[source]¶ Given a collection name, inquires the registry for its associated Resource class. If not found raises HTTPError(NOT_FOUND)
-
initialize
(registry, base_urlpath, api_version)[source]¶ Initialization method for when the class is instantiated.
-
log
¶
-
registry
¶ Returns the class vs Resource registry
-
-
class
tornadowebapi.handler.
CollectionHandler
(application, request, **kwargs)[source]¶ Bases:
tornadowebapi.handler.BaseHandler
Handler for URLs addressing a collection.
-
class
tornadowebapi.handler.
JSAPIHandler
(application, request, **kwargs)[source]¶ Bases:
tornadowebapi.handler.BaseHandler
Handles the JavaScript API request.
-
class
tornadowebapi.handler.
ResourceHandler
(application, request, **kwargs)[source]¶ Bases:
tornadowebapi.handler.BaseHandler
Handler for URLs addressing a resource.
-
SUPPORTED_METHODS
= ('GET', 'POST', 'PUT', 'DELETE')¶
-
tornadowebapi.registry module¶
-
class
tornadowebapi.registry.
Registry
[source]¶ Bases:
object
-
api_handlers
(base_urlpath, version='v1')[source]¶ Returns the API handlers for the interface. Add these handlers to your application to provide an interface to your Resources.
Parameters: Notes
The current implementation does not support multiple API versions yet. The version option is only provided for futureproofing.
-
authenticator
¶
-
register
(typ, collection_name=None)[source]¶ Registers a Resource type with an appropriate collection name. A collection name is a pluralized version of the resource, created by lowercasing the class name and adding an “s”. The resulting collection name will be used in the URL representing the resource. For example, a resource Image will have URLs of the type
http://example.com/api/v1/images/identifier/
The collection name can always be overridden by specifying __collection_name__ in the resource class, or by specifying the collection_name parameter.
Parameters: Raises: TypeError: – if typ is not a subclass of Resource
-
registered_types
¶
-
-
tornadowebapi.registry.
registry
= <tornadowebapi.registry.Registry object>¶ global registry for registration of the classes.
tornadowebapi.resource module¶
-
class
tornadowebapi.resource.
Resource
(application, current_user)[source]¶ Bases:
object
Base class for resources. To implement a new Resource class, inherit from this subclass and reimplement the CRUD class methods with the appropriate logic.
The Resource exports two member vars: application and current_user. They are equivalent to the members in the tornado web handler.
-
create
(representation)[source]¶ Called to create a resource with a given representation The representation is a dictionary containing keys. The reimplementing code is responsible for checking the validity of the representation. Correspond to a POST operation on the resource collection.
Parameters: representation (dict) – A dictionary containing the representation as from the HTTP request.
Returns: id – An identifier identifying the newly created resource. It must be unique within the collection.
Return type: Raises: - Exists: – Raised when the resource cannot be created because of a conflicting already existing resource.
- BadRepresentation: – Raised when the representation does not validate according to the resource expected representation.
- NotImplementedError: – If the resource does not support the method.
-
delete
(identifier)[source]¶ Called to delete a specific resource given its identifier. Corresponds to a DELETE operation on the resource URL.
Parameters: identifier (str) – A string identifying the resource
Returns: Return type: Raises: - NotFound: – Raised if the resource with the given identifier cannot be found
- NotImplementedError: – If the resource does not support the method.
-
exists
(identifier)[source]¶ Returns True if the resource with a given identifier exists. False otherwise.
Parameters: identifier (str) – A string identifying the resource Returns: bool Return type: True if found, False otherwise.
-
items
()[source]¶ Invoked when a request is performed to the collection URL. Returns a list of identifiers available. Corresponds to a GET operation on the collection URL.
Returns: list Return type: The list of available identifiers. Raises: NotImplementedError: – If the resource collection does not support the method. Notes
For security reasons stemming from cross site execution, this list will not be rendered as a list in a json representation. Instead, a dictionary with the key “items” and value as this list will be returned.
-
retrieve
(identifier)[source]¶ Called to retrieve a specific resource given its identifier. Correspond to a GET operation on the resource URL.
Parameters: identifier (str) – A string identifying the resource
Returns: representation – a dict representation of the resource.
Return type: Raises: - NotFound: – Raised if the resource with the given identifier cannot be found
- NotImplementedError: – If the resource does not support the method.
-
update
(identifier, representation)[source]¶ Called to update a specific resource given its identifier with a new representation. The method is responsible for validating the representation content. Correspond to a PUT operation on the resource URL.
Parameters: Returns: Return type: Raises: - NotFound: – Raised if the resource with the given identifier cannot be found
- BadRepresentation: – Raised when the representation does not validate according to the resource expected representation.
- NotImplementedError: – If the resource does not support the method.
-
validate_identifier
(identifier)[source]¶ Validates the identifier from a request. Any exception occurring in this method will be converted into a NotFound exception. Exceptions that belong to this distribution will be let through to produce the expected response.
Note: We use NotFound (404) because the URL is likely valid. If our identifiers are all integers, so that
/foo/1/
is a valid URL for identifier 1, and the following url
/foo/whatever/
Is simply not present.
This method is always called before being dispatched to the CRUD methods accepting an identifier. By default, it does nothing, accepts any identifier, and returns the same identifier.
The method can also be used to modify the incoming identifier so that it’s compliant with the expectations, or return a new identifier.
Returns: Return type: The identifier that will be used.
-
validate_representation
(representation)[source]¶ Validates the representation incoming from a request, after it has been decoded. Any generic exception occurring in this method will be converted into a BadRepresentation exception. Exceptions that belong to this distribution will be let through to produce the expected response.
This method is always called before being dispatched to the CRUD methods accepting a representation. By default, it does nothing, accepts any representation, and returns the same representation.
The method can also be used to modify the incoming representation so that it’s compliant with the expectations, or return a new representation.
Returns: Return type: The representation that will be used.
-
tornadowebapi.utils module¶
Module contents¶
-
tornadowebapi.
api_handlers
(base_urlpath, version='v1')[source]¶ Returns the API handlers for the interface. Add these handlers to your application to provide an interface to your Resources.
Parameters: Notes
The current implementation does not support multiple API versions yet. The version option is only provided for futureproofing.