From d5a30f5909a17c7d8ba661d35f73c5c09a7c0dc6 Mon Sep 17 00:00:00 2001
From: drebs <drebs@leap.se>
Date: Fri, 8 Dec 2017 22:48:02 -0200
Subject: [PATCH] [refactor] make get_flags() return a deferred

---
 src/leap/soledad/server/_blobs.py     | 14 +++++++++-----
 src/leap/soledad/server/interfaces.py |  4 ++--
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py
index 9d1c9b1e..5fe9c1b7 100644
--- a/src/leap/soledad/server/_blobs.py
+++ b/src/leap/soledad/server/_blobs.py
@@ -101,11 +101,12 @@ class FilesystemBlobsBackend(object):
     def get_flags(self, user, blob_id, namespace=''):
         path = self._get_path(user, blob_id, namespace)
         if not os.path.isfile(path):
-            raise BlobNotFound
+            return defer.fail(BlobNotFound)
         if not os.path.isfile(path + '.flags'):
-            return []
+            return defer.succeed([])
         with open(path + '.flags', 'r') as flags_file:
-            return json.loads(flags_file.read())
+            flags = json.loads(flags_file.read())
+            return defer.succeed(flags)
 
     def set_flags(self, user, blob_id, flags, namespace=''):
         path = self._get_path(user, blob_id, namespace)
@@ -298,8 +299,11 @@ class BlobsResource(resource.Resource):
         only_flags = request.args.get('only_flags', [False])[0]
         try:
             if only_flags:
-                flags = self._handler.get_flags(user, blob_id, namespace)
-                return json.dumps(flags)
+                d = self._handler.get_flags(user, blob_id, namespace)
+                d.addCallback(lambda flags: json.dumps(flags))
+                d.addCallback(lambda flags: request.write(flags))
+                d.addCallback(lambda _: request.finish())
+                return NOT_DONE_YET
             tag = self._handler.get_tag(user, blob_id, namespace)
             request.responseHeaders.setRawHeaders('Tag', [tag])
         except BlobNotFound:
diff --git a/src/leap/soledad/server/interfaces.py b/src/leap/soledad/server/interfaces.py
index ecea0bf8..bd804573 100644
--- a/src/leap/soledad/server/interfaces.py
+++ b/src/leap/soledad/server/interfaces.py
@@ -173,8 +173,8 @@ class IBlobsBackend(Interface):
         :param namespace: An optional namespace for the blob.
         :type namespace: str
 
-        :return: a list of flags.
-        :rtype: list of str
+        :return: A deferred that fires with the list of flags for a blob.
+        :rtype: twisted.internet.defer.Deferred
         """
 
     def set_flags(user, blob_id, flags, namespace=''):
-- 
GitLab