Search code examples
google-app-engineurlwebapp2

Is it equivalent to put something as part of the path and as a parameter?


Right now I have two ways of accomplishing (apparently) the same thing in my application. I can either make my urls look like this https://something/product/shirt or https://something/product?q=shirt. In both cases I can extract what I need from it, which is shirt.

First way (with a regular expression):

class FirstHandler(BaseHandler):
    def get(self, page_id):
        target = page_id

PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([('/something' + PAGE_RE, FirstHandler)],
                               debug=True)

The second way in which I can deal with it is using a parameter, which would look like this:

class SecondHandler(BaseHandler):
    def get(self):
        target = self.request.get('q')

app = webapp2.WSGIApplication([('/something' SecondHandler)],
                               debug=True)

My question is, are these methods equivalent? Is it the same if I do one or the other or do I have to take something else into consideration?


Solution

  • Look at all of the requests that you expect to make, and decide which of them should be represented by unique endpoints (/shirt), and which will allow one or more parameters. You can combine these approaches when necessary.

    There is no performance penalty or any other advantage of using url path or parameters. The key factors are:

    • how easy it is to understand your API if it is or will be used by outside developers
    • how easy it is to organize and maintain your code
    • how flexible is your approach when you need to expand it

    For example, /shirt may look like a good idea now, but once you have thousands of product types, it becomes a nightmare to maintain. Instead, you may want to use something like:

    /product/?type=shirt&size=10&orderBy=price&results=20&offset=40