[refactor] make list_blobs() return a deferred
authordrebs <drebs@leap.se>
Sat, 9 Dec 2017 00:34:34 +0000 (22:34 -0200)
committerdrebs <drebs@leap.se>
Wed, 13 Dec 2017 15:49:27 +0000 (13:49 -0200)
src/leap/soledad/server/_blobs.py
src/leap/soledad/server/interfaces.py
tests/blobs/test_fs_backend.py

index 42c0084..7210cb6 100644 (file)
@@ -184,7 +184,7 @@ class FilesystemBlobsBackend(object):
             blob_ids = list(self._filter_flag(blob_ids, filter_flag))
         blob_ids = [os.path.basename(path).replace('.deleted', '')
                     for path in blob_ids]
-        return blob_ids
+        return defer.succeed(blob_ids)
 
     def _filter_flag(self, blob_paths, flag):
         for blob_path in blob_paths:
@@ -288,10 +288,13 @@ class BlobsResource(resource.Resource):
             order = request.args.get('order_by', [None])[0]
             filter_flag = request.args.get('filter_flag', [False])[0]
             deleted = request.args.get('deleted', [False])[0]
-            blobs = self._handler.list_blobs(user, namespace,
-                                             order_by=order, deleted=deleted,
-                                             filter_flag=filter_flag)
-            return json.dumps(blobs)
+            d = self._handler.list_blobs(user, namespace,
+                                         order_by=order, deleted=deleted,
+                                         filter_flag=filter_flag)
+            d.addCallback(lambda blobs: json.dumps(blobs))
+            d.addCallback(lambda blobs: request.write(blobs))
+            d.addCallback(lambda _: request.finish())
+            return NOT_DONE_YET
         only_flags = request.args.get('only_flags', [False])[0]
         try:
             if only_flags:
index a176264..7009cb8 100644 (file)
@@ -127,6 +127,10 @@ class IBlobsBackend(Interface):
         :return: A list of blob ids, optionally ordered and/or restricted by
                  namespace.
         :rtype: list of str
+
+        :return: A deferred that fires with a list of blob ids, optionally
+            ordered and/or restricted by namespace.
+        :rtype: twisted.internet.defer.Deferred
         """
 
     def get_total_storage(user):
index e77e56b..727ffe9 100644 (file)
@@ -127,20 +127,22 @@ class FilesystemBackendTestCase(unittest.TestCase):
 
     @pytest.mark.usefixtures("method_tmpdir")
     @mock.patch('leap.soledad.server._blobs.os.walk')
+    @defer.inlineCallbacks
     def test_list_blobs(self, walk_mock):
         backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir)
         _ = None
         walk_mock.return_value = [('', _, ['blob_0']), ('', _, ['blob_1'])]
-        result = backend.list_blobs('user')
+        result = yield backend.list_blobs('user')
         self.assertEquals(result, ['blob_0', 'blob_1'])
 
     @pytest.mark.usefixtures("method_tmpdir")
     @mock.patch('leap.soledad.server._blobs.os.walk')
+    @defer.inlineCallbacks
     def test_list_blobs_limited_by_namespace(self, walk_mock):
         backend = _blobs.FilesystemBlobsBackend(self.tempdir)
         _ = None
         walk_mock.return_value = [('', _, ['blob_0']), ('', _, ['blob_1'])]
-        result = backend.list_blobs('user', namespace='incoming')
+        result = yield backend.list_blobs('user', namespace='incoming')
         self.assertEquals(result, ['blob_0', 'blob_1'])
         target_dir = os.path.join(self.tempdir, 'user', 'incoming')
         walk_mock.assert_called_once_with(target_dir)