Skip to content

Commit e40c5e4

Browse files
authored
Merge pull request #172 from Kinto/171-fix-batch-patch
Fix patch methods in batch requests (fixes #171)
2 parents bdc4a21 + 72cc8ad commit e40c5e4

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ This document describes changes between each past release.
77
9.2.0 (unreleased)
88
==================
99

10-
- Nothing changed yet.
10+
**Bug fixes**
1111

12+
- Fix patch methods in batch requests (fixes #171)
1213

1314
9.1.0 (2018-02-05)
1415
==================

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VIRTUALENV = virtualenv
1+
VIRTUALENV = virtualenv --python=python3.6
22
VENV := $(shell echo $${VIRTUAL_ENV-.venv})
33
PYTHON = $(VENV)/bin/python
44
DEV_STAMP = $(VENV)/.dev_env_installed.stamp

kinto_http/batch.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ def __init__(self, client, batch_max_requests=0):
1919
self._results = []
2020

2121
def request(self, method, endpoint, data=None, permissions=None,
22-
headers=None):
22+
payload=None, headers=None):
2323
# Store all the requests in a dict, to be read later when .send()
2424
# is called.
25-
self.requests.append((method, endpoint, data, permissions, headers))
25+
payload = payload or {}
26+
if data is not None:
27+
payload['data'] = data
28+
if permissions is not None:
29+
payload['permissions'] = permissions
30+
31+
self.requests.append((method, endpoint, payload, headers))
2632
# This is the signature of the session request.
2733
return defaultdict(dict), defaultdict(dict)
2834

@@ -32,17 +38,13 @@ def reset(self):
3238

3339
def _build_requests(self):
3440
requests = []
35-
for (method, url, data, permissions, headers) in self.requests:
41+
for (method, url, payload, headers) in self.requests:
3642
# Strip the prefix in batch requests.
3743
request = {
3844
'method': method.upper(),
3945
'path': url.replace('v1/', '')}
4046

41-
request['body'] = {}
42-
if data is not None:
43-
request['body']['data'] = data
44-
if permissions is not None:
45-
request['body']['permissions'] = permissions
47+
request['body'] = payload
4648
if headers is not None:
4749
request['headers'] = headers
4850
requests.append(request)

kinto_http/tests/test_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def test_context_manager_works_as_expected(self):
2525
with self.client.batch(bucket='mozilla', collection='test') as batch:
2626
batch.create_record(id=1234, data={'foo': 'bar'})
2727
batch.create_record(id=5678, data={'bar': 'baz'})
28+
batch.patch_record(id=5678, data={'bar': 'biz'})
29+
changes = JSONPatch([{'op': 'add', 'location': 'foo', 'value': 'bar'}])
30+
batch.patch_record(id=5678, changes=changes)
2831

2932
self.session.request.assert_called_with(
3033
method='POST',
@@ -37,7 +40,15 @@ def test_context_manager_works_as_expected(self):
3740
{'body': {'data': {'bar': 'baz'}},
3841
'path': '/buckets/mozilla/collections/test/records/5678',
3942
'method': 'PUT',
40-
'headers': {'If-None-Match': '*'}}]})
43+
'headers': {'If-None-Match': '*'}},
44+
{'body': {'data': {'bar': 'biz'}},
45+
'path': '/buckets/mozilla/collections/test/records/5678',
46+
'method': 'PATCH',
47+
'headers': {'Content-Type': 'application/json'}},
48+
{'body': [{'op': 'add', 'location': 'foo', 'value': 'bar'}],
49+
'path': '/buckets/mozilla/collections/test/records/5678',
50+
'method': 'PATCH',
51+
'headers': {'Content-Type': 'application/json-patch+json'}}]})
4152

4253
def test_batch_raises_exception(self):
4354
# Make the next call to sess.request raise a 403.

0 commit comments

Comments
 (0)