I'm adding API functionality to an exiting app whose controller
actions aren't close to being in shape for direct exposure. This
pretty much precludes me from turning them into REST controllers and
accomplishing this task that way.
So I've created a new api controller that will house the actions for
programmatic access. This has an unfortunate side effect of making my
routing kind of difficult.
For example, suppose I have model objects X and Y and the following API:
class ApiController(BaseController):
@jsonify
def getXbyid(self, id):
q = meta.session.query(model.X)
result = q.get(id)
return {"result": result.__json__()}
@jsonify
def getXbyacct(self, acct):
q = meta.session.query(model.X)
result = q.filter_by(acct=acct).one()
return {"result": result.__json__()}
@jsonify
def getYbyname(self, name):
q = meta.session.query(model.Y)
result = q.filter_by(name=name).one()
return {"result": result.__json__()}
How can I effectively map these actions without listing each action in
my routes? The best I have come up with is to include a route for
each parameter name I use:
map.connect("/api/{action}/{id:[0-9]+}", controller="api")
map.connect("/api/{action}/{acct:[0-9]+}", controller="api")
map.connect("/api/{action}/{name:[a-zA-Z0-9+}", controller="api")
--
Ross Vandegrift
ro...@kallisti.us
"If the fight gets hot, the songs get hotter. If the going gets tough,
the songs get tougher."
--Woody Guthrie
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")
Ross