On Nov 23, 12:06 pm, Ross Vandegrift <[email protected]> wrote:
> On Mon, Nov 23, 2009 at 11:59:03AM -0800, Mike Orr wrote:
> > The answer is to use a generic 'id' variable in the route, and to
> > rename the variable in the action. This is the philosophy behind the
> > default "/{controller}/{action}/{id}" route.
>
> > map.connect("/api/{action}/{id}", controller="api")
>
> I thought about doing something like this, but this will break
> documentation generation: the parameters will all be generated in the
> docs as "id". While I might be able to get over that, I have to
> publish this API to programmers that will (fairly) get confused when
> they see a funtion that takes an id, but the documentation claims it's
> something like a name.
>
> I guess I may have to live with a bunch of routes. That's not really
> the end of the world, since I can write things like:
>
> map.connect(r"/api/{action:get.*byid}/{id:[0-9]+}", controller="api")
> map.connect(r"/api/{action:get.*byacct}/{acct:[0-9]+}", controller="api")
> map.connect(r"/api/{action:get.*bystr}/{searchstr}", controller="api")
This is only tangentially related to your question, but if you are
going for a REST-esque API, I would design the URLs more like this:
1) /api/{resource_name}/{id}
2) /api/{resource_name}/{access_type}/{id}
/api/{resource_name}/{access_type}/{name}
The first type of URL would use the default ID type, perhaps the
numeric ID. The second type of URL would be something like /api/pants/
by_name/{name}. In either case, you'd route to an action based on the
HTTP verb. All of the by_* stuff would really just be different ways
to get to your `show` action, which could have args like
`access_type='id', id=None, name=None`. Of course, your docstring
would have to note that only *one* ID parameter can be passed.
Personally, I would do what Mike O. suggested, perhaps changing the ID
name from `id` to something that's a bit less overloaded and that
indicates different types might be passed. I think even `identifier`
might be better, though it's essentially the same thing--I just think
`id` in particular is often (usually?) automatically translated to
"numeric ID" or "primary key".
Anyway, just a thought or two about API design. Feel free to
disregard. :)
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=.