diff options
author | franck cuny <franck@lumberjaph.net> | 2010-10-19 22:00:28 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-10-19 22:03:37 +0200 |
commit | df0971d28c7e8251314efdb278c2a1a7009d30a8 (patch) | |
tree | 8fdae4cf4613702dcf6f9c7a9cedaa77636214de | |
parent | new middlewares: (diff) | |
download | net-http-spore-df0971d28c7e8251314efdb278c2a1a7009d30a8.tar.gz |
small updates on request:
* abstract method to execute middlewares on response * on response callback, middlewares can return a Net::HTTP::Spore::Response object * method _request
-rw-r--r-- | lib/Net/HTTP/Spore/Role/Request.pm | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/lib/Net/HTTP/Spore/Role/Request.pm b/lib/Net/HTTP/Spore/Role/Request.pm index ddd52e6..d22a721 100644 --- a/lib/Net/HTTP/Spore/Role/Request.pm +++ b/lib/Net/HTTP/Spore/Role/Request.pm @@ -33,22 +33,40 @@ sub http_request { } } - if (defined $response) { - map { $_->($response) } reverse @middlewares; - return $response; + return + $self->_execute_middlewares_on_response( $response, @middlewares ) + if defined $response; + + $response = $self->_request($request); + + return $self->_execute_middlewares_on_response( $response, @middlewares ); +} + +sub _execute_middlewares_on_response { + my ($self, $response, @middlewares) = @_; + + foreach my $mw ( reverse @middlewares ) { + my $res = $mw->($response); + $response = $res + if ( defined $res + && Scalar::Util::blessed($res) + && $res->isa('Net::HTTP::Spore::Response') ); } + $response; +} + +sub _request { + my ($self, $request) = @_; + my $result = $self->request($request->finalize); - $response = $request->new_response( + my $response = $request->new_response( $result->code, $result->headers, $result->content, ); - - map { $_->($response) } reverse @middlewares; - - $response; + return $response; } 1; |