Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"))

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"))

Expand Down Expand Up @@ -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)
Expand All @@ -60,5 +62,3 @@

# delete a private box
jb.delete(MY_PRIVATE_BOX_ID, record_id, api_key=api_key)


15 changes: 15 additions & 0 deletions jsonbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 13 additions & 4 deletions tests/test_jsonbin.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
Expand Down