Skip to content

Commit a0d76d6

Browse files
author
Kenneth Chan
committed
SDKPYTHON-18 Support channel
1 parent 69761b0 commit a0d76d6

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

examples/event_sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
print("Delete user")
9999
print(client.delete_user("foo"))
100100

101-
# The SDK also support specifying the eventTime. It is useful for importing
101+
# The SDK also support specifying the eventTime. It is useful for importing
102102
# events happened in the past.
103103
foo_time = datetime(2014, 8, 31, 4, 56, tzinfo=pytz.timezone('US/Pacific'))
104104
print("Create user at " + str(foo_time))

examples/event_sample_channel.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# this is example of import events to a specific channel of an App
2+
3+
from predictionio import EventClient
4+
from predictionio import NotFoundError
5+
from datetime import datetime
6+
import pytz
7+
import sys
8+
9+
access_key = None
10+
channel = None
11+
12+
assert access_key is not None, "Please create an access key with 'pio app new'"
13+
# Need to create channel first before
14+
assert channel is not None, "Please create new channel with 'pio app channel-new'"
15+
16+
client = EventClient(access_key=access_key, url="http://localhost:7070",
17+
channel=channel)
18+
19+
# Check status
20+
print("Check status")
21+
print(client.get_status())
22+
23+
# First event
24+
first_event_properties = {
25+
"prop1" : 1,
26+
"prop2" : "value2",
27+
"prop3" : [1, 2, 3],
28+
"prop4" : True,
29+
"prop5" : ["a", "b", "c"],
30+
"prop6" : 4.56 ,
31+
}
32+
first_event_time = datetime(
33+
2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain'))
34+
first_event_response = client.create_event(
35+
event="my_event",
36+
entity_type="user",
37+
entity_id="uid",
38+
properties=first_event_properties,
39+
event_time=first_event_time,
40+
)
41+
print("First Event response")
42+
print(first_event_response)
43+
print
44+
45+
# Second event
46+
second_event_properties = {
47+
"someProperty" : "value1",
48+
"anotherProperty" : "value2",
49+
}
50+
second_event_response = client.create_event(
51+
event="my_event",
52+
entity_type="user",
53+
entity_id="uid",
54+
target_entity_type="item",
55+
target_entity_id="iid",
56+
properties=second_event_properties,
57+
event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc))
58+
print("Second Event response")
59+
print(second_event_response)
60+
print
61+
62+
63+
# Get the first event from Event Server
64+
first_event_id = first_event_response.json_body["eventId"]
65+
print("Get Event")
66+
event = client.get_event(first_event_id)
67+
print(event)
68+
print
69+
70+
# Delete the first event from Event Server
71+
print("Delete Event")
72+
delete_response = client.delete_event(first_event_id)
73+
print(delete_response)
74+
print
75+
76+
# Delete the first event from Event Server again should yield exception.
77+
print("Delete Event Again")
78+
try:
79+
delete_response = client.delete_event(first_event_id)
80+
except NotFoundError, ex:
81+
print("The expected error: {0}".format(ex))
82+
print

predictionio/__init__.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
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+
2128
import json
2229
import 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

Comments
 (0)