summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-04-10 18:53:09 +0200
committerfranck cuny <franck@lumberjaph.net>2010-04-10 18:53:09 +0200
commit4af8109dc5f298b65ea8fcf95cb961c7ca8099ae (patch)
tree1b990879d0fc526ae72398bcf805aa9380d2a0bc
parentcan use callback (diff)
downloadanyevent-riak-4af8109dc5f298b65ea8fcf95cb961c7ca8099ae.tar.gz
add store, fetch, still missing delete
-rw-r--r--lib/AnyEvent/Riak.pm95
1 files changed, 57 insertions, 38 deletions
diff --git a/lib/AnyEvent/Riak.pm b/lib/AnyEvent/Riak.pm
index 1930da4..f1bf60b 100644
--- a/lib/AnyEvent/Riak.pm
+++ b/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 ) = @_;