Skip to main content

Improve Flask-RESTFul's behavior. Add some new features.

Project description

Translate the document into English was a bit difficult, I have tried.

Flask-RESTFul-extend

Improve Flask-RESTFul’s behavior. Add some new features.

All features that shown below was optional, choose according to your needs.

Improve error handling

source: error_handling.py

Flask-RESTFul’s error handler can only output error message for the exceptions that raised by itself.

This make it can handling other kind of exceptions in same way.

api = restful_extend.ErrorHandledApi(app)   # instead of `api = restful.Api(app)`

class MyRoutes(Resource):
    def get(self):
        raise Exception("errmsg")   # now, the 'errmsg' can output to client

Improve JSON support

enhance JSON encode ability

sources: extend_json.py, json_encode_manager.py

Support more data type by default, and you can simply add supports for new data type.

# This is a custom type, you can't direct return a value of this type in Flask or Flask-RESTFul.
class RationalNumber(object):
    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator

api = restful.Api(app)

# Enable enhanced json encode feature
enhance_json_encode(api)


# create and register a custom encoder, to encode your custom type to serializable value.
def rational_encoder(rational):
    return rational.numerator * 1.0 / rational.denominator

api.json_encoder.register(rational_encoder, RationalNumber)


class MyRoutes(Resource):
    def get(self):
        return RationalNumber(1, 5)     # now you can return the value of your custom type directly

support JSONP

source: extend_json.py

Respond jsonp request automatically

api = restful.Api(app)
support_jsonp(api)

class MyRoutes(Resource):
    def get(self):
        return dict(foo='bar')

api.add_resource(MyRoutes, '/my_routes')

# normal request: /my_routes                    response: {"foo": "bar"}
# jsonp request:  /my_routes?callback=my_cb     response: my_cb({"foo": "bar"})

More Details

For more details, please read the documents in source.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-RESTful-extend-0.3.3.tar.gz (13.6 kB view hashes)

Uploaded Source

Built Distributions

Flask_RESTful_extend-0.3.3-py3-none-any.whl (16.0 kB view hashes)

Uploaded Python 3

Flask_RESTful_extend-0.3.3-py2-none-any.whl (16.0 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page