Skip to content

Commit ba05bb9

Browse files
authored
Merge pull request #396 from splunk/DVPL-10033
Updated KVStore Methods
2 parents ff6cd64 + d634d2d commit ba05bb9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

examples/kvstore.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,29 @@ def main():
5151
# Let's make sure it doesn't have any data
5252
print("Should be empty: %s" % json.dumps(collection.data.query()))
5353

54-
# Let's add some data
54+
# Let's add some json data
5555
collection.data.insert(json.dumps({"_key": "item1", "somekey": 1, "otherkey": "foo"}))
56-
collection.data.insert(json.dumps({"_key": "item2", "somekey": 2, "otherkey": "foo"}))
56+
#Let's add data as a dictionary object
57+
collection.data.insert({"_key": "item2", "somekey": 2, "otherkey": "foo"})
5758
collection.data.insert(json.dumps({"somekey": 3, "otherkey": "bar"}))
5859

5960
# Let's make sure it has the data we just entered
6061
print("Should have our data: %s" % json.dumps(collection.data.query(), indent=1))
6162

6263
# Let's run some queries
6364
print("Should return item1: %s" % json.dumps(collection.data.query_by_id("item1"), indent=1))
65+
66+
#Let's update some data
67+
data = collection.data.query_by_id("item2")
68+
data['otherkey'] = "bar"
69+
#Passing data using 'json.dumps'
70+
collection.data.update("item2", json.dumps(data))
71+
print("Should return item2 with updated data: %s" % json.dumps(collection.data.query_by_id("item2"), indent=1))
72+
data['otherkey'] = "foo"
73+
# Passing data as a dictionary instance
74+
collection.data.update("item2", data)
75+
print("Should return item2 with updated data: %s" % json.dumps(collection.data.query_by_id("item2"), indent=1))
76+
6477

6578
query = json.dumps({"otherkey": "foo"})
6679
print("Should return item1 and item2: %s" % json.dumps(collection.data.query(query=query), indent=1))

splunklib/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,8 @@ def insert(self, data):
36643664
:return: _id of inserted object
36653665
:rtype: ``dict``
36663666
"""
3667+
if isinstance(data, dict):
3668+
data = json.dumps(data)
36673669
return json.loads(self._post('', headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))
36683670

36693671
def delete(self, query=None):
@@ -3700,6 +3702,8 @@ def update(self, id, data):
37003702
:return: id of replaced document
37013703
:rtype: ``dict``
37023704
"""
3705+
if isinstance(data, dict):
3706+
data = json.dumps(data)
37033707
return json.loads(self._post(UrlEncoded(str(id)), headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))
37043708

37053709
def batch_find(self, *dbqueries):

0 commit comments

Comments
 (0)