@@ -0,0 +1,2653 @@
+# -*- coding: utf-8 -*-%0A%22%22%22A simple number and datetime addition JSON API.%0ARun the app:%0A%0A    $ python examples/flaskrestful_example.py%0A%0ATry the following with httpie (a cURL-like utility, http://httpie.org):%0A%0A    $ pip install httpie%0A    $ http GET :5001/%0A    $ http GET :5001/ name==Ada%0A    $ http POST :5001/add x=40 y=2%0A    $ http POST :5001/dateadd value=1973-04-10 addend=63%0A    $ http POST :5001/dateadd value=2014-10-23 addend=525600 unit=minutes%0A%22%22%22%0Aimport datetime as dt%0A%0Afrom dateutil import parser as dateparser%0Afrom flask import Flask%0Afrom flask.ext import restful%0A%0Afrom webargs import Arg, ValidationError%0Afrom webargs.flaskparser import use_args, use_kwargs, parser%0A%0Aapp = Flask(__name__)%0Aapi = restful.Api(app)%0A%0A%0Aclass IndexResource(restful.Resource):%0A    %22%22%22A welcome page.%22%22%22%0A%0A    hello_args = %7B%0A        'name': Arg(str, default='Friend')%0A    %7D%0A%0A    @use_args(hello_args)%0A    def get(self, args):%0A        return %7B'message': 'Welcome, %7B%7D!'.format(args%5B'name'%5D)%7D%0A%0A%0Aclass AddResource(restful.Resource):%0A    %22%22%22An addition endpoint.%22%22%22%0A%0A    add_args = %7B%0A        'x': Arg(float, required=True),%0A        'y': Arg(float, required=True),%0A    %7D%0A%0A    @use_kwargs(add_args)%0A    def post(self, x, y):%0A        %22%22%22An addition endpoint.%22%22%22%0A        return %7B'result': x + y%7D%0A%0A%0Adef string_to_datetime(val):%0A    return dateparser.parse(val)%0A%0Adef validate_unit(val):%0A    if val not in %5B'minutes', 'days'%5D:%0A        raise ValidationError(%22Unit must be either 'minutes' or 'days'.%22)%0A%0Aclass DateAddResource(restful.Resource):%0A%0A    dateadd_args = %7B%0A        'value': Arg(default=dt.datetime.utcnow, use=string_to_datetime),%0A        'addend': Arg(int, required=True, validate=lambda val: val %3E= 0),%0A        'unit': Arg(str, validate=validate_unit)%0A    %7D%0A%0A    @use_kwargs(dateadd_args)%0A    def post(self, value, addend, unit):%0A        %22%22%22A datetime adder endpoint.%22%22%22%0A        if unit == 'minutes':%0A            delta = dt.timedelta(minutes=addend)%0A        else:%0A            delta = dt.timedelta(days=addend)%0A        result = value + delta%0A        return %7B'result': result.isoformat()%7D%0A%0A# This error handler is necessary for usage with Flask-RESTful%0A@parser.error_handler%0Adef handle_request_parsing_error(err):%0A    %22%22%22webargs error handler that uses Flask-RESTful's abort function to return%0A    a JSON error response to the client.%0A    %22%22%22%0A    code, msg = getattr(err, 'status_code', 400), getattr(err, 'message', 'Invalid Request')%0A    restful.abort(code, message=msg)%0A%0Aif __name__ == '__main__':%0A    api.add_resource(IndexResource, '/')%0A    api.add_resource(AddResource, '/add')%0A    api.add_resource(DateAddResource, '/dateadd')%0A    app.run(port=5001, debug=True)%0A

Add example usage with Flask-RESTFul