summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2017-12-06 19:48:53 -0200
committerdrebs <drebs@leap.se>2017-12-13 13:43:48 -0200
commit4a6ea1ac3302420584a3cd453652e487c5eebca0 (patch)
tree6cea1aff1807815203a56cb9f54be3c32977f73b
parent03b4b4c410252af0a85dd94cb0b3a91eaca9b9a4 (diff)
[refactor] make blobs backend set_flags() agnostic of twisted.web requests
-rw-r--r--src/leap/soledad/common/blobs/__init__.py6
-rw-r--r--src/leap/soledad/server/_blobs.py26
-rw-r--r--src/leap/soledad/server/_incoming.py8
-rw-r--r--src/leap/soledad/server/interfaces.py13
4 files changed, 29 insertions, 24 deletions
diff --git a/src/leap/soledad/common/blobs/__init__.py b/src/leap/soledad/common/blobs/__init__.py
index 34a04d3c..bce02b04 100644
--- a/src/leap/soledad/common/blobs/__init__.py
+++ b/src/leap/soledad/common/blobs/__init__.py
@@ -20,3 +20,9 @@ Represents flags that can be used on blobs.
from collections import namedtuple
ACCEPTED_FLAGS = ['PENDING', 'PROCESSING', 'PROCESSED', 'FAILED']
Flags = namedtuple('Flags', ' '.join(ACCEPTED_FLAGS))(*ACCEPTED_FLAGS)
+
+
+class InvalidFlag(Exception):
+ """
+ Raised when a flag that is not accepted is used.
+ """
diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py
index bf370979..893f752e 100644
--- a/src/leap/soledad/server/_blobs.py
+++ b/src/leap/soledad/server/_blobs.py
@@ -41,6 +41,7 @@ from leap.common.files import mkdir_p
from leap.soledad.common.log import getLogger
from leap.soledad.server import interfaces
from leap.soledad.common.blobs import ACCEPTED_FLAGS
+from leap.soledad.common.blobs import InvalidFlag
__all__ = ['BlobsResource']
@@ -94,19 +95,15 @@ class FilesystemBlobsBackend(object):
with open(path + '.flags', 'r') as flags_file:
return json.loads(flags_file.read())
- def set_flags(self, user, blob_id, request, namespace=''):
+ def set_flags(self, user, blob_id, flags, namespace=''):
path = self._get_path(user, blob_id, namespace)
if not os.path.isfile(path):
- # 404 - Not Found
- request.setResponseCode(404)
- return "Blob doesn't exists: %s" % blob_id
- raw_flags = request.content.read()
- flags = json.loads(raw_flags)
+ raise BlobNotFound
for flag in flags:
if flag not in ACCEPTED_FLAGS:
- request.setResponseCode(406)
- return "Unsupported flag: %s" % flag
+ raise InvalidFlag(flag)
with open(path + '.flags', 'w') as flags_file:
+ raw_flags = json.dumps(flags)
flags_file.write(raw_flags)
@defer.inlineCallbacks
@@ -315,7 +312,18 @@ class BlobsResource(resource.Resource):
def render_POST(self, request):
logger.info("http post: %s" % request.path)
user, blob_id, namespace = self._validate(request)
- self._handler.set_flags(user, blob_id, request, namespace)
+ 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
+ request.setResponseCode(404)
+ return "Blob doesn't exists: %s" % blob_id
+ except InvalidFlag as e:
+ request.setResponseCode(406)
+ flag = e.message
+ return "Invalid flag: %s" % str(flag)
return ''
def _error(self, e, request):
diff --git a/src/leap/soledad/server/_incoming.py b/src/leap/soledad/server/_incoming.py
index e4ca4de8..be172b22 100644
--- a/src/leap/soledad/server/_incoming.py
+++ b/src/leap/soledad/server/_incoming.py
@@ -17,13 +17,11 @@
"""
A twisted resource that saves externally delivered documents into user's db.
"""
-import json
import base64
from io import BytesIO
from twisted.web.server import NOT_DONE_YET
from twisted.web.resource import Resource
-from twisted.web.test.test_web import DummyRequest
from leap.soledad.common.blobs import Flags
from leap.soledad.common.blobs import preamble
@@ -83,9 +81,9 @@ class IncomingResource(Resource):
# FIXME: We really need to decouple request handling from the
# backend! This is very ugly, but will change when this refactor
# is done.
- flagsReq = DummyRequest([''])
- flagsReq.content = BytesIO(json.dumps([Flags.PENDING]))
- d.addCallback(lambda _: db.set_flags(uuid, doc_id, flagsReq, 'MX'))
+ flags = [Flags.PENDING]
+ d.addCallback(lambda _: db.set_flags(uuid, doc_id, flags,
+ namespace='MX'))
d.addCallback(lambda _: self._finish(request))
d.addErrback(self._error, request)
return NOT_DONE_YET
diff --git a/src/leap/soledad/server/interfaces.py b/src/leap/soledad/server/interfaces.py
index 3274dfdf..a4dead70 100644
--- a/src/leap/soledad/server/interfaces.py
+++ b/src/leap/soledad/server/interfaces.py
@@ -170,23 +170,16 @@ class IBlobsBackend(Interface):
:rtype: list of str
"""
- def set_flags(user, blob_id, request, namespace=''):
+ def set_flags(user, blob_id, flags, namespace=''):
"""
Set flags for a blob.
- The flags are expected to be send in the body of the request, as a JSON
- list of strings.
-
:param user: The id of the user who owns the blob.
:type user: str
:param blob_id: The id of the blob.
:type blob_id: str
- :param request: A representation of all of the information about the
- request that is being made.
- :type request: twisted.web.server.Request
+ :param flags: The list of flags to be set.
+ :type flags: list of str
:param namespace: An optional namespace for the blob.
:type namespace: str
-
- :return: A string describing an error or ``None`` in case of success.
- :rtype: str
"""