summary refs log tree commit diff
path: root/lib/MooseX/UserAgent/Async.pm
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2009-06-25 11:01:13 +0200
committerfranck cuny <franck@lumberjaph.net>2009-06-25 11:01:13 +0200
commit9731c038acf73a68352fc622cf742b5940a517ef (patch)
treeba6a0b2b53bbf84e0722444f0a68fdcba17ebfdc /lib/MooseX/UserAgent/Async.pm
parentbasic useragent (diff)
downloadmoosex-useragent-9731c038acf73a68352fc622cf742b5940a517ef.tar.gz
add dep
Diffstat (limited to 'lib/MooseX/UserAgent/Async.pm')
-rw-r--r--lib/MooseX/UserAgent/Async.pm34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/MooseX/UserAgent/Async.pm b/lib/MooseX/UserAgent/Async.pm
new file mode 100644
index 0000000..4adcf9a
--- /dev/null
+++ b/lib/MooseX/UserAgent/Async.pm
@@ -0,0 +1,34 @@
+package MooseX::UserAgent::Async;
+
+use Moose::Role;
+with qw/MooseX::UserAgent::Config MooseX::UserAgent::Content
+    MooseX::UserAgent::Cache/;
+
+use AnyEvent::HTTP;
+use HTTP::Response;
+
+sub fetch {
+    my ( $self, $url ) = @_;
+    my $status = AnyEvent->condvar;
+
+    my $last_modified = $self->get_ua_cache($url);
+
+    my $request_headers = { 'Accept-Encoding' => 'gzip', };
+    $request_headers->{'If-Modified-Since'} = $last_modified
+        if $last_modified;
+
+    http_request GET => $url, headers => $request_headers, sub {
+        my ( $data, $headers ) = @_;
+        my $response = HTTP::Response->new;
+        $response->content($data);
+        $response->code(delete $headers->{Status});
+        foreach my $header ( keys %$headers ) {
+            $response->header( $header => $headers->{$header} );
+        }
+        $self->store_ua_cache($url, $response);
+        $status->send($response);
+    };
+    return $status->recv;
+}
+
+1;