Skip to content

Commit 39573f0

Browse files
authored
Merge pull request #214 from Kinto/112-history-support
Add history support (fixes #112)
2 parents 83b66fb + 86066fd commit 39573f0

File tree

6 files changed

+82
-2
lines changed

6 files changed

+82
-2
lines changed

CHANGELOG.rst

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

10-
- Nothing changed yet.
10+
**New features**
11+
12+
- Add history support (fixes #112), Thanks @FlorianKuckelkorn!
1113

1214

1315
10.4.1 (2019-05-22)

README.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,28 @@ of them, you can specify the number of pages:
317317
records = client.get_records(_limit=10, pages=float('inf')) # Infinity
318318
319319
320+
History
321+
-------
322+
323+
If the built-in `history plugin <https://kinto.readthedocs.io/en/latest/api/1.x/history.html>`_ is enabled, it is possible to retrieve the history of changes:
324+
325+
.. code-block:: python
326+
327+
# Get the complete history of a bucket
328+
changes = client.get_history(bucket='default')
329+
330+
# and optionally use filters
331+
hist = client.get_history(bucket='default', _limit=2, _sort='-last_modified', _since='1533762576015')
332+
hist = client.get_history(bucket='default', resource_name='collection')
333+
334+
335+
The history of a bucket can also be purged with:
336+
337+
.. code-block:: python
338+
339+
client.purge_history(bucket='default')
340+
341+
320342
Endpoint URLs
321343
-------------
322344

kinto_http/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Endpoints(object):
4747
"batch": "{root}/batch",
4848
"buckets": "{root}/buckets",
4949
"bucket": "{root}/buckets/{bucket}",
50+
"history": "{root}/buckets/{bucket}/history",
5051
"groups": "{root}/buckets/{bucket}/groups",
5152
"group": "{root}/buckets/{bucket}/groups/{group}",
5253
"collections": "{root}/buckets/{bucket}/collections",
@@ -862,6 +863,18 @@ def delete_records(self, *, collection=None, bucket=None, safe=True, if_match=No
862863
resp, _ = self.session.request("delete", endpoint, headers=headers)
863864
return resp["data"]
864865

866+
def get_history(self, *, bucket=None, **kwargs):
867+
endpoint = self.get_endpoint("history", bucket=bucket)
868+
logger.info("Get history from bucket %r" % bucket or self._bucket_name)
869+
return self._paginated(endpoint, **kwargs)
870+
871+
def purge_history(self, *, bucket=None, safe=True, if_match=None):
872+
endpoint = self.get_endpoint('history', bucket=bucket)
873+
headers = self._get_cache_headers(safe, if_match=if_match)
874+
logger.info("Purge History of bucket %r" % bucket or self._bucket_name)
875+
resp, _ = self.session.request('delete', endpoint, headers=headers)
876+
return resp['data']
877+
865878
def __repr__(self):
866879
if self._collection_name:
867880
endpoint = self.get_endpoint(

kinto_http/tests/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_group_creation_if_not_exists(self):
142142
def test_group_creation_if_bucket_does_not_exist(self):
143143
with pytest.raises(KintoException) as e:
144144
self.client.create_group(id="payments", bucket="mozilla", data={"members": ["blah"]})
145-
assert str(e).endswith(
145+
assert str(e.value).endswith(
146146
"PUT /v1/buckets/mozilla/groups/payments - "
147147
"403 Unauthorized. Please check that the "
148148
"bucket exists and that you have the permission "

kinto_http/tests/test_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,3 +1182,43 @@ def test_update_record_can_deduce_id_from_data(self):
11821182
permissions=None,
11831183
headers=None,
11841184
)
1185+
1186+
1187+
class HistoryTest(unittest.TestCase):
1188+
def setUp(self):
1189+
self.session = mock.MagicMock()
1190+
self.client = Client(session=self.session)
1191+
1192+
def test_basic_retrivial_of_bucket_history(self):
1193+
mock_response(self.session)
1194+
self.client.get_history(bucket='mybucket')
1195+
url = '/buckets/mybucket/history'
1196+
self.session.request.assert_called_with('get', url, headers={}, params={})
1197+
1198+
def test_filter_sorting_operations_on_bucket_history(self):
1199+
mock_response(self.session)
1200+
self.client.get_history(bucket='mybucket',
1201+
_limit=2,
1202+
_sort='-last_modified',
1203+
_since='1533762576015')
1204+
1205+
url = '/buckets/mybucket/history'
1206+
self.session.request.assert_called_with('get', url, headers={},
1207+
params={'_limit': 2,
1208+
'_sort': '-last_modified',
1209+
'_since': '1533762576015'}
1210+
)
1211+
1212+
def test_filtering_by_resource_name(self):
1213+
mock_response(self.session)
1214+
self.client.get_history(bucket='mybucket', resource_name='collection')
1215+
url = '/buckets/mybucket/history'
1216+
self.session.request.assert_called_with('get', url, headers={},
1217+
params={'resource_name': 'collection'}
1218+
)
1219+
1220+
def test_purging_of_history(self):
1221+
mock_response(self.session)
1222+
self.client.purge_history(bucket='mybucket')
1223+
url = '/buckets/mybucket/history'
1224+
self.session.request.assert_called_with('delete', url, headers=None)

kinto_http/tests/test_endpoints.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def test_record(self):
3838
== "/buckets/buck/collections/coll/records/1"
3939
)
4040

41+
def test_history(self):
42+
assert self.endpoints.get("history", **self.kwargs) == "/buckets/buck/history"
43+
4144
def test_missing_arguments_raise_an_error(self):
4245
# Don't include the record id; it should raise an error.
4346
with self.assertRaises(KintoException) as context:

0 commit comments

Comments
 (0)