Permalink
Browse files

add store, fetch, still missing delete

  • Loading branch information...
1 parent 0ed18bf commit 4af8109dc5f298b65ea8fcf95cb961c7ca8099ae @franckcuny franckcuny committed Apr 10, 2010
Showing with 57 additions and 38 deletions.
  1. +57 −38 lib/AnyEvent/Riak.pm
View
95 lib/AnyEvent/Riak.pm
@@ -95,6 +95,8 @@ sub default_cb {
else {
return $_[0];
}
+ }else{
+ return 1;
}
};
}
@@ -134,53 +136,70 @@ sub list_bucket {
}
sub set_bucket {
- my ( $self, $bucket, $schema ) = @_;
+ my $self = shift;
+ my $bucket = shift;
+ my $schema = shift;
- carp "your schema is missing allowed_fields"
- if ( !exists $schema->{allowed_fields} );
+ my ( $cv, $cb ) = $self->_init_callback(@_);
+ $cb = $self->default_cb( { json => 1 } ) if !$cb;
- if ( !exists $schema->{required_fields} ) {
- $schema->{required_fields} = [];
- }
- if ( !exists $schema->{read_mask} ) {
- $schema->{read_mask} = $schema->{allowed_fields};
- }
- if ( !exists $schema->{write_mask} ) {
- $schema->{write_mask} = $schema->{read_mask};
- }
+ http_request(
+ PUT => $self->_build_uri( [ $self->{path}, 'bucket' ] ),
+ headers => { 'Content-Type' => 'application/json' },
+ body => JSON::encode_json($schema),
+ sub {
+ $cv->send( $cb->(@_) );
+ }
+ );
+ $cv;
+}
+
+sub fetch {
+ my $self = shift;
+ my $bucket = shift;
+ my $key = shift;
+ my $r = shift;
+ $r = $self->{r} if !$r;
+
+ my ( $cv, $cb ) = $self->_init_callback(@_);
+ $cb = $self->default_cb( { json => 1 } ) if !$cb;
+ http_request(
+ GET => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
+ headers => { 'Content-Type' => 'application/json' },
+ sub {
+ $cv->send( $cb->(@_) );
+ }
+ );
+ $cv;
}
-# sub fetch {
-# 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 = shift;
+ my $object = shift;
+ my $w = shift;
+ my $dw = shift;
-# sub store {
-# my ( $self, $object, $w, $dw, ) = @_;
+ $w = $self->{w} if !$w;
+ $dw = $self->{dw} if !$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};
-# my $bucket = $object->{bucket};
-# my $key = $object->{key};
-# $object->{links} = [] if !exists $object->{links};
+ my ( $cv, $cb ) = $self->_init_callback(@_);
+ $cb = $self->default_cb( { json => 1 } ) if !$cb;
-# return $self->_request(
-# 'PUT',
-# $self->_build_uri(
-# [ $bucket, $key ],
-# {
-# w => $w,
-# dw => $dw,
-# returnbody => 'true'
-# }
-# ),
-# '200',
-# encode_json $object);
-# }
+ http_request(
+ POST => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
+ headers => { 'Content-Type' => 'application/json' },
+ body => JSON::encode_json($object),
+ sub {
+ $cv->send( $cb->(@_) );
+ }
+ );
+ $cv;
+}
# sub delete {
# my ( $self, $bucket, $key, $rw ) = @_;

0 comments on commit 4af8109

Please sign in to comment.