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
Built Distribution
Hashes for Flask_RESTful_extend-0.3.1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 436f474da6cf2c974dbf3eb4774022de2795dc1cf548b72d4809cd6ca7e85626 |
|
MD5 | 1f8ace7ac77a1b94d2465493dd4965f7 |
|
BLAKE2b-256 | b5debfd26d85941984408312bb92c6e59dc8039846eac4d3160098f5ef153c3e |