From ca07c53421f0146f0d7f290f7d0b7be0b6afa01e Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:16:26 -0700 Subject: [PATCH 1/9] update version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2afa4af..bd95042 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ url='https://github.com/harlev/jsonbox-python', author="Ron Harlev", author_email="harlev@gmail.com", - version='1.1.0', + version='1.2.1', packages=['.'], license='MIT', long_description=open('README.md').read(), From 62b2dd2440e749fbaf1861f15125314ec1fdce9a Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:28:04 -0700 Subject: [PATCH 2/9] support getting meta --- jsonbox.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/jsonbox.py b/jsonbox.py index 324cfdb..7e60c0c 100644 --- a/jsonbox.py +++ b/jsonbox.py @@ -40,6 +40,11 @@ def _get_url(self, return url + def _get_meta_url(self, box_id): + url = "{0}/_meta/{1}".format(self.service_host, box_id) + + return url + def get_record_id(self, data): if isinstance(data, list): return [item[self.RECORD_ID_KEY] for item in data] @@ -62,6 +67,12 @@ def read(self, response = requests.get(url) return self._check_response(response) + def get_meta(self, box_id): + url = self._get_meta_url(box_id) + + response = requests.get(url) + return self._check_response(response) + def write(self, data, box_id, collection=None, api_key=None): url = self._get_url(box_id, collection) From 7e02e5fb9aec5d399cfc4e7a7bb3f7f7d1ad425b Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:28:11 -0700 Subject: [PATCH 3/9] test meta --- tests/test_jsonbin.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_jsonbin.py b/tests/test_jsonbin.py index 76a747f..672a58a 100644 --- a/tests/test_jsonbin.py +++ b/tests/test_jsonbin.py @@ -47,6 +47,16 @@ def test_read_box(self): self.assertIsNotNone(json_data) self.assertTrue(isinstance(json_data, list)) + def test_get_meta(self): + data = [{"name": "first", "age": "25"}, {"name": "second", "age": "19"}] + box_id = TEST_BOX_ID + "_meta" + result = self.jb.write(data, box_id) + + json_data = self.jb.get_meta(box_id) + self.assertIsNotNone(json_data) + self.assertEqual(json_data["_count"], 2) + self.assertIsNotNone(json_data["_createdOn"]) + def test_read_sort(self): data = [{"name": "first", "age": "25"}, {"name": "second", "age": "19"}] box_id = TEST_BOX_ID + "_sort" From 93842a152f79f87c2e9ecd97fadeef66ade5bf00 Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:28:19 -0700 Subject: [PATCH 4/9] add example --- examples/example.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/example.py b/examples/example.py index f0f297e..bd0a7b0 100644 --- a/examples/example.py +++ b/examples/example.py @@ -21,6 +21,9 @@ # read all records in box print(jb.read(MY_BOX_ID)) +# get metadata for box +print(jb.get_meta(MY_BOX_ID)) + # read all records in box with sort print(jb.read(MY_BOX_ID, sort_by="age")) From 126571c5397c8c8bc2fe8e4685c5e4e38c9657b6 Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:29:04 -0700 Subject: [PATCH 5/9] update example in readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b9607ce..55629c3 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ print(jb.read(MY_BOX_ID, record_ids[0])) # read all records in box print(jb.read(MY_BOX_ID)) +# get metadata for box +print(jb.get_meta(MY_BOX_ID)) + # read all records in box with sort print(jb.read(MY_BOX_ID, sort_by="age")) From 7171201e0b83462daf65db366252432497e014e4 Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:31:15 -0700 Subject: [PATCH 6/9] add get_new_box_id --- jsonbox.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jsonbox.py b/jsonbox.py index 7e60c0c..a7c83b2 100644 --- a/jsonbox.py +++ b/jsonbox.py @@ -55,6 +55,10 @@ def get_record_id(self, data): def get_new_api_key(): return str(uuid.uuid4()) + @staticmethod + def get_new_box_id(): + return str(uuid.uuid4()).replace("-", "_") + def read(self, box_id, collection_or_record=None, From 9336708621e2711d95f945807c03578fdb0d13f8 Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:31:24 -0700 Subject: [PATCH 7/9] use in tests --- tests/test_jsonbin.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_jsonbin.py b/tests/test_jsonbin.py index 672a58a..10e5d36 100644 --- a/tests/test_jsonbin.py +++ b/tests/test_jsonbin.py @@ -1,10 +1,9 @@ import unittest -import uuid from jsonbox import JsonBox -TEST_BOX_ID = str(uuid.uuid4()).replace("-", "_") -TEST_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_") -TEST_PRIVATE_BOX_ID_FAIL = str(uuid.uuid4()).replace("-", "_") +TEST_BOX_ID = JsonBox.get_new_box_id() +TEST_PRIVATE_BOX_ID = JsonBox.get_new_box_id() +TEST_PRIVATE_BOX_ID_FAIL = JsonBox.get_new_box_id() TEST_COLLECTION_ID = "collection_427453" TEST_RECORD_ID = "test_sjdgfygsf2347623564twfgyu" TEST_DATA_KEY_1 = "gjsfdjghdjs" From 5bf75ea2cf62e017eb720356e854c815163c1dbd Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:33:09 -0700 Subject: [PATCH 8/9] use in example --- examples/example.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/example.py b/examples/example.py index bd0a7b0..7941528 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,8 +1,7 @@ -import uuid from jsonbox import JsonBox # generate unique box id -MY_BOX_ID = str(uuid.uuid4()).replace("-", "_") +MY_BOX_ID = JsonBox.get_new_box_id() # create instance jb = JsonBox() @@ -52,7 +51,7 @@ jb.delete(MY_BOX_ID, record_ids[1]) # write to a private box -MY_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_") +MY_PRIVATE_BOX_ID = JsonBox.get_new_box_id() api_key = jb.get_new_api_key() result = jb.write(data, MY_PRIVATE_BOX_ID, api_key=api_key) record_id = jb.get_record_id(result) @@ -63,5 +62,3 @@ # delete a private box jb.delete(MY_PRIVATE_BOX_ID, record_id, api_key=api_key) - - From 5f9646f010b64f641cb4058aa862a826553504ed Mon Sep 17 00:00:00 2001 From: rharlev Date: Wed, 18 Mar 2020 17:33:28 -0700 Subject: [PATCH 9/9] update in readme --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 55629c3..5c5e005 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,10 @@ Python wrapper for https://jsonbox.io (with support for V2 features) ## Usage ```python -import uuid from jsonbox import JsonBox # generate unique box id -MY_BOX_ID = str(uuid.uuid4()).replace("-", "_") +MY_BOX_ID = JsonBox.get_new_box_id() # create instance jb = JsonBox() @@ -68,7 +67,7 @@ print(jb.delete(MY_BOX_ID, query="age:=23")) jb.delete(MY_BOX_ID, record_ids[1]) # write to a private box -MY_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_") +MY_PRIVATE_BOX_ID = JsonBox.get_new_box_id() api_key = jb.get_new_api_key() result = jb.write(data, MY_PRIVATE_BOX_ID, api_key=api_key) record_id = jb.get_record_id(result)