Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Add request bound data, shared with subrequests.
Browse files Browse the repository at this point in the history
This allows to store any arbitrary data in a ``dict`` on the current request,
instead of setting custom attributes and testing their existance.

It is shared with subrequests during batch.

In the short term, this will allow us to reduce storage hits in batch
subrequests on the default bucket in Kinto.

In the long term, this will allow us to share storage connections accross
batch subrequests.
  • Loading branch information
leplatrem committed Sep 23, 2015
1 parent 365bda8 commit 03e85c2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions cliquet/__init__.py
Expand Up @@ -40,6 +40,7 @@
'cliquet.http_scheme': None,
'cliquet.id_generator': 'cliquet.storage.generators.UUID4',
'cliquet.initialization_sequence': (
'cliquet.initialization.setup_request_bound_data',
'cliquet.initialization.setup_json_serializer',
'cliquet.initialization.setup_logging',
'cliquet.initialization.setup_storage',
Expand Down
10 changes: 10 additions & 0 deletions cliquet/initialization.py
Expand Up @@ -31,6 +31,16 @@
from pyramid_multiauth import MultiAuthPolicySelected


def setup_request_bound_data(config):
"""Attach custom data on request object, and share it with parent
requests during batch."""
def attach_bound_data(request):
parent = getattr(request, 'parent', None)
return parent.bound_data if parent else {}

config.add_request_method(attach_bound_data, name='bound_data', reify=True)


def setup_json_serializer(config):
# Monkey patch to use ujson
webob.request.json = utils.json
Expand Down
28 changes: 28 additions & 0 deletions cliquet/tests/test_initialization.py
Expand Up @@ -2,6 +2,7 @@
import webtest

from pyramid.config import Configurator
from pyramid.events import NewRequest

import cliquet
from cliquet import initialization
Expand Down Expand Up @@ -245,6 +246,33 @@ def _get_app(self, settings={}):
cliquet.initialize(config, '0.0.1', 'name')
return webtest.TestApp(config.make_wsgi_app())

def test_requests_have_a_bound_data_attribute(self):
config = Configurator()
cliquet.initialize(config, '0.0.1', 'name')

def on_new_request(event):
data = event.request.bound_data
self.assertEqual(data, {})
self.assertEqual(id(data), id(event.request.bound_data))

config.add_subscriber(on_new_request, NewRequest)
app = webtest.TestApp(config.make_wsgi_app())
app.get('/v0/')

def test_subrequests_share_parent_bound_data(self):
config = Configurator()
cliquet.initialize(config, '0.0.1', 'name')

bound_datas = set()

def on_new_request(event):
bound_datas.add(id(event.request.bound_data))

config.add_subscriber(on_new_request, NewRequest)
app = webtest.TestApp(config.make_wsgi_app())
app.post_json('/v0/batch', {'requests': [{'path': '/'}]})
self.assertEqual(len(bound_datas), 1)

def test_by_default_relies_on_pyramid_application_url(self):
app = self._get_app()
resp = app.get('/v0/')
Expand Down
6 changes: 6 additions & 0 deletions cliquet/tests/test_views_batch.py
Expand Up @@ -253,6 +253,12 @@ def test_returns_requests_path_in_responses(self):
result = self.post({'requests': [{'path': '/'}]})
self.assertEqual(result['responses'][0]['path'], '/v0/')

def test_subrequests_have_parent_attribute(self):
self.request.path = '/batch'
self.post({'requests': [{'path': '/'}]})
subrequest, = self.request.invoke_subrequest.call_args[0]
self.assertEqual(subrequest.parent.path, '/batch')

def test_subrequests_are_GET_by_default(self):
self.post({'requests': [{'path': '/'}]})
subrequest, = self.request.invoke_subrequest.call_args[0]
Expand Down
1 change: 1 addition & 0 deletions cliquet/views/batch.py
Expand Up @@ -91,6 +91,7 @@ def post_batch(request):

for subrequest_spec in requests:
subrequest = build_request(request, subrequest_spec)
subrequest.parent = request

sublogger.bind(path=subrequest.path,
method=subrequest.method)
Expand Down

0 comments on commit 03e85c2

Please sign in to comment.