pythononwheels / pow_devel

development repo for PyhtonOnWheels framework
www.pythononwheels.org
MIT License
75 stars 10 forks source link

Add add_route decorator for methods #31

Closed pythononwheels closed 5 years ago

pythononwheels commented 6 years ago

Use the class decorator add_route style for methods as well. Of course the method name can be left out then. See: add_mroute(...) below... shold be called add_route as well in the real implementation.

sample:

@app.add_route('/handler1/classdec', dispatch={"get" : "new_test"})
class Handler1(PowHandler):
    #
    # on HTTP GET this method will be called. See dispatch parameter.
    #
    def _get_method(self, testval=0):
        """
            just a simple hanlder sceleton. Adapt to your needs
        """ 
        out_dict = {"testval" : str(testval)}
        self.success(message="I got testval:", data=out_dict, format="json", raw_data=True)
        #self.write("I got testval:" + str(testval))

    @add_mroute('/handler1/funcdec', dispatch=["get"])
    def new_test(self):
        """
            method to test a function decotrator
        """
pythononwheels commented 5 years ago

Done.

You can use method routes from PythonOnWheels: 0.9x. The current version 0.912 fully supports this.

Usage:

  1. Decorate the class using the @app.make_routes() decorator
  2. Decorate the methods using rhe @route decorator.

In contrast to the class based routes you can obviously omit the dispatch parameter which defines the method to call for this route. Since you attach the route to the method directly.

Both methods to define routes work equivalently and can be chosen by your choice. Choose whatver you prefer to be more handy..

Example:

@app.make_routes()  
class HelloHandler(BaseHandler):  
    @route('/hello', dispatch=["get"])  
    def hello(self):  
        self.write("Hello world!") 

More info can be found in the routing documentation