Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updated KVStore Methods #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2021
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
17 changes: 15 additions & 2 deletions examples/kvstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,29 @@ 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))
Expand Down
4 changes: 4 additions & 0 deletions splunklib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,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 +3702,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)), headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))

def batch_find(self, *dbqueries):
Expand Down