flask-ripozo API

class flask_ripozo.dispatcher.FlaskDispatcher(app, url_prefix=u'', error_handler=<function exception_handler>, argument_getter=<function get_request_query_body_args>, **kwargs)

Bases: ripozo.dispatch_base.DispatcherBase

This is the actual dispatcher responsible for integrating ripozo with flask. Pretty simple right?

__init__(app, url_prefix=u'', error_handler=<function exception_handler>, argument_getter=<function get_request_query_body_args>, **kwargs)

Initialize the adapter. The app can actually be either a flask.Flask instance or a flask.Blueprint instance.

Parameters:
  • app (flask.Flask) – The flask app that is responsible for handling the web application.
  • url_prefix (unicode) – The url prefix will be prepended to every route that is registered on this dispatcher. It is helpful if, for example, you want to expose your api on the ‘/api’ path.
  • error_handler (function) – A function that takes a dispatcher, accepted_mimetypes, and exception that handles error responses. It should return a flask.Response instance.
  • argument_getter (function) – The function responsible for getting the query/body arguments from the Flask Request as a tuple. This function should return (dict, dict,) with the first as the query args and the second as the request body args.
base_url
Returns:The base_url for this adapter. It simply joins the provided base_url in the __init__ method and joins it with the request.url_root. If this app provided is actually a blueprint, it will return join the blueprints url_prefix in between
Return type:unicode
register_route(endpoint, endpoint_func=None, route=None, methods=None, **options)

Registers the endpoints on the flask application or blueprint. It does so by using the add_url_rule on the blueprint/app. It wraps the endpoint_func with the flask_dispatch_wrapper which returns an updated function. This function appropriately sets the RequestContainer object before passing it to the apimethod.

Parameters:
  • endpoint (unicode) – The name of the endpoint. This is typically used in flask for reversing urls
  • endpoint_func (method) – The actual function that is going to be called. This is generally going to be a @apimethod decorated, ResourceBase subclass method.
  • route (unicode) – The actual route that is going to be used.
  • methods (list) – The http verbs that can be used with this endpoint
  • options (dict) – The additional options to pass to the add_url_rule
flask_ripozo.dispatcher.exception_handler(dispatcher, accepted_mimetypes, exc)

Responsible for handling exceptions in the project. This catches any RestException (from ripozo.exceptions) and calls the format_exception class method on the adapter class. It will appropriately set the status_code, response, and content type for the exception.

Parameters:
  • dispatcher (FlaskDispatcher) – A FlaskDispatcher instance used to format the exception
  • accepted_mimetypes (list) – A list of the accepted mimetypes for the client.
  • exc (Exception) – The exception that was raised.
Returns:

A flask Response object.

Return type:

flask.Response

flask_ripozo.dispatcher.flask_dispatch_wrapper(dispatcher, f, argument_getter=<function get_request_query_body_args>)

A decorator for wrapping the apimethods provided to the dispatcher. The actual wrapper performs that actual construction of the RequestContainer, determines the appropriate adapter to use, dispatches the method, and passes errors to the dispatcher.error_handler method. Finally, it will return a flask.Response instance except in the case of an exception raised by the dispatch.error_handler (typically error_handlers only handle certain sets of exceptions).

Parameters:
  • dispatcher (FlaskDispatcher) – The dispatcher that is created this.
  • f (function) – The apimethod to wrap.
  • argument_getter (function) – The function that takes a flask Request object and uses it to get the query arguments and the body arguments as a tuple.
flask_ripozo.dispatcher.get_request_query_body_args(request_obj)

Gets the request query args and the body arguments. It gets the query_args from the flask request.args and transforms it from an ImmutableMultiDict to a dict. It attempts to retrieve json for the body first. If it doesn’t find any it looks at the form otherwise it returns an empty dictionary. The body is also transformed from an ImmutableMultiDict to a builtin dict.

Parameters:request_obj (flask.Request) – A Flask request object.
Returns:A tuple of the appropriately formatted query args and body args
Return type:(dict, dict)