falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.52k stars 944 forks source link

Add support for query string parameters in route templates #838

Closed kgriffs closed 7 months ago

kgriffs commented 8 years ago

We can borrow from RFC 6570 syntax and do something like this:

'/things{?count,detailed}'

Note that all params would default to None if present. I'm assuming we would just pass these as kwargs to the responder method.

Filters could be specified by extending RFC 6570, i.e:

'/things{?count:int,detailed:bool}'
softgripper commented 7 years ago

This is EXACTLY what I'm after.

I'm basically making my own server that wraps an external API, but does some processing on some of the functions, and is accessed by ajax so need to avoid CORS and my own caching.

I've added a route decorator and basically do this.

@route('/market/{instrument}/{currency}/trades?since={timestamp}')
class Trades(RoutedHandler):
     def __init__(self):
         super().__init__()

Of course the 'timestamp' param in the above example does not get passed as **kwargs, but for all simple "/" param based API calls, it's saved me a LOT of time and effort, and is quite elegant.

Then in the super I do

    def on_get(self, req, resp, **kwargs):
        url = config.domain + self.get_route(**kwargs)
        if req.params:
            url += "?{}".format(req.query_string)

        result = requests.get(url)
        resp.body = result.text

I wish I knew how to work around this, but I've only been using Falcon since yesterday. 👍 and it would be nice if the querystring arguments could be used for routing.

vytas7 commented 7 months ago

Given this has generated only a limited interest from the community, I think we want to keep the default router focused solely on routing based on req.path. However, I filed a separate issue to be able to easier extend the default router with additions like this one: #2219.