Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 515e4d6

Browse files
authored
Merge pull request #3 from harlev/meta
Support metadata read URL
2 parents af9b8b1 + 5f9646f commit 515e4d6

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ Python wrapper for https://jsonbox.io (with support for V2 features)
1414

1515
## Usage
1616
```python
17-
import uuid
1817
from jsonbox import JsonBox
1918

2019
# generate unique box id
21-
MY_BOX_ID = str(uuid.uuid4()).replace("-", "_")
20+
MY_BOX_ID = JsonBox.get_new_box_id()
2221

2322
# create instance
2423
jb = JsonBox()
@@ -37,6 +36,9 @@ print(jb.read(MY_BOX_ID, record_ids[0]))
3736
# read all records in box
3837
print(jb.read(MY_BOX_ID))
3938

39+
# get metadata for box
40+
print(jb.get_meta(MY_BOX_ID))
41+
4042
# read all records in box with sort
4143
print(jb.read(MY_BOX_ID, sort_by="age"))
4244

@@ -65,7 +67,7 @@ print(jb.delete(MY_BOX_ID, query="age:=23"))
6567
jb.delete(MY_BOX_ID, record_ids[1])
6668

6769
# write to a private box
68-
MY_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_")
70+
MY_PRIVATE_BOX_ID = JsonBox.get_new_box_id()
6971
api_key = jb.get_new_api_key()
7072
result = jb.write(data, MY_PRIVATE_BOX_ID, api_key=api_key)
7173
record_id = jb.get_record_id(result)

‎examples/example.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import uuid
21
from jsonbox import JsonBox
32

43
# generate unique box id
5-
MY_BOX_ID = str(uuid.uuid4()).replace("-", "_")
4+
MY_BOX_ID = JsonBox.get_new_box_id()
65

76
# create instance
87
jb = JsonBox()
@@ -21,6 +20,9 @@
2120
# read all records in box
2221
print(jb.read(MY_BOX_ID))
2322

23+
# get metadata for box
24+
print(jb.get_meta(MY_BOX_ID))
25+
2426
# read all records in box with sort
2527
print(jb.read(MY_BOX_ID, sort_by="age"))
2628

@@ -49,7 +51,7 @@
4951
jb.delete(MY_BOX_ID, record_ids[1])
5052

5153
# write to a private box
52-
MY_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_")
54+
MY_PRIVATE_BOX_ID = JsonBox.get_new_box_id()
5355
api_key = jb.get_new_api_key()
5456
result = jb.write(data, MY_PRIVATE_BOX_ID, api_key=api_key)
5557
record_id = jb.get_record_id(result)
@@ -60,5 +62,3 @@
6062

6163
# delete a private box
6264
jb.delete(MY_PRIVATE_BOX_ID, record_id, api_key=api_key)
63-
64-

‎jsonbox.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def _get_url(self,
4040

4141
return url
4242

43+
def _get_meta_url(self, box_id):
44+
url = "{0}/_meta/{1}".format(self.service_host, box_id)
45+
46+
return url
47+
4348
def get_record_id(self, data):
4449
if isinstance(data, list):
4550
return [item[self.RECORD_ID_KEY] for item in data]
@@ -50,6 +55,10 @@ def get_record_id(self, data):
5055
def get_new_api_key():
5156
return str(uuid.uuid4())
5257

58+
@staticmethod
59+
def get_new_box_id():
60+
return str(uuid.uuid4()).replace("-", "_")
61+
5362
def read(self,
5463
box_id,
5564
collection_or_record=None,
@@ -62,6 +71,12 @@ def read(self,
6271
response = requests.get(url)
6372
return self._check_response(response)
6473

74+
def get_meta(self, box_id):
75+
url = self._get_meta_url(box_id)
76+
77+
response = requests.get(url)
78+
return self._check_response(response)
79+
6580
def write(self, data, box_id, collection=None, api_key=None):
6681
url = self._get_url(box_id, collection)
6782

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
url='https://github.com/harlev/jsonbox-python',
77
author="Ron Harlev",
88
author_email="harlev@gmail.com",
9-
version='1.1.0',
9+
version='1.2.1',
1010
packages=['.'],
1111
license='MIT',
1212
long_description=open('README.md').read(),

‎tests/test_jsonbin.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import unittest
2-
import uuid
32
from jsonbox import JsonBox
43

5-
TEST_BOX_ID = str(uuid.uuid4()).replace("-", "_")
6-
TEST_PRIVATE_BOX_ID = str(uuid.uuid4()).replace("-", "_")
7-
TEST_PRIVATE_BOX_ID_FAIL = str(uuid.uuid4()).replace("-", "_")
4+
TEST_BOX_ID = JsonBox.get_new_box_id()
5+
TEST_PRIVATE_BOX_ID = JsonBox.get_new_box_id()
6+
TEST_PRIVATE_BOX_ID_FAIL = JsonBox.get_new_box_id()
87
TEST_COLLECTION_ID = "collection_427453"
98
TEST_RECORD_ID = "test_sjdgfygsf2347623564twfgyu"
109
TEST_DATA_KEY_1 = "gjsfdjghdjs"
@@ -47,6 +46,16 @@ def test_read_box(self):
4746
self.assertIsNotNone(json_data)
4847
self.assertTrue(isinstance(json_data, list))
4948

49+
def test_get_meta(self):
50+
data = [{"name": "first", "age": "25"}, {"name": "second", "age": "19"}]
51+
box_id = TEST_BOX_ID + "_meta"
52+
result = self.jb.write(data, box_id)
53+
54+
json_data = self.jb.get_meta(box_id)
55+
self.assertIsNotNone(json_data)
56+
self.assertEqual(json_data["_count"], 2)
57+
self.assertIsNotNone(json_data["_createdOn"])
58+
5059
def test_read_sort(self):
5160
data = [{"name": "first", "age": "25"}, {"name": "second", "age": "19"}]
5261
box_id = TEST_BOX_ID + "_sort"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /