@@ -41,8 +41,20 @@ class NotFoundError(PredictionIOAPIError):
41
41
pass
42
42
43
43
44
- def now_if_none (t ):
45
- return datetime .now (pytz .utc ) if t is None else t
44
+ def event_time_validation (t ):
45
+ """ Validate event_time according to EventAPI Specification.
46
+ """
47
+
48
+ if t is None :
49
+ return datetime .now (pytz .utc )
50
+
51
+ if type (t ) != datetime :
52
+ raise AttributeError ("event_time must be datetime.datetime" )
53
+
54
+ if t .tzinfo is None :
55
+ raise AttributeError ("event_time must have tzinfo" )
56
+
57
+ return t
46
58
47
59
48
60
class BaseClient (object ):
@@ -146,15 +158,40 @@ def __init__(self, app_id, url="http://localhost:7070",
146
158
super (EventClient , self ).__init__ (url , threads , qsize , timeout )
147
159
self .app_id = app_id
148
160
149
- def acreate_event (self , data ):
161
+ def acreate_event (self , event , entity_type , entity_id ,
162
+ target_entity_type = None , target_entity_id = None , properties = None ,
163
+ event_time = None ):
164
+ data = {
165
+ "appId" : self .app_id ,
166
+ "event" : event ,
167
+ "entityType" : entity_type ,
168
+ "entityId" : entity_id ,
169
+ }
170
+
171
+ if target_entity_type is not None :
172
+ data ["targetEntityType" ] = target_entity_type
173
+
174
+ if target_entity_id is not None :
175
+ data ["targetEntityId" ] = target_entity_id
176
+
177
+ if properties is not None :
178
+ data ["properties" ] = properties
179
+
180
+ event_time = event_time_validation (event_time )
181
+ data ["eventTime" ] = event_time .isoformat ()
182
+
150
183
path = "/events.json"
151
184
request = AsyncRequest ("POST" , path , ** data )
152
185
request .set_rfunc (self ._acreate_resp )
153
186
self ._connection .make_request (request )
154
187
return request
155
188
156
- def create_event (self , data ):
157
- return self .acreate_event (data ).get_response ()
189
+ def create_event (self , event , entity_type , entity_id ,
190
+ target_entity_type = None , target_entity_id = None , properties = None ,
191
+ event_time = None ):
192
+ return self .acreate_event (event , entity_type , entity_id ,
193
+ target_entity_type , target_entity_id , properties ,
194
+ event_time ).get_response ()
158
195
159
196
def aget_event (self , event_id ):
160
197
enc_event_id = urllib .quote (event_id , "" ) # replace special char with %xx
@@ -181,107 +218,86 @@ def delete_event(self, event_id):
181
218
## Below are helper functions
182
219
183
220
def aset_user (self , uid , properties = {}, event_time = None ):
184
- event_time = now_if_none (event_time )
185
-
186
221
"""set properties of an user"""
187
- return self .acreate_event ({
188
- "event" : "$set" ,
189
- "entityType" : "pio_user" ,
190
- "entityId" : uid ,
191
- "properties" : properties ,
192
- "appId" : self .app_id ,
193
- "eventTime" : event_time .isoformat (),
194
- })
222
+ return self .acreate_event (
223
+ event = "$set" ,
224
+ entity_type = "pio_user" ,
225
+ entity_id = uid ,
226
+ properties = properties ,
227
+ event_time = event_time ,
228
+ )
195
229
196
230
def set_user (self , uid , properties = {}, event_time = None ):
197
231
return self .aset_user (uid , properties , event_time ).get_response ()
198
232
199
233
def aunset_user (self , uid , properties , event_time = None ):
200
234
"""unset properties of an user"""
201
- event_time = now_if_none (event_time )
202
-
203
235
# check properties={}, it cannot be empty
204
- return self .acreate_event ({
205
- "event" : "$unset" ,
206
- "entityType" : "pio_user" ,
207
- "entityId" : uid ,
208
- "properties" : properties ,
209
- "appId" : self .app_id ,
210
- "eventTime" : event_time .isoformat (),
211
- })
236
+ return self .acreate_event (
237
+ event = "$unset" ,
238
+ entity_type = "pio_user" ,
239
+ entity_id = uid ,
240
+ properties = properties ,
241
+ event_time = event_time ,
242
+ )
212
243
213
244
def unset_user (self , uid , properties , event_time = None ):
214
245
return self .aunset_user (uid , properties , event_time ).get_response ()
215
246
216
247
def adelete_user (self , uid , event_time = None ):
217
248
"""set properties of an user"""
218
- event_time = now_if_none (event_time )
219
- return self .acreate_event ({
220
- "event" : "$delete" ,
221
- "entityType" : "pio_user" ,
222
- "entityId" : uid ,
223
- "appId" : self .app_id ,
224
- "eventTime" : event_time .isoformat (),
225
- })
249
+ return self .acreate_event (
250
+ event = "$delete" ,
251
+ entity_type = "pio_user" ,
252
+ entity_id = uid ,
253
+ event_time = event_time )
226
254
227
255
def delete_user (self , uid , event_time = None ):
228
256
return self .adelete_user (uid , event_time ).get_response ()
229
257
230
258
def aset_item (self , iid , properties = {}, event_time = None ):
231
- event_time = now_if_none (event_time )
232
- return self .acreate_event ({
233
- "event" : "$set" ,
234
- "entityType" : "pio_item" ,
235
- "entityId" : iid ,
236
- "properties" : properties ,
237
- "appId" : self .app_id ,
238
- "eventTime" : event_time .isoformat (),
239
- })
259
+ return self .acreate_event (
260
+ event = "$set" ,
261
+ entity_type = "pio_item" ,
262
+ entity_id = iid ,
263
+ properties = properties ,
264
+ event_time = event_time )
240
265
241
266
def set_item (self , iid , properties = {}, event_time = None ):
242
- return self .aset_item (iid , properties ).get_response ()
267
+ return self .aset_item (iid , properties , event_time ).get_response ()
243
268
244
269
def aunset_item (self , iid , properties = {}, event_time = None ):
245
- event_time = now_if_none (event_time )
246
- return self .acreate_event ({
247
- "event" : "$unset" ,
248
- "entityType" : "pio_item" ,
249
- "entityId" : iid ,
250
- "properties" : properties ,
251
- "appId" : self .app_id ,
252
- "eventTime" : event_time .isoformat (),
253
- })
270
+ return self .acreate_event (
271
+ event = "$unset" ,
272
+ entity_type = "pio_item" ,
273
+ entity_id = iid ,
274
+ properties = properties ,
275
+ event_time = event_time )
254
276
255
277
def unset_item (self , iid , properties = {}, event_time = None ):
256
278
return self .aunset_item (iid , properties , event_time ).get_response ()
257
279
258
280
def adelete_item (self , iid , event_time = None ):
259
281
"""set properties of an user"""
260
- event_time = now_if_none (event_time )
261
- return self .acreate_event ({
262
- "event" : "$delete" ,
263
- "entityType" : "pio_item" ,
264
- "entityId" : iid ,
265
- "appId" : self .app_id ,
266
- "eventTime" : event_time .isoformat (),
267
- })
282
+ return self .acreate_event (
283
+ event = "$delete" ,
284
+ entity_type = "pio_item" ,
285
+ entity_id = iid ,
286
+ event_time = event_time )
268
287
269
288
def delete_item (self , iid , event_time = None ):
270
289
return self .adelete_item (iid , event_time ).get_response ()
271
290
272
291
def arecord_user_action_on_item (self , action , uid , iid , properties = {},
273
292
event_time = None ):
274
- event_time = now_if_none (event_time )
275
- return self .acreate_event ({
276
- "event" : action ,
277
- "entityType" : "pio_user" ,
278
- "entityId" : uid ,
279
- "targetEntityType" : "pio_item" ,
280
- "targetEntityId" : iid ,
281
- "properties" : properties ,
282
- "appId" : self .app_id ,
283
- "eventTime" : event_time .isoformat (),
284
- })
293
+ return self .acreate_event (
294
+ event = action ,
295
+ entity_type = "pio_user" ,
296
+ entity_id = uid ,
297
+ target_entity_type = "pio_item" ,
298
+ target_entity_id = iid ,
299
+ properties = properties ,
300
+ event_time = event_time )
285
301
286
302
def record_user_action_on_item (self , action , uid , iid , properties = {},
287
303
event_time = None ):
0 commit comments