summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2009-12-13 17:23:54 +0100
committerfranck cuny <franck@lumberjaph.net>2009-12-13 17:23:54 +0100
commit44c205ea1550adfeda2b013ef3504b7d6f918f41 (patch)
tree541bbf922baa41af352182787470ebf7cc45f6e2
parentupdate POD (diff)
downloadanyevent-riak-44c205ea1550adfeda2b013ef3504b7d6f918f41.tar.gz
w r and dw can be set
-rw-r--r--lib/AnyEvent/Riak.pm40
-rw-r--r--t/basic.t12
2 files changed, 30 insertions, 22 deletions
diff --git a/lib/AnyEvent/Riak.pm b/lib/AnyEvent/Riak.pm
index 1812990..9292d4b 100644
--- a/lib/AnyEvent/Riak.pm
+++ b/lib/AnyEvent/Riak.pm
@@ -38,31 +38,41 @@ sub set_bucket {
         $schema->{write_mask} = $schema->{read_mask};
     }
 
-    $self->_request('PUT', $self->_build_uri([$bucket]), '204',
-        encode_json{schema => $schema});
+    $self->_request(
+        'PUT', $self->_build_uri( [$bucket] ),
+        '204', encode_json { schema => $schema }
+    );
 }
 
 sub list_bucket {
     my ( $self, $bucket ) = @_;
-    return $self->_request('GET', $self->_build_uri([$bucket]), '200');
+    return $self->_request( 'GET', $self->_build_uri( [$bucket] ), '200' );
 }
 
 sub fetch {
-    my ($self, $bucket, $key) = @_;
-    return $self->_request('GET', $self->_build_uri([$bucket, $key]), '200');
+    my ( $self, $bucket, $key, $r ) = @_;
+    $r = $self->{r} || 2 if !$r;
+    return $self->_request( 'GET',
+        $self->_build_uri( [ $bucket, $key ], { r =>  $r} ), '200' );
 }
 
 sub store {
-    my ( $self, $object ) = @_;
+    my ( $self, $object, $w, $dw, ) = @_;
+
+    $w  = $self->{w}  || 2 if !$w;
+    $dw = $self->{dw} || 2 if !$dw;
 
     my $bucket = $object->{bucket};
     my $key    = $object->{key};
+    $object->{links} = [] if !exists $object->{links};
+
     return $self->_request(
         'PUT',
         $self->_build_uri(
             [ $bucket, $key ],
             {
-                dw         => 2,
+                w          => $w,
+                dw         => $dw,
                 returnbody => 'true'
             }
         ),
@@ -70,18 +80,12 @@ sub store {
         encode_json $object);
 }
 
-sub fetch {
-    my ( $self, $bucket, $key, ) = @_;
-
-    return $self->_request( 'GET',
-        $self->_build_uri( [ $bucket, $key ], { r => 2 } ), '200' );
-}
-
 sub delete {
-    my ( $self, $bucket, $key ) = @_;
+    my ( $self, $bucket, $key, $rw ) = @_;
 
+    $rw = $self->{rw} || 2 if !$rw;
     return $self->_request( 'DELETE',
-        $self->_build_uri( [ $bucket, $key ], { dw => 2 } ), 204 );
+        $self->_build_uri( [ $bucket, $key ], { dw => $rw } ), 204 );
 }
 
 sub _build_uri {
@@ -103,8 +107,8 @@ sub _request {
                 : return $cv->send(1);
         }
         else {
-            return $cv->send(
-                $headers->{Status} . ' : ' . $headers->{Reason} );
+            return $cv->croak(
+                encode_json( [ $headers->{Status}, $headers->{Reason} ] ) );
         }
     };
     if ($body) {
diff --git a/t/basic.t b/t/basic.t
index 29fed23..cb011f7 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -1,6 +1,7 @@
 use strict;
 use warnings;
 use Test::More;
+use JSON::XS;
 use Test::Exception;
 use AnyEvent::Riak;
 
@@ -18,14 +19,17 @@ ok my $new_bucket
 
 my $value = {
     bucket => 'foo',
-    key    => 'baz',
-    object => { foo => "bar" },
+    key    => 'bar',
+    object => { foo => "bar", baz => 1 },
     links  => []
 };
 
 ok my $res = $jiak->store($value)->recv, '... set a new key';
 
-ok $res = $jiak->fetch( 'foo', 'baz' )->recv, '... fetch our new key';
-ok $res = $jiak->delete( 'foo', 'baz' )->recv, '... delete our key';
+ok $res = $jiak->fetch( 'foo', 'bar' )->recv, '... fetch our new key';
+ok $res = $jiak->delete( 'foo', 'bar' )->recv, '... delete our key';
+
+dies_ok { $jiak->fetch( 'foo', 'foo' )->recv } '... dies when error';
+like $@, qr/404/, '... 404 response';
 
 done_testing();