Skip to content

Commit 3ce0e37

Browse files
authored
Merge branch 'develop' into DVPL-10029
2 parents 3d013c4 + cd742c8 commit 3ce0e37

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

examples/kvstore.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,40 @@ 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))
6780

6881
query = json.dumps({"otherkey": "bar"})
6982
print("Should return third item with auto-generated _key: %s" % json.dumps(collection.data.query(query=query), indent=1))
70-
83+
84+
# passing query data as dict
85+
query = {"somekey": {"$gt": 1}}
86+
print("Should return item2 and item3: %s" % json.dumps(collection.data.query(query=query), indent=1))
87+
7188
# Let's delete the collection
7289
collection.delete()
7390

splunklib/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,6 +3640,11 @@ def query(self, **query):
36403640
:return: Array of documents retrieved by query.
36413641
:rtype: ``array``
36423642
"""
3643+
3644+
for key, value in query.items():
3645+
if isinstance(query[key], dict):
3646+
query[key] = json.dumps(value)
3647+
36433648
return json.loads(self._get('', **query).body.read().decode('utf-8'))
36443649

36453650
def query_by_id(self, id):
@@ -3664,6 +3669,8 @@ def insert(self, data):
36643669
:return: _id of inserted object
36653670
:rtype: ``dict``
36663671
"""
3672+
if isinstance(data, dict):
3673+
data = json.dumps(data)
36673674
return json.loads(self._post('', headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))
36683675

36693676
def delete(self, query=None):
@@ -3700,6 +3707,8 @@ def update(self, id, data):
37003707
:return: id of replaced document
37013708
:rtype: ``dict``
37023709
"""
3710+
if isinstance(data, dict):
3711+
data = json.dumps(data)
37033712
return json.loads(self._post(UrlEncoded(str(id), encode_slash=True), headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))
37043713

37053714
def batch_find(self, *dbqueries):

0 commit comments

Comments
 (0)