Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

remove some code; use _build_headers, fix POST and DELETE

  • Loading branch information...
commit 57850822e0e942cc0be6d4b31a7992d69d917b7f 1 parent 260d577
Franck Cuny authored April 11, 2010

Showing 1 changed file with 40 additions and 94 deletions. Show diff stats Hide diff stats

  1. 134  lib/AnyEvent/Riak.pm
134  lib/AnyEvent/Riak.pm
@@ -23,7 +23,7 @@ sub new {
23 23
     my $dw          = delete $args{dw}          || 2;
24 24
 
25 25
     my $client_id
26  
-        = "perl_anyevent_riak_" . encode_base64( int( rand(10737411824) ) );
  26
+        = "perl_anyevent_riak_" . encode_base64( int( rand(10737411824) ), '' );
27 27
 
28 28
     bless {
29 29
         host        => $host,
@@ -49,40 +49,29 @@ sub _build_headers {
49 49
     my ( $self, $options) = @_;
50 50
     my $headers = {};
51 51
     $headers = {
52  
-        'Content-Type'    => 'application/json',
53 52
         'X-Riak-ClientId' => $self->{client_id},
  53
+        'Content-Type'    => 'application/json',
54 54
     };
55 55
     return $headers;
56 56
 }
57 57
 
  58
+sub _build_query {
  59
+    my ($self, $options) = @_;
  60
+}
  61
+
58 62
 sub _init_callback {
59 63
     my $self = shift;
60  
-    $self->all_cv->begin();
61 64
 
62 65
     my ( $cv, $cb );
63 66
     if (@_) {
64 67
         $cv = pop if UNIVERSAL::isa( $_[-1], 'AnyEvent::CondVar' );
65 68
         $cb = pop if ref $_[-1] eq 'CODE';
66 69
     }
67  
-    $cv ||= AE::cv;
68  
-
69  
-    $cv->cb(
70  
-        sub {
71  
-            my $cv  = shift;
72  
-            my $res = $cv->recv;
73  
-            $cb->($res);
74  
-        }
75  
-    ) if $cb;
76 70
 
  71
+    $cv ||= AE::cv;
77 72
     return ( $cv, $cb );
78 73
 }
79 74
 
80  
-sub all_cv {
81  
-    my $self = shift;
82  
-    $self->{all_cv} = AE::cv unless $self->{all_cv};
83  
-    return $self->{all_cv};
84  
-}
85  
-
86 75
 sub default_cb {
87 76
     my ( $self, $options ) = @_;
88 77
     return sub {
@@ -109,7 +98,7 @@ sub is_alive {
109 98
 
110 99
     http_request(
111 100
         GET => $self->_build_uri( [qw/ping/] ),
112  
-        headers => { 'Content-Type' => 'application/json', },
  101
+        headers => $self->_build_headers(),
113 102
         sub {
114 103
             $cv->send( $cb->(@_) );
115 104
         },
@@ -127,10 +116,10 @@ sub list_bucket {
127 116
 
128 117
     http_request(
129 118
         GET => $self->_build_uri( [ $self->{path}, $bucket_name ] ),
130  
-        headers => { 'Content-Type' => 'application/json', },
131  
-        sub {
132  
-            $cv->send( $cb->(@_) );
133  
-        }
  119
+        headers => $self->_build_headers(),
  120
+       sub {
  121
+           $cv->send($cb->(@_));
  122
+       }
134 123
     );
135 124
     return $cv;
136 125
 }
@@ -145,10 +134,10 @@ sub set_bucket {
145 134
 
146 135
     http_request(
147 136
         PUT => $self->_build_uri( [ $self->{path}, 'bucket' ] ),
148  
-        headers => { 'Content-Type' => 'application/json' },
  137
+        headers => $self->_build_headers(),
149 138
         body    => JSON::encode_json($schema),
150 139
         sub {
151  
-            $cv->send( $cb->(@_) );
  140
+            $cv->send($cb->(@_));
152 141
         }
153 142
     );
154 143
     $cv;
@@ -164,9 +153,10 @@ sub fetch {
164 153
 
165 154
     my ( $cv, $cb ) = $self->_init_callback(@_);
166 155
     $cb = $self->default_cb( { json => 1 } ) if !$cb;
  156
+
167 157
     http_request(
168 158
         GET => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
169  
-        headers => { 'Content-Type' => 'application/json' },
  159
+        headers => $self->_build_headers(),
170 160
         sub {
171 161
             $cv->send( $cb->(@_) );
172 162
         }
@@ -176,24 +166,24 @@ sub fetch {
176 166
 
177 167
 sub store {
178 168
     my $self   = shift;
179  
-    my $object = shift;
  169
+    my $bucket = shift;
  170
+    my $key    = shift;
  171
+    my $data   = shift;
180 172
     my $w      = shift;
181 173
     my $dw     = shift;
182 174
 
183 175
     $w  = $self->{w}  if !$w;
184 176
     $dw = $self->{dw} if !$dw;
185 177
 
186  
-    my $bucket = $object->{bucket};
187  
-    my $key    = $object->{key};
188  
-    $object->{links} = [] if !exists $object->{links};
189  
-
190 178
     my ( $cv, $cb ) = $self->_init_callback(@_);
191  
-    $cb = $self->default_cb( { json => 1 } ) if !$cb;
  179
+    $cb = $self->default_cb( { json => 0 } ) if !$cb;
  180
+
  181
+    my $json = JSON::encode_json($data);
192 182
 
193 183
     http_request(
194 184
         POST => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
195  
-        headers => { 'Content-Type' => 'application/json' },
196  
-        body    => JSON::encode_json($object),
  185
+        headers => $self->_build_headers(),
  186
+        body    => $json,
197 187
         sub {
198 188
             $cv->send( $cb->(@_) );
199 189
         }
@@ -201,13 +191,23 @@ sub store {
201 191
     $cv;
202 192
 }
203 193
 
204  
-# sub delete {
205  
-#     my ( $self, $bucket, $key, $rw ) = @_;
  194
+sub delete {
  195
+    my $self = shift;
  196
+    my $bucket = shift;
  197
+    my $key = shift;
  198
+
  199
+    my ($cv, $cb) = $self->_init_callback(@_);
  200
+    $cb = $self->default_cb({json => 1}) if !$cb;
206 201
 
207  
-#     $rw = $self->{rw} || 2 if !$rw;
208  
-#     return $self->_request( 'DELETE',
209  
-#         $self->_build_uri( [ $bucket, $key ], { dw => $rw } ), 204 );
210  
-# }
  202
+    http_request(
  203
+        DELETE => $self->_build_uri([$self->{path}, $bucket, $key]),
  204
+        headers => $self->_build_headers(),
  205
+        sub {
  206
+            $cv->send($cb->(@_));
  207
+        }
  208
+    );
  209
+    $cv;
  210
+}
211 211
 
212 212
 # sub walk {
213 213
 #     my ( $self, $bucket, $key, $spec ) = @_;
@@ -228,60 +228,6 @@ sub store {
228 228
 #     return $acc;
229 229
 # }
230 230
 
231  
-
232  
-# sub _build_query {
233  
-#     my ($self, $options) = @_;
234  
-# }
235  
-
236  
-# sub _request {
237  
-#     my ( $self, $method, $uri, $expected, $body ) = @_;
238  
-#     my $cv = AnyEvent->condvar;
239  
-#     my $cb = sub {
240  
-#         my ( $body, $headers ) = @_;
241  
-#         if ( $headers->{Status} == $expected ) {
242  
-#             if ( $body && $headers->{'content-type'} eq 'application/json' ) {
243  
-#                 return $cv->send( decode_json($body) );
244  
-#             }
245  
-#             else {
246  
-#                 return $cv->send(1);
247  
-#             }
248  
-#         }
249  
-#         else {
250  
-#             return $cv->croak(
251  
-#                 encode_json( [ $headers->{Status}, $headers->{Reason} ] ) );
252  
-#         }
253  
-#     };
254  
-
255  
-#     if ($body) {
256  
-#         http_request(
257  
-#             $method => $uri,
258  
-#             headers => { 'Content-Type' => 'application/json', },
259  
-#             body    => $body,
260  
-#             $cb
261  
-#         );
262  
-#     }
263  
-#     else {
264  
-#         http_request(
265  
-#             $method => $uri,
266  
-#             headers => { 'Content-Type' => 'application/json', },
267  
-#             $cb
268  
-#         );
269  
-#     }
270  
-#     $cv;
271  
-# }
272  
-
273  
-sub head {
274  
-}
275  
-
276  
-sub get {
277  
-}
278  
-
279  
-sub put {
280  
-}
281  
-
282  
-sub post {
283  
-}
284  
-
285 231
 1;
286 232
 __END__
287 233
 

0 notes on commit 5785082

Please sign in to comment.
Something went wrong with that request. Please try again.