Skip Menu |

This queue is for tickets about the Catalyst-Plugin-SubRequest CPAN distribution.

Report information
The Basics
Id: 39486
Status: resolved
Priority: 0/
Queue: Catalyst-Plugin-SubRequest

People
Owner: bobtfish [...] bobtfish.net
Requestors: arthas [...] cpan.org
ESH [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Patch for C::P::SubRequest to return full response object
Attached is a patch (against 0.13, with tests and docs) which extends your Catalyst::Plugin::SubRequest to provide the additional method: my $res = $c->sub_request_response(); (aliased as subreq_res, names courtesy of Aristotle Pagaltzis) in order to obtain the full Catalyst::Response object as return value nstead of just its body. There are also some minor documentation improvements.
Subject: SubRequest_response.patch
diff -x .svn -ur SRold/lib/Catalyst/Plugin/SubRequest.pm SRnew/lib/Catalyst/Plugin/SubRequest.pm --- SRold/lib/Catalyst/Plugin/SubRequest.pm 2008-06-26 09:07:57.000000000 +0200 +++ SRnew/lib/Catalyst/Plugin/SubRequest.pm 2008-06-26 18:58:49.000000000 +0200 @@ -13,16 +13,29 @@ use Catalyst 'SubRequest'; - $c->subreq('/test/foo/bar', { template => 'magic.tt' }); + my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' }); - $c->subreq( { path => '/test/foo/bar', - body => $body }, - { template => 'magic.tt' }); + my $res_body = $c->subreq( { + path => '/test/foo/bar', + body => $body + }, { + template => 'magic.tt' + }); + + # Get the full response object + my $res = $c->subreq_res('/test/foo/bar', { + template => 'mailz.tt' + }, { + param1 => 23 + }); + $c->log->warn( $res->content_type ); =head1 DESCRIPTION Make subrequests to actions in Catalyst. Uses the catalyst dispatcher, so it will work like an external url call. +Methods are provided both to get the body of the response and the full +response (L<Catalyst::Response>) object. =head1 METHODS @@ -33,16 +46,32 @@ =item sub_request Takes a full path to a path you'd like to dispatch to. -If the path is passed as a hash ref then it can include body, action, match and path. -Any additional parameters are put into the stash. +If the path is passed as a hash ref then it can include body, action, +match and path. +An optional second argument as hashref can contain data to put into the +stash of the subrequest. +An optional third argument as hashref can contain data to pass as +parameters to the subrequest. +Returns the body of the response. + +=item subreq_res [path as string or hash ref], [stash as hash ref], [parameters as hash ref] + +=item sub_request_response + +Like C<sub_request()>, but returns a full L<Catalyst::Response> object. =back =cut *subreq = \&sub_request; +*subreq_res = \&sub_request_response; sub sub_request { + return shift->sub_request_response( @_ )->body ; +} + +sub sub_request_response { my ( $c, $path, $stash, $params ) = @_; $path =~ s#^/##; @@ -91,7 +120,7 @@ $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug); - return $inner_ctx->response->body; + return $inner_ctx->response; } =head1 SEE ALSO diff -x .svn -ur SRold/t/02subreq.t SRnew/t/02subreq.t --- SRold/t/02subreq.t 2008-06-26 09:07:57.000000000 +0200 +++ SRnew/t/02subreq.t 2008-06-26 18:50:12.000000000 +0200 @@ -1,6 +1,6 @@ package main; -use Test::More tests => 9; +use Test::More tests => 12; use lib 't/lib'; use Catalyst::Test 'TestApp'; use File::stat; @@ -25,3 +25,10 @@ is( $response->code, 200, 'OK status code' ); is( $response->content, '1abc3', 'Normal request content' ); } + +{ + ok( my $response = request('/subtest_full_response'), 'Sub Reuqest returning full response object' ); + is( $response->code, 200, 'OK status code' ); + is( $response->content, '1text/csv3', 'Normal request content', ); +} + diff -x .svn -ur SRold/t/lib/TestApp.pm SRnew/t/lib/TestApp.pm --- SRold/t/lib/TestApp.pm 2008-06-26 09:07:57.000000000 +0200 +++ SRnew/t/lib/TestApp.pm 2008-06-26 18:48:08.000000000 +0200 @@ -19,12 +19,12 @@ $c->subreq('/normal/4'); $c->res->body($subreq); } - - sub normal : Global { + + sub normal : Global { my ( $self, $c, $arg ) = @_; $c->res->body($c->res->body().$arg); } - + sub subtest_params : Global { my ( $self, $c ) = @_; my $before = $c->req->params->{value}; @@ -33,6 +33,18 @@ $c->res->body($c->res->body().$after); } + sub subtest_full_response : Global { + my ( $self, $c ) = @_; + my $subreq_res = $c->subreq_res('/typesetter'); + $c->res->body( $c->res->body() . $subreq_res->content_type ); + } + + sub typesetter : Global { + my ( $self, $c, $arg ) = @_; + $c->res->content_type( 'text/csv' ); + $c->res->body($c->res->body()); + } + sub end : Private { my ( $self, $c ) = @_; $c->res->body($c->res->body().'3');
On Sun Sep 21 07:41:36 2008, ARTHAS wrote: Show quoted text
> Attached is a patch (against 0.13, with tests and docs) which extends your > Catalyst::Plugin::SubRequest to provide the additional method: > > my $res = $c->sub_request_response(); > > (aliased as subreq_res, names courtesy of Aristotle Pagaltzis) > in order to obtain the full Catalyst::Response object as return value > nstead of just its body. > > There are also some minor documentation improvements.
Please include that patch. I really need access to the response header! regards, moritz
Sure! I'll try to get to this tonight.. If you're using this a lot - please come drop by #catalyst-dev and grab a commit bit :)
Applied with slight modifications to match the current state of things: http://dev.catalystframework.org/svnweb/Catalyst/revision?rev=12764 Shipped as 0.16 to CPAN.