diff --git a/README.md b/README.md index b9607ce..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() @@ -37,6 +36,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")) @@ -65,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) diff --git a/examples/example.py b/examples/example.py index f0f297e..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() @@ -21,6 +20,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")) @@ -49,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) @@ -60,5 +62,3 @@ # delete a private box jb.delete(MY_PRIVATE_BOX_ID, record_id, api_key=api_key) - - diff --git a/jsonbox.py b/jsonbox.py index 324cfdb..a7c83b2 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] @@ -50,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, @@ -62,6 +71,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) 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(), diff --git a/tests/test_jsonbin.py b/tests/test_jsonbin.py index 76a747f..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" @@ -47,6 +46,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"