From 8a1ab21e09632928be79fdf7473073ae363f8231 Mon Sep 17 00:00:00 2001 From: drebs Date: Fri, 8 Dec 2017 22:54:50 -0200 Subject: [refactor] make set_flags() return a deferred --- src/leap/soledad/server/_blobs.py | 28 +++++++++++++++++++--------- src/leap/soledad/server/interfaces.py | 3 +++ 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'src/leap/soledad/server') diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py index 5fe9c1b7..bfe95516 100644 --- a/src/leap/soledad/server/_blobs.py +++ b/src/leap/soledad/server/_blobs.py @@ -320,7 +320,8 @@ class BlobsResource(resource.Resource): def catchBlobNotFound(failure): failure.trap(BlobNotFound) request.setResponseCode(404) - return "Blob doesn't exists: %s" % blob_id + request.write("Blob doesn't exists: %s" % blob_id) + request.finish() d = self._handler.delete_blob(user, blob_id, namespace=namespace) d.addCallback(lambda _: request.finish()) @@ -357,17 +358,26 @@ class BlobsResource(resource.Resource): user, blob_id, namespace = self._validate(request) raw_flags = request.content.read() flags = json.loads(raw_flags) - try: - self._handler.set_flags(user, blob_id, flags, namespace=namespace) - except BlobNotFound: - # 404 - Not Found + + def catchBlobNotFound(failure): + failure.trap(BlobNotFound) request.setResponseCode(404) - return "Blob doesn't exists: %s" % blob_id - except InvalidFlag as e: + request.write("Blob doesn't exists: %s" % blob_id) + request.finish() + + def catchInvalidFlag(failure): + e = failure.trap(InvalidFlag) request.setResponseCode(406) flag = e.message - return "Invalid flag: %s" % str(flag) - return '' + request.write("Invalid flag: %s" % str(flag)) + request.finish() + + d = self._handler.set_flags(user, blob_id, flags, namespace=namespace) + d.addCallback(lambda _: request.write('')) + d.addCallback(lambda _: request.finish()) + d.addErrback(catchBlobNotFound) + d.addErrback(catchInvalidFlag) + return NOT_DONE_YET def _error(self, e, request): logger.error('Error processing request: %s' % e.getErrorMessage()) diff --git a/src/leap/soledad/server/interfaces.py b/src/leap/soledad/server/interfaces.py index bd804573..358a0d62 100644 --- a/src/leap/soledad/server/interfaces.py +++ b/src/leap/soledad/server/interfaces.py @@ -189,4 +189,7 @@ class IBlobsBackend(Interface): :type flags: list of str :param namespace: An optional namespace for the blob. :type namespace: str + + :return: A deferred that fires when the flags have been set. + :rtype: twisted.internet.defer.Deferred """ -- cgit v1.2.3