Skip to content
Snippets Groups Projects
Verified Commit 9f07826c authored by drebs's avatar drebs
Browse files

[refactor] make blobs backend get_flags() agnostic of twisted.web requests

parent 6f29df7d
No related branches found
No related tags found
1 merge request!169#9007 - improve and document IBlobsBackend
...@@ -85,16 +85,14 @@ class FilesystemBlobsBackend(object): ...@@ -85,16 +85,14 @@ class FilesystemBlobsBackend(object):
_file = static.File(path, defaultType='application/octet-stream') _file = static.File(path, defaultType='application/octet-stream')
return _file.render_GET(request) return _file.render_GET(request)
def get_flags(self, user, blob_id, request, namespace=''): def get_flags(self, user, blob_id, namespace=''):
path = self._get_path(user, blob_id, namespace) path = self._get_path(user, blob_id, namespace)
if not os.path.isfile(path): if not os.path.isfile(path):
# 404 - Not Found raise BlobNotFound
request.setResponseCode(404)
return "Blob doesn't exists: %s" % blob_id
if not os.path.isfile(path + '.flags'): if not os.path.isfile(path + '.flags'):
return '[]' return []
with open(path + '.flags', 'r') as flags_file: with open(path + '.flags', 'r') as flags_file:
return flags_file.read() return json.loads(flags_file.read())
def set_flags(self, user, blob_id, request, namespace=''): def set_flags(self, user, blob_id, request, namespace=''):
path = self._get_path(user, blob_id, namespace) path = self._get_path(user, blob_id, namespace)
...@@ -286,16 +284,18 @@ class BlobsResource(resource.Resource): ...@@ -286,16 +284,18 @@ class BlobsResource(resource.Resource):
order_by=order, deleted=deleted, order_by=order, deleted=deleted,
filter_flag=filter_flag) filter_flag=filter_flag)
only_flags = request.args.get('only_flags', [False])[0] only_flags = request.args.get('only_flags', [False])[0]
if only_flags:
return self._handler.get_flags(user, blob_id, request, namespace)
try: try:
if only_flags:
flags = self._handler.get_flags(user, blob_id, namespace)
return json.dumps(flags)
tag = self._handler.get_tag(user, blob_id, namespace) tag = self._handler.get_tag(user, blob_id, namespace)
request.responseHeaders.setRawHeaders('Tag', [tag])
except BlobNotFound: except BlobNotFound:
# 404 - Not Found # 404 - Not Found
request.setResponseCode(404) request.setResponseCode(404)
return "Blob doesn't exists: %s" % blob_id return "Blob doesn't exists: %s" % blob_id
request.responseHeaders.setRawHeaders('Tag', [tag]) return self._handler.read_blob(user, blob_id, request,
return self._handler.read_blob(user, blob_id, request, namespace) namespace=namespace)
def render_DELETE(self, request): def render_DELETE(self, request):
logger.info("http put: %s" % request.path) logger.info("http put: %s" % request.path)
......
...@@ -167,7 +167,7 @@ class IBlobsBackend(Interface): ...@@ -167,7 +167,7 @@ class IBlobsBackend(Interface):
:rtype: str :rtype: str
""" """
def get_flags(user, blob_id, request, namespace=''): def get_flags(user, blob_id, namespace=''):
""" """
Get the flags for a blob. Get the flags for a blob.
...@@ -175,14 +175,11 @@ class IBlobsBackend(Interface): ...@@ -175,14 +175,11 @@ class IBlobsBackend(Interface):
:type user: str :type user: str
:param blob_id: The id of the blob. :param blob_id: The id of the blob.
:type blob_id: str :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 namespace: An optional namespace for the blob. :param namespace: An optional namespace for the blob.
:type namespace: str :type namespace: str
:return: a JSON encoded string with a list of flags. :return: a list of flags.
:rtype: str :rtype: list of str
""" """
def set_flags(user, blob_id, request, namespace=''): def set_flags(user, blob_id, request, namespace=''):
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
Integration tests for incoming API Integration tests for incoming API
""" """
import pytest import pytest
import json
from io import BytesIO from io import BytesIO
from uuid import uuid4 from uuid import uuid4
from twisted.web.test.test_web import DummyRequest from twisted.web.test.test_web import DummyRequest
...@@ -86,8 +85,7 @@ class IncomingOnCouchServerTestCase(CouchDBTestCase): ...@@ -86,8 +85,7 @@ class IncomingOnCouchServerTestCase(CouchDBTestCase):
db = self.state.open_database(user_id) db = self.state.open_database(user_id)
request = DummyRequest([user_id, doc_id]) request = DummyRequest([user_id, doc_id])
yield db.read_blob(user_id, doc_id, request, 'MX') yield db.read_blob(user_id, doc_id, request, 'MX')
flags = db.get_flags(user_id, doc_id, request, 'MX') flags = db.get_flags(user_id, doc_id, 'MX')
flags = json.loads(flags)
expected_preamble = formatter.preamble(content, doc_id) expected_preamble = formatter.preamble(content, doc_id)
expected_preamble = decode_preamble(expected_preamble, True) expected_preamble = decode_preamble(expected_preamble, True)
written_preamble, written_content = request.written[0].split() written_preamble, written_content = request.written[0].split()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment