1818 # pylint: disable=F0401
1919 # http is a Python3 module, replacing httplib
2020 from http import client as httplib
21+
22+ try :
23+ from urllib import urlencode
24+ except ImportError :
25+ # pylint: disable=F0401,E0611
26+ from urllib .parse import urlencode
27+
2128import json
2229import urllib
2330
@@ -165,11 +172,12 @@ class EventClient(BaseClient):
165172 :param timeout: timeout for HTTP connection attempts and requests in
166173 seconds (optional).
167174 Default value is 5.
175+ :param channel: channel name (optional)
168176 """
169177
170178 def __init__ (self , access_key ,
171179 url = "http://localhost:7070" ,
172- threads = 1 , qsize = 0 , timeout = 5 ):
180+ threads = 1 , qsize = 0 , timeout = 5 , channel = None ):
173181 assert type (access_key ) is str , ("access_key must be string. "
174182 "Notice that app_id has been deprecated in Prediction.IO 0.8.2. "
175183 "Please use access_key instead." )
@@ -183,6 +191,7 @@ def __init__(self, access_key,
183191 "you may use an earlier version of this sdk." )
184192
185193 self .access_key = access_key
194+ self .channel = channel
186195
187196 def acreate_event (self , event , entity_type , entity_id ,
188197 target_entity_type = None , target_entity_id = None , properties = None ,
@@ -228,7 +237,15 @@ def acreate_event(self, event, entity_type, entity_id,
228237 et_str = et .strftime ("%Y-%m-%dT%H:%M:%S.%f" )[:- 3 ] + et .strftime ("%z" )
229238 data ["eventTime" ] = et_str
230239
231- path = "/events.json?accessKey=%s" % (self .access_key , )
240+ qparam = {
241+ "accessKey" : self .access_key
242+ }
243+
244+ if self .channel is not None :
245+ qparam ["channel" ] = self .channel
246+
247+ path = "/events.json?%s" % (urlencode (qparam ), )
248+
232249 request = AsyncRequest ("POST" , path , ** data )
233250 request .set_rfunc (self ._acreate_resp )
234251 self ._connection .make_request (request )
@@ -251,9 +268,16 @@ def aget_event(self, event_id):
251268 :returns:
252269 AsyncRequest object.
253270 """
271+ qparam = {
272+ "accessKey" : self .access_key
273+ }
274+
275+ if self .channel is not None :
276+ qparam ["channel" ] = self .channel
277+
254278 enc_event_id = urllib .quote (event_id , "" ) # replace special char with %xx
255- path = "/events/%s.json" % enc_event_id
256- request = AsyncRequest ("GET" , path , accessKey = self . access_key )
279+ path = "/events/%s.json" % ( enc_event_id , )
280+ request = AsyncRequest ("GET" , path , ** qparam )
257281 request .set_rfunc (self ._aget_resp )
258282 self ._connection .make_request (request )
259283 return request
@@ -271,9 +295,16 @@ def adelete_event(self, event_id):
271295 :returns:
272296 AsyncRequest object.
273297 """
298+ qparam = {
299+ "accessKey" : self .access_key
300+ }
301+
302+ if self .channel is not None :
303+ qparam ["channel" ] = self .channel
304+
274305 enc_event_id = urllib .quote (event_id , "" ) # replace special char with %xx
275306 path = "/events/%s.json" % (enc_event_id , )
276- request = AsyncRequest ("DELETE" , path , accessKey = self . access_key )
307+ request = AsyncRequest ("DELETE" , path , ** qparam )
277308 request .set_rfunc (self ._adelete_resp )
278309 self ._connection .make_request (request )
279310 return request
0 commit comments