PIOTEAM-41. $delete actions
diff --git a/examples/event_sample.py b/examples/event_sample.py
index 467bb38..9000ee1 100644
--- a/examples/event_sample.py
+++ b/examples/event_sample.py
@@ -1,7 +1,7 @@
from predictionio import EventClient
from predictionio import NotFoundError
-client = EventClient(app_id=4, url="http://localhost:7070")
+client = EventClient(app_id=6, url="http://localhost:7070")
# Check status
print("Check status")
@@ -69,3 +69,27 @@
except NotFoundError, ex:
print("The expected error: {0}".format(ex))
print
+
+# Use "user"-helper methods
+
+# Set user properties implicitly create a user
+# This call creates a user "foo", and set the properties of "foo".
+print("Create user foo")
+foo_properties = {"city": "sunnyvale", "car": "honda fit"}
+print(client.set_user("foo", properties=foo_properties))
+
+# This call overrides the existing properties for user "foo", setting "car" to
+# a new "honda odyssey" and create a new property "food" to "seafood".
+print("Set new properties")
+foo_properties = {"car": "honda odyssey", "food": "seafood"}
+print(client.set_user("foo", properties=foo_properties))
+
+# This call removes the specified properties. It ignores the value of the dict.
+# After this call, the "city" will become an unset field.
+print("Unset properties")
+foo_properties = {"city": "x"}
+print(client.unset_user("foo", properties=foo_properties))
+
+# This call deletes a user
+print("Delete user")
+print(client.delete_user("foo"))
diff --git a/predictionio/__init__.py b/predictionio/__init__.py
index dba7f4b..4828d9b 100644
--- a/predictionio/__init__.py
+++ b/predictionio/__init__.py
@@ -25,6 +25,8 @@
import json
import urllib
+from datetime import datetime
+
from predictionio.connection import Connection
from predictionio.connection import AsyncRequest
from predictionio.connection import PredictionIOAPIError
@@ -177,9 +179,13 @@
"properties" : properties,
"appId" : self.app_id
})
+
+ def set_user(self, uid, properties={}):
+ return self.aset_user(uid, properties).get_response()
- def aunset_user(self, uid, properties={}):
+ def aunset_user(self, uid, properties):
"""unset properties of an user"""
+ # check properties={}, it cannot be empty
return self.acreate_event({
"event" : "$unset",
"entityType" : "pio_user",
@@ -187,6 +193,21 @@
"properties" : properties,
"appId" : self.app_id
})
+
+ def unset_user(self, uid, properties):
+ return self.aunset_user(uid, properties).get_response()
+
+ def adelete_user(self, uid):
+ """set properties of an user"""
+ return self.acreate_event({
+ "event" : "$delete",
+ "entityType" : "pio_user",
+ "entityId" : uid,
+ "appId": self.app_id,
+ })
+
+ def delete_user(self, uid):
+ return self.adelete_user(uid).get_response()
def aset_item(self, iid, properties={}):
return self.acreate_event({
@@ -196,6 +217,9 @@
"properties" : properties,
"appId" : self.app_id
})
+
+ def set_item(self, iid, properties={}):
+ return self.aset_item(iid, properties).get_response()
def aunset_item(self, iid, properties={}):
return self.acreate_event({
@@ -205,6 +229,9 @@
"properties" : properties,
"appId" : self.app_id
})
+
+ def unset_item(self, iid, properties={}):
+ return self.aunset_item(iid, properties).get_response()
def arecord_user_action_on_item(self, action, uid, iid, properties={}):
return self.acreate_event({
@@ -217,18 +244,6 @@
"appId" : self.app_id
})
- def set_user(self, uid, properties={}):
- return self.aset_user(uid, properties).get_response()
-
- def set_item(self, iid, properties={}):
- return self.aset_item(iid, properties).get_response()
-
- def unset_user(self, uid, properties={}):
- return self.aunset_user(uid, properties).get_response()
-
- def unset_item(self, iid, properties={}):
- return self.aunset_item(iid, properties).get_response()
-
def record_user_action_on_item(self, action, uid, iid, properties={}):
return self.arecord_user_action_on_item(
action, uid, iid, properties).get_response()