Search code examples
pythongoogle-app-enginewebapp2

In webapp2, how can I get a list of all route URIs?


I have a typical WSGI application like so:

app = webapp2.WSGIApplication([
    ('/site/path/one',     'package.module.ClassOne'),
    ('/site/path/two',     'package.module.ClassTwo'),
    ('/site/path/three',   'package.module.ClassThree'),
])

I would like to be able to retrieve the list of routes later in my application, say for example, in ClassOne:

class ClassOne(webapp2.RequestHandler):
    def get(self):
        print ''' list of routes (e.g. '/site/path/one', '/site/path/two', etc.) '''

Solution

  • It looks like WSGIApplication has a router property

    Router has

    def __repr__(self):
            routes = self.match_routes + [v for k, v in \
                self.build_routes.iteritems() if v not in self.match_routes]
    
            return '<Router(%r)>' % routes
    

    Your wsgiApplication instance is a property of your webapp2.RequestHandler

    so I think request.app.router

    So hopefully there is a more straighforward way but if not the above should worK?

    Webapp2 had really great searchable source code available at http://webapp2.readthedocs.io/en/latest/