[Catalyst] Mapping parameters for web services
Bill Moseley
moseley at hank.org
Wed Oct 24 18:15:20 GMT 2007
On Tue, Oct 23, 2007 at 09:33:13PM +0100, Matt S Trout wrote:
>
> I'm not sure I understand; the controller should really just be doing stuff
> that's specific to the app UI, so surely you should be mapping both your
> existing controllers' input params and te XMLRPC params onto calls to the
> same model?
Sorry, maybe I wasn't clear. It's not a pressing problem (I can work
it out) but more curious about how best to do this in a catalyst way.
The question was about sharing the *same* controller for different types
of requests.
The C::P::Server::XMLRPC plugin allows adding attributes to existing
controllers, so you can do:
sub user_detail : Local : XMLRPC {
my ( $self, $c, $user_id ) = @_;
# validate user_id and place user object in stash.
}
But in the XMLRPC request the user_id might not be passed as an
argument, but rather as $c->req->params->{user_id}.
One option might be:
sub user_detail : Local : XMLRPC {
my ( $self, $c, $user_id ) = @_;
$user_id = $c->req->params->{user_id}
if $c->req->xmlrpc->is_xmlrpc_request;
# validate user_id and place user object in stash.
}
But, I'd rather have a layer of abstraction that does that parameter
mapping somewhere else -- so that $user_id is just passed as an argument to
the above controller. Otherwise, when I later add SOAP or some other
request method I'll need to edit the controllers again.
The question is where to put that mapping?
The existing component system is handy. I could, for example I could
probably do:
sub auto : Private {
my ( $self, $c ) = @_;
$c->forward( $c->view( 'Request::XMLRPC' ) )
if $c->req->xmlrpc->is_xmlrpc_request;
1;
}
and have its process() dispatch to other methods based on
$c->action->reverse.
Or maybe the XMLRPC plugin could do:
$c->map_paramters if $c->can('map_parameters');
after $c->action and the input arguments are set. Then add a
map_parameters() method to my application to again look at $c->action
and alter the parameters.
Or override dispatch() or prepare_action() and alter the params.
Which approach would you use?
--
Bill Moseley
moseley at hank.org
More information about the Catalyst
mailing list