Skip to content

Commit

Permalink
Merge branch 'develop' into DVPL-10029
Browse files Browse the repository at this point in the history
  • Loading branch information
ashah-splunk committed Sep 23, 2021
2 parents 3d013c4 + cd742c8 commit 3ce0e37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 20 additions & 3 deletions examples/kvstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,40 @@ def main():
# Let's make sure it doesn't have any data
print("Should be empty: %s" % json.dumps(collection.data.query()))

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

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

# Let's run some queries
print("Should return item1: %s" % json.dumps(collection.data.query_by_id("item1"), indent=1))

#Let's update some data
data = collection.data.query_by_id("item2")
data['otherkey'] = "bar"
#Passing data using 'json.dumps'
collection.data.update("item2", json.dumps(data))
print("Should return item2 with updated data: %s" % json.dumps(collection.data.query_by_id("item2"), indent=1))
data['otherkey'] = "foo"
# Passing data as a dictionary instance
collection.data.update("item2", data)
print("Should return item2 with updated data: %s" % json.dumps(collection.data.query_by_id("item2"), indent=1))


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

query = json.dumps({"otherkey": "bar"})
print("Should return third item with auto-generated _key: %s" % json.dumps(collection.data.query(query=query), indent=1))


# passing query data as dict
query = {"somekey": {"$gt": 1}}
print("Should return item2 and item3: %s" % json.dumps(collection.data.query(query=query), indent=1))

# Let's delete the collection
collection.delete()

Expand Down
9 changes: 9 additions & 0 deletions splunklib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,11 @@ def query(self, **query):
:return: Array of documents retrieved by query.
:rtype: ``array``
"""

for key, value in query.items():
if isinstance(query[key], dict):
query[key] = json.dumps(value)

return json.loads(self._get('', **query).body.read().decode('utf-8'))

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

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

def batch_find(self, *dbqueries):
Expand Down

0 comments on commit 3ce0e37

Please sign in to comment.