Permalink
Browse files

w r and dw can be set

  • Loading branch information...
fcuny committed Dec 13, 2009
1 parent 68a610f commit 44c205ea1550adfeda2b013ef3504b7d6f918f41
Showing with 30 additions and 22 deletions.
  1. +22 −18 lib/AnyEvent/Riak.pm
  2. +8 −4 t/basic.t
View
@@ -38,50 +38,54 @@ 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'
}
),
'200',
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) {
View
@@ -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();

0 comments on commit 44c205e

Please sign in to comment.