summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-19 22:00:28 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-19 22:03:37 +0200
commitdf0971d28c7e8251314efdb278c2a1a7009d30a8 (patch)
tree8fdae4cf4613702dcf6f9c7a9cedaa77636214de
parentnew middlewares: (diff)
downloadnet-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.pm34
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;