Commits

jasonjwwilliams committed ad88d22

Added list_buckets call to RiakClient.

* RiakClient now implements the list buckets command
as list_buckets().
* Modified build_rest_path() to allow for no bucket to
be passed in (needed for list_buckets). Modification
also makes the bucket arg required for the key arg
to be used by build_rest_path().

Comments (0)

Files changed (2)

         """
         mr = RiakMapReduce(self)
         return apply(mr.link, args)
+    
+    @defer.inlineCallbacks
+    def list_buckets(self):
+        """
+        Retrieve a list of all buckets.
+    
+        :returns: list -- via deferred
+        """
+        # Create the request
+        params = {"buckets" : "true"}
+    
+        host, port, url = RiakUtils.build_rest_path(self,
+                                                    None,
+                                                    None,
+                                                    None,
+                                                    params)
+        raw_response = yield RiakUtils.http_request_deferred('GET', host, port, url)
+        
+        if raw_response[0]["http_code"] != 200:
+            raise Exception('Error listing buckets.')
+        
+        # Parse the bucket list
+        buckets = json.loads(raw_response[1])["buckets"]
+        
+        defer.returnValue(buckets)
 
     def map(self, *args):
         """
             return default_value
 
     @classmethod
-    def build_rest_path(cls, client, bucket, key=None, spec=None, params=None) :
+    def build_rest_path(cls, client, bucket=None, key=None, spec=None, params=None) :
         """
         Given a RiakClient, RiakBucket, Key, LinkSpec, and Params,
         construct and return a URL.
         # Build 'http://hostname:port/prefix/bucket'
         path = ''
         path += '/' + client._prefix
-        path += '/' + urllib.quote_plus(bucket._name)
+        
+        # Add '.../bucket'
+        if bucket:
+            path += '/' + urllib.quote_plus(bucket._name)
 
         # Add '.../key'
-        if (key != None):
+        if bucket and key:
             path += '/' + urllib.quote_plus(key)
 
         # Add query parameters.
 See txriak.LICENSE for details.
 """
 
-import os, sys, time
+import os, sys
 import json
 import random
 from twisted.trial import unittest
     @defer.inlineCallbacks
     def test_list_keys(self):
         """Test listing all keys in bucket."""
-        log.msg("*** add_and_delete")
+        log.msg("*** list_keys")
     
         client = riak.RiakClient(client_id=RIAK_CLIENT_ID)
         bucket = client.bucket("bucket")
         
         keys = yield bucket.list_keys()
         self.assertEquals(sorted([u"foo1", u"foo2"]), sorted(keys))
+    
+    @defer.inlineCallbacks
+    def test_list_buckets(self):
+        """Test listing all buckets."""
+        log.msg("*** list_buckets")
+    
+        client = riak.RiakClient(client_id=RIAK_CLIENT_ID)
+        bucket1 = client.bucket("bucket1")
+        obj1 = bucket1.new("foo1", "test1")
+        yield obj1.store()
+        
+        bucket2 = client.bucket("bucket2")
+        obj2 = bucket2.new("foo2", "test2")
+        yield obj2.store()
+        
+        buckets = yield client.list_buckets()
+        self.assertTrue(u"bucket1" in buckets)
+        self.assertTrue(u"bucket2" in buckets)
+        
+        # Cleanup after ourselves
+        yield obj1.delete()
+        yield obj2.delete()
 
 
     @defer.inlineCallbacks