emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.06k stars 71 forks source link

Two service decorator for route #125

Closed jigarmistry closed 7 years ago

jigarmistry commented 8 years ago

Is it possible to decorate two services for one route ? Like this ..

@app.route("/test") @service.json @service.xml def hello():

If yes then how can I get xml from this ?

gi0baro commented 8 years ago

Hi @jigarmistry, no I'm sorry but this is not available in weppy. You should route two different method and write down something like this:

def build_data():
    return {'hello': 'world'}

@app.route('/data.xml')
@service.xml
def xml_data():
    return build_data()

@app.route('/data.json')
@service.json
def json_data():
    return build_data()

I know is not such a good option, but writing down a service decorator that is capable of splitting your route in two different things is out of plans. If you really need this I can convert this issue into a feature request, but will probably take long time (I'm focused on several other things for the next releases).

jigarmistry commented 8 years ago

Thanks for reply. I think this is a better way to decorate methods. Actually I don't need this feature at this time but it will be good option to have in weppy.

gi0baro commented 7 years ago

@jigarmistry after a while I'm for rejecting this.

I considered all the options and I'm not really satisfied with any design I tried. The best way to do this is the following syntax:

from weppy.tools import ServiceHandler

@app.route('/data.json', handlers=[ServiceHandler('json')])
@app.route('/data.xml', handlers=[ServiceHandler('xml')])
def build_data():
    return {'hello': 'world'}

I think this is ok. @jigarmistry comments?

gi0baro commented 7 years ago

I'm closing this due to documentation update (9601da1).