librelist archives

« back to archive

Getting the URL template when servicing a request

Getting the URL template when servicing a request

From:
Andrew Stuart
Date:
2015-07-28 @ 20:46
When a request comes in, I need the original URL template that it matched.

It appears that this is discarded when the routes are added.

Any ideas on how I could get it when I get a request?

thanks

Re: [falcon] Getting the URL template when servicing a request

From:
Michał Jaworski
Date:
2015-07-28 @ 23:03
Andrew, I had the same issue when I wanted to create auto documenting
module for my "graceful" project. I think the easiest solution is to create
some custom router that can mokey patch your resource instances on
add_route call. See following example:

from falcon import routing

class CustomRouter(routing.DefaultRouter):
    def add_route(self, uri_template, method_map, resource):
        resource.uri_template = uri_template

        return super(CustomRouter, self).add_route(
            uri_template, method_map, resource
        )

api = application = falcon.API(router=CustomRouter())

Then this route will be available during any resource method handler call.
This is not ideal because in falcon the single resource instance can be
mapped to multiple routes but should work in most cases. Also note that
custom router is a feature introduced in falcon 0.3.0 and this will not
work in falcon<0.3.0.

2015-07-28 22:46 GMT+02:00 Andrew Stuart <andrew.stuart@supercoders.com.au>:

> When a request comes in, I need the original URL template that it matched.
>
> It appears that this is discarded when the routes are added.
>
> Any ideas on how I could get it when I get a request?
>
> thanks
>
>
>

Re: [falcon] Getting the URL template when servicing a request

From:
Andrew Stuart
Date:
2015-07-28 @ 23:12
Thanks - given that I’m not the only one with the requirement maybe there 
is value in Falcon saving the URI template and providing it with the 
request.



On 29 Jul 2015, at 9:03 am, Michał Jaworski <swistakm@gmail.com> wrote:

Andrew, I had the same issue when I wanted to create auto documenting 
module for my "graceful" project. I think the easiest solution is to 
create some custom router that can mokey patch your resource instances on 
add_route call. See following example:

from falcon import routing

class CustomRouter(routing.DefaultRouter):
    def add_route(self, uri_template, method_map, resource):
        resource.uri_template = uri_template

        return super(CustomRouter, self).add_route(
            uri_template, method_map, resource
        )

api = application = falcon.API(router=CustomRouter())

Then this route will be available during any resource method handler call.
This is not ideal because in falcon the single resource instance can be 
mapped to multiple routes but should work in most cases. Also note that 
custom router is a feature introduced in falcon 0.3.0 and this will not 
work in falcon<0.3.0.

2015-07-28 22:46 GMT+02:00 Andrew Stuart <andrew.stuart@supercoders.com.au>:
When a request comes in, I need the original URL template that it matched.

It appears that this is discarded when the routes are added.

Any ideas on how I could get it when I get a request?

thanks



Re: [falcon] Getting the URL template when servicing a request

From:
Kurt Griffiths
Date:
2015-08-03 @ 19:38
Indeed, I think there is value in implementing this directly in the 
framework. The URI template will need to be persisted in the routing 
table/tree and then the template will need to be set as an attribute on 
each Request instance. Would someone like to submit a GH issue and we can 
continue the discussion there?

-Kurt




On 7/28/15, 6:12 PM, "falcon@librelist.com on behalf of Andrew Stuart" 
<falcon@librelist.com on behalf of andrew.stuart@supercoders.com.au> 
wrote:

>Thanks - given that I’m not the only one with the requirement maybe there
is value in Falcon saving the URI template and providing it with the 
request.
>
>
>
>On 29 Jul 2015, at 9:03 am, Michał Jaworski <swistakm@gmail.com> wrote:
>
>Andrew, I had the same issue when I wanted to create auto documenting 
module for my "graceful" project. I think the easiest solution is to 
create some custom router that can mokey patch your resource instances on 
add_route call. See following example:
>
>from falcon import routing
>
>class CustomRouter(routing.DefaultRouter):
>    def add_route(self, uri_template, method_map, resource):
>        resource.uri_template = uri_template
>
>        return super(CustomRouter, self).add_route(
>            uri_template, method_map, resource
>        )
>
>api = application = falcon.API(router=CustomRouter())
>
>Then this route will be available during any resource method handler 
call. This is not ideal because in falcon the single resource instance can
be mapped to multiple routes but should work in most cases. Also note that
custom router is a feature introduced in falcon 0.3.0 and this will not 
work in falcon<0.3.0.
>
>2015-07-28 22:46 GMT+02:00 Andrew Stuart <andrew.stuart@supercoders.com.au>:
>When a request comes in, I need the original URL template that it matched.
>
>It appears that this is discarded when the routes are added.
>
>Any ideas on how I could get it when I get a request?
>
>thanks
>
>
>
>

Re: [falcon] Getting the URL template when servicing a request

From:
Kurt Griffiths
Date:
2015-09-24 @ 18:45
I went ahead and created an issue to follow up on this. Suggestions are welcome!

https://github.com/falconry/falcon/issues/619


-Kurt



On 8/3/15, 2:38 PM, "falcon@librelist.com on behalf of Kurt Griffiths" 
<falcon@librelist.com on behalf of kurt.griffiths@rackspace.com> wrote:

>Indeed, I think there is value in implementing this directly in the 
framework. The URI template will need to be persisted in the routing 
table/tree and then the template will need to be set as an attribute on 
each Request instance. Would someone like to submit a GH issue and we can 
continue the discussion there?
>
>-Kurt
>
>
>
>
>On 7/28/15, 6:12 PM, "falcon@librelist.com on behalf of Andrew Stuart" 
<falcon@librelist.com on behalf of andrew.stuart@supercoders.com.au> 
wrote:
>
>>Thanks - given that I’m not the only one with the requirement maybe 
there is value in Falcon saving the URI template and providing it with the
request.
>>
>>
>>
>>On 29 Jul 2015, at 9:03 am, Michał Jaworski <swistakm@gmail.com> wrote:
>>
>>Andrew, I had the same issue when I wanted to create auto documenting 
module for my "graceful" project. I think the easiest solution is to 
create some custom router that can mokey patch your resource instances on 
add_route call. See following example:
>>
>>from falcon import routing
>>
>>class CustomRouter(routing.DefaultRouter):
>>    def add_route(self, uri_template, method_map, resource):
>>        resource.uri_template = uri_template
>>
>>        return super(CustomRouter, self).add_route(
>>            uri_template, method_map, resource
>>        )
>>
>>api = application = falcon.API(router=CustomRouter())
>>
>>Then this route will be available during any resource method handler 
call. This is not ideal because in falcon the single resource instance can
be mapped to multiple routes but should work in most cases. Also note that
custom router is a feature introduced in falcon 0.3.0 and this will not 
work in falcon<0.3.0.
>>
>>2015-07-28 22:46 GMT+02:00 Andrew Stuart <andrew.stuart@supercoders.com.au>:
>>When a request comes in, I need the original URL template that it matched.
>>
>>It appears that this is discarded when the routes are added.
>>
>>Any ideas on how I could get it when I get a request?
>>
>>thanks
>>
>>
>>
>>

Re: [falcon] Getting the URL template when servicing a request

From:
Andrew Stuart
Date:
2015-07-28 @ 23:36
It appears that method_map gets returned by the find method of the router.

Maybe when adding a route, just add the template in to the method_map 
dictionary and then it will be available.

It’s a bit hacky but might work.




On 29 Jul 2015, at 9:03 am, Michał Jaworski <swistakm@gmail.com> wrote:

Andrew, I had the same issue when I wanted to create auto documenting 
module for my "graceful" project. I think the easiest solution is to 
create some custom router that can mokey patch your resource instances on 
add_route call. See following example:

from falcon import routing

class CustomRouter(routing.DefaultRouter):
    def add_route(self, uri_template, method_map, resource):
        resource.uri_template = uri_template

        return super(CustomRouter, self).add_route(
            uri_template, method_map, resource
        )

api = application = falcon.API(router=CustomRouter())

Then this route will be available during any resource method handler call.
This is not ideal because in falcon the single resource instance can be 
mapped to multiple routes but should work in most cases. Also note that 
custom router is a feature introduced in falcon 0.3.0 and this will not 
work in falcon<0.3.0.

2015-07-28 22:46 GMT+02:00 Andrew Stuart <andrew.stuart@supercoders.com.au>:
When a request comes in, I need the original URL template that it matched.

It appears that this is discarded when the routes are added.

Any ideas on how I could get it when I get a request?

thanks