summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-05-11 09:24:04 +0200
committerfranck cuny <franck@lumberjaph.net>2010-05-11 09:24:04 +0200
commit60cc0758cb442f482f3ebb3f6fa4e8e8a98a2297 (patch)
tree82a80439a001460a209f95072b9e51a1eb96d950
parentupdate bucket properties, method to get an object (diff)
downloadanyevent-riak-60cc0758cb442f482f3ebb3f6fa4e8e8a98a2297.tar.gz
fetch object content
-rw-r--r--lib/AnyEvent/Riak/Object.pm40
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/AnyEvent/Riak/Object.pm b/lib/AnyEvent/Riak/Object.pm
index f756af4..d106254 100644
--- a/lib/AnyEvent/Riak/Object.pm
+++ b/lib/AnyEvent/Riak/Object.pm
@@ -1,18 +1,50 @@
 package AnyEvent::Riak::Object;
 
 use Moose;
+use AnyEvent::HTTP;
+
+with qw/
+  AnyEvent::Riak::Role::Client
+  AnyEvent::Riak::Role::HTTPUtils
+  AnyEvent::Riak::Role::CVCB
+  /;
 
-has _client => (is => 'rw', isa => 'AnyEvent::Riak', requid => 1);
 has key     => (is => 'rw', isa => 'Str');
-has content => (is => 'rw', isa => 'HashRef');
+has _content => (is => 'rw', isa => 'HashRef', predicate => '_has_content');
 has content_type => (is => 'rw', isa => 'Str', default => 'application/json');
 has bucket => (is => 'rw', isa => 'AnyEvent::Riak::Bucket', required => 1);
 has status => (is => 'rw', isa => 'Int');
 has r      => (is => 'rw', isa => 'Int');
 
 sub get {
-    my ($self) = @_;
-    $self->_client->http_get($self->bucket_name, $self->key, $self->r);
+    my ($self, %options) = @_;
+
+    my ($cv, $cb) = $self->cvcb(\%options);
+
+    if ($self->_has_content) {
+        $cv->send($self->_content);
+    }
+    else {
+        http_request(
+            GET => $self->_build_uri(
+                [$self->_client->path, $self->bucket->name, $self->key],
+                $options{params}
+            ),
+            headers => $self->_build_headers($options{params}),
+            sub {
+                my ($body, $headers) = @_;
+                if ($body && $headers->{Status} == 200) {
+                    my $content = JSON::decode_json($body);
+                    $self->_content($content);
+                    $cv->send($cb->($self->_content));
+                }
+                else {
+                    $cv->send(undef);
+                }
+            }
+        );
+    }
+    return $cv;
 }
 
 no Moose;