def acreate_user(self, uid, params={}): """Asynchronously create a user. :param uid: user id. type str. :param params: optional attributes. type dictionary. For example, { 'custom': 'value', 'pio_inactive' : True, 'pio_latlng': [4.5,67.8] } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_inactive" in params: params["pio_inactive"] = str(params["pio_inactive"]).lower() path = "%s/users.json" % self.apiversion request = AsyncRequest( "POST", path, pio_appkey=self.appkey, pio_uid=uid, **params) request.set_rfunc(self._acreate_user_resp) self._connection.make_request(request) return request
def _aget_itemsim_topn(self, engine, iid, n, params={}): """Private function to asynchronously get top n similar items of the item :param engine: name of the prediction engine. type str. :param iid: item id. type str. :param n: number of similar items. type int. :param params: optional parameters. type dictionary For example, { 'pio_itypes' : ("t1","t2") } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_itypes" in params: params["pio_itypes"] = ",".join(params["pio_itypes"]) if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_attributes" in params: params["pio_attributes"] = ",".join(params["pio_attributes"]) path = "%s/engines/itemsim/%s/topn.json" % (self.apiversion, engine) request = AsyncRequest("GET", path, pio_appkey=self.appkey, pio_iid=iid, pio_n=n, **params) request.set_rfunc(self._aget_itemsim_topn_resp) self._connection.make_request(request) return request
def _auser_action_on_item(self, action, uid, iid, params): """Private function to asynchronously create an user action on an item :param action: action type. type str. ("like", "dislike", "conversion", "rate", "view") :param uid: user id. type str or int. :param iid: item id. type str or int. :param params: optional attributes. type dictionary. For example, { 'pio_rate' : 4, 'pio_latlng' : [1.23,4.56] } NOTE: For "rate" action, pio_rate attribute is required. integer value of 1-5 (1 is least preferred and 5 is most preferred) :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) path = "%s/actions/u2i.json" % (self.apiversion) request = AsyncRequest("POST", path, pio_appkey=self.appkey, pio_action=action, pio_uid=uid, pio_iid=iid, **params) request.set_rfunc(self._auser_action_on_item_resp) self._connection.make_request(request) return request
def _aget_user_itemrank_ranked(self, engine, uid, iids, params={}): """Private function to asynchronously get ranked item for user :param engine: name of the prediction engine. type str. :param uid: user id. type str. :param iids: items to be ranked. type list of item ids. For example, ["i0", "i1", "i2"] :param params: optional parameters. type dictionary For example. { 'pio_attributes' : "name" } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_attributes" in params: params["pio_attributes"] = ",".join(params["pio_attributes"]) pio_iids = ",".join(iids) path = "%s/engines/itemrank/%s/ranked.json" % \ (self.apiversion, engine) request = AsyncRequest("GET", path, pio_appkey=self.appkey, pio_uid=uid, pio_iids=pio_iids, **params) request.set_rfunc(self._aget_user_itemrank_ranked_resp) self._connection.make_request(request) return request
def acreate_item(self, iid, itypes, params={}): """Asynchronously create item. :param iid: item id. type str. :param itypes: item types. Tuple of Str. For example, if this item belongs to item types "t1", "t2", "t3", "t4",then itypes=("t1", "t2", "t3", "t4"). NOTE: if this item belongs to only one itype, use tuple of one element, eg. itypes=("t1",) :param params: optional attributes. type dictionary. For example, { 'custom': 'value', 'pio_inactive' : True, 'pio_latlng': [4.5,67.8] } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ itypes_str = ",".join(itypes) # join items with "," if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_inactive" in params: params["pio_inactive"] = str(params["pio_inactive"]).lower() path = "%s/items.json" % self.apiversion request = AsyncRequest("POST", path, pio_appkey=self.appkey, pio_iid=iid, pio_itypes=itypes_str, **params) request.set_rfunc(self._acreate_item_resp) self._connection.make_request(request) return request
def acreate_user(self, uid, params={}): """Asynchronously create a user. :param uid: user id. type str. :param params: optional attributes. type dictionary. For example, { 'custom': 'value', 'pio_inactive' : True, 'pio_latlng': [4.5,67.8] } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_inactive" in params: params["pio_inactive"] = str(params["pio_inactive"]).lower() path = "%s/users.json" % self.apiversion request = AsyncRequest("POST", path, pio_appkey=self.appkey, pio_uid=uid, **params) request.set_rfunc(self._acreate_user_resp) self._connection.make_request(request) return request
def acreate_item(self, iid, itypes, params={}): """Asynchronously create item. :param iid: item id. type str. :param itypes: item types. Tuple of Str. For example, if this item belongs to item types "t1", "t2", "t3", "t4", then itypes=("t1", "t2", "t3", "t4"). NOTE: if this item belongs to only one itype, use tuple of one element, eg. itypes=("t1",) :param params: optional attributes. type dictionary. For example, { 'custom': 'value', 'pio_inactive' : True, 'pio_latlng': [4.5,67.8] } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ itypes_str = ",".join(itypes) # join items with "," if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_inactive" in params: params["pio_inactive"] = str(params["pio_inactive"]).lower() path = "%s/items.json" % self.apiversion request = AsyncRequest("POST", path, pio_appkey=self.appkey, pio_iid=iid, pio_itypes=itypes_str, **params) request.set_rfunc(self._acreate_item_resp) self._connection.make_request(request) return request
def _aget_user_itemrec_topn(self, uid, n, engine, params={}): """Private function to asynchronously get recommendations for user :param uid: user id. type str. :param n: number of recommendation. type int. :param engine: name of the prediction engine. type str. :param params: optional parameters. type dictionary For example, { 'pio_itypes' : ("t1","t2") } :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if "pio_itypes" in params: params["pio_itypes"] = ",".join(params["pio_itypes"]) if "pio_latlng" in params: params["pio_latlng"] = ",".join(map(str, params["pio_latlng"])) if "pio_attributes" in params: params["pio_attributes"] = ",".join(params["pio_attributes"]) enc_uid = urllib.quote(uid,"") # replace special char with %xx path = "%s/engines/itemrec/%s/topn.json" % (self.apiversion, engine) request = AsyncRequest("GET", path, pio_appkey=self.appkey, pio_uid=enc_uid, pio_n=n, **params) request.set_rfunc(self._aget_user_itemrec_topn_resp) self._connection.make_request(request) return request
def acreate_event(self, event, entity_type, entity_id, target_entity_type=None, target_entity_id=None, properties=None, event_time=None): """Asynchronously create an event. :param event: event name. type str. :param entity_type: entity type. It is the namespace of the entityId and analogous to the table name of a relational database. The entityId must be unique within same entityType. type str. :param entity_id: entity id. *entity_type-entity_id* becomes the unique identifier of the entity. For example, you may have entityType named user, and different entity IDs, say 1 and 2. In this case, user-1 and user-2 uniquely identifies entities. type str :param target_entity_type: target entity type. type str. :param target_entity_id: target entity id. type str. :param properties: a custom dict associated with an event. type dict. :param event_time: the time of the event. type datetime, must contain timezone info. :returns: AsyncRequest object. You can call the get_response() method using this object to get the final resuls or status of this asynchronous request. """ data = { "event": event, "entityType": entity_type, "entityId": entity_id, } if target_entity_type is not None: data["targetEntityType"] = target_entity_type if target_entity_id is not None: data["targetEntityId"] = target_entity_id if properties is not None: data["properties"] = properties et = event_time_validation(event_time) # EventServer uses milliseconds, but python datetime class uses micro. Hence # need to skip the last three digits. et_str = et.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + et.strftime("%z") data["eventTime"] = et_str qparam = {"accessKey": self.access_key} if self.channel is not None: qparam["channel"] = self.channel path = "/events.json?%s" % (urlencode(qparam), ) request = AsyncRequest("POST", path, **data) request.set_rfunc(self._acreate_resp) self._connection.make_request(request) return request
def acreate_event(self, event, entity_type, entity_id, target_entity_type=None, target_entity_id=None, properties=None, event_time=None): """Asynchronously create an event. :param event: event name. type str. :param entity_type: entity type. It is the namespace of the entityId and analogous to the table name of a relational database. The entityId must be unique within same entityType. type str. :param entity_id: entity id. *entity_type-entity_id* becomes the unique identifier of the entity. For example, you may have entityType named user, and different entity IDs, say 1 and 2. In this case, user-1 and user-2 uniquely identifies entities. type str :param target_entity_type: target entity type. type str. :param target_entity_id: target entity id. type str. :param properties: a custom dict associated with an event. type dict. :param event_time: the time of the event. type datetime, must contain timezone info. :returns: AsyncRequest object. You can call the get_response() method using this object to get the final resuls or status of this asynchronous request. """ data = { "event": event, "entityType": entity_type, "entityId": entity_id, } if target_entity_type is not None: data["targetEntityType"] = target_entity_type if target_entity_id is not None: data["targetEntityId"] = target_entity_id if properties is not None: data["properties"] = properties et = event_time_validation(event_time) # EventServer uses milliseconds, but python datetime class uses micro. Hence # need to skip the last three digits. et_str = et.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + et.strftime("%z") data["eventTime"] = et_str qparam = { "accessKey" : self.access_key } if self.channel is not None: qparam["channel"] = self.channel path = "/events.json?%s" % (urlencode(qparam), ) request = AsyncRequest("POST", path, **data) request.set_rfunc(self._acreate_resp) self._connection.make_request(request) return request
def get_status(self): """Get the status of the PredictionIO API Server :returns: status message. :raises: ServerStatusError. """ path = "/" request = AsyncRequest("GET", path) request.set_rfunc(self._aget_resp) self._connection.make_request(request) result = request.get_response() return result
def asend_query(self, data): """Asynchronously send a request to the engine instance with data as the query. :param data: the query: It is coverted to an json object using json.dumps method. type dict. :returns: AsyncRequest object. You can call the get_response() method using this object to get the final resuls or status of this asynchronous request. """ path = "/queries.json" request = AsyncRequest("POST", path, **data) request.set_rfunc(self._aget_resp) self._connection.make_request(request) return request
def adelete_event(self, event_id): """Asynchronouly delete an event from Event Server. :param event_id: event id returned by the EventServer when creating the event. :returns: AsyncRequest object. """ enc_event_id = urllib.quote(event_id, "") # replace special char with %xx path = "/events/%s.json" % enc_event_id request = AsyncRequest("DELETE", path) request.set_rfunc(self._adelete_resp) self._connection.make_request(request) return request
def aget_item(self, iid): """Asynchronously get item :param iid: item id. type str. :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ enc_iid = urllib.quote(iid, "") path = "%s/items/%s.json" % (self.apiversion, enc_iid) request = AsyncRequest("GET", path, pio_appkey=self.appkey) request.set_rfunc(self._aget_item_resp) self._connection.make_request(request) return request
def adelete_user(self, uid): """Asynchronously delete user. :param uid: user id. type str. :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ enc_uid = urllib.quote(uid,"") # replace special char with %xx path = "%s/users/%s.json" % (self.apiversion, enc_uid) request = AsyncRequest("DELETE", path, pio_appkey=self.appkey) request.set_rfunc(self._adelete_user_resp) self._connection.make_request(request) return request
def adelete_event(self, event_id): """Asynchronouly delete an event from Event Server. :param event_id: event id returned by the EventServer when creating the event. :returns: AsyncRequest object. """ qparam = {"accessKey": self.access_key} if self.channel is not None: qparam["channel"] = self.channel enc_event_id = quote(event_id, "") # replace special char with %xx path = "/events/%s.json" % (enc_event_id, ) request = AsyncRequest("DELETE", path, **qparam) request.set_rfunc(self._adelete_resp) self._connection.make_request(request) return request
def acreate_user(self, uid, **params): """Asynchronously create a user. :param uid: user id. type str or int. :param params: keyword arguments for optional attributes. For example, bday="1985-01-23", inactive="true". :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ path = "%s/users.json" % self.apiversion request = AsyncRequest("POST", path, appkey=self.appkey, uid=uid, **params) request.set_rfunc(self._acreate_user_resp) self._connection.make_request(request) return request
def _auser_action_item(self, action, uid, iid, **params): """Asynchronously create an user action on an item :param action: action type. type str. ("like", "dislike", "conversion", "rate", "view") :param uid: user id. type str or int. :param iid: item id. type str or int. :param params: keyword arguments for optional attributes. For example, latlng="123.4, 56.7" For "rate" action, rate param is required. integer value of 1-5 (1 is least preferred and 5 is most preferred) (eg. rate=4) :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ path = "%s/actions/u2i/%s.json" % (self.apiversion, action) request = AsyncRequest("POST", path, appkey=self.appkey, uid=uid, iid=iid, **params) request.set_rfunc(self._au2i_action_resp) self._connection.make_request(request) return request
def acreate_item(self, iid, itypes, **params): """Asynchronously create item. :param iid: item id. type str or int. :param itypes: item types. Tuple of Str. For example, if this item belongs to item types "t1", "t2", "t3", "t4", then itypes=("t1", "t2", "t3", "t4"). NOTE: if this item belongs to only one itype, use tuple of one element, eg. itypes=("t1",) :param params: keyword arguments for optional attributes. For example, latlng="123.4, 56.7". :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ itypes_str = ",".join(map(str, itypes)) # convert to string and join items with "," path = "%s/items.json" % self.apiversion request = AsyncRequest("POST", path, appkey=self.appkey, iid=iid, itypes=itypes_str, **params) request.set_rfunc(self._acreate_item_resp) self._connection.make_request(request) return request
def adelete_event(self, event_id): """Asynchronouly delete an event from Event Server. :param event_id: event id returned by the EventServer when creating the event. :returns: AsyncRequest object. """ qparam = { "accessKey" : self.access_key } if self.channel is not None: qparam["channel"] = self.channel enc_event_id = quote(event_id, "") # replace special char with %xx path = "/events/%s.json" % (enc_event_id, ) request = AsyncRequest("DELETE", path, **qparam) request.set_rfunc(self._adelete_resp) self._connection.make_request(request) return request
def aget_events(self, startTime=None, untilTime=None, entityType=None, entityId=None, limit=None, reversed=False): """Asynchronouly get events from Event Server. (Getting events through the Event Server API is used for debugging and not recommended for production) :param startTime: time in ISO8601 format. Return events with eventTime >= startTime. :param untilTime: time in ISO8601 format. Return events with eventTime < untilTime. :param entityId: String. The entityId. Return events for this entityId only. :param limit: Integer. The number of record events returned. Default is 20. -1 to get all. :param reversed: Boolean. Must be used with both entityType and entityId specified, returns events in reversed chronological order. Default is false. :returns: AsyncRequest object. """ qparam = {"accessKey": self.access_key, "reversed": reversed} if startTime is not None: qparam["startTime"] = startTime if untilTime is not None: qparam["untilTime"] = untilTime if entityType is not None: qparam["entityType"] = entityType if entityId is not None: qparam["entityId"] = entityId if limit is not None: qparam["limit"] = limit if self.channel is not None: qparam["channel"] = self.channel path = "/events.json" request = AsyncRequest("GET", path, **qparam) request.set_rfunc(self._aget_resp) self._connection.make_request(request) return request
def aget_itemrec(self, uid, n, engine, **params): """Asynchronously get recommendations :param uid: user id. type str or int. :param n: number of recommendation. type int. :param engine: name of the prediction engine. type str. :param params: keyword arguments for optional attributes. For example, latlng="123.4, 56.7" :returns: AsyncRequest object. You should call the aresp() method using this AsyncRequest object as argument to get the final result or status of this asynchronous request. """ if type(uid) is int: enc_uid = uid else: enc_uid = urllib.quote(uid,"") # replace special char with %xx path = "%s/engines/itemrec/%s/topn.json" % (self.apiversion, engine) request = AsyncRequest("GET", path, appkey=self.appkey, uid=enc_uid, n=n, **params) request.set_rfunc(self._aget_itemrec_resp) self._connection.make_request(request) return request
def aget_events(self, startTime=None, untilTime=None, entityType=None, entityId=None, limit=None, reversed=False): """Asynchronouly get events from Event Server. (Getting events through the Event Server API is used for debugging and not recommended for production) :param startTime: time in ISO8601 format. Return events with eventTime >= startTime. :param untilTime: time in ISO8601 format. Return events with eventTime < untilTime. :param entityId: String. The entityId. Return events for this entityId only. :param limit: Integer. The number of record events returned. Default is 20. -1 to get all. :param reversed: Boolean. Must be used with both entityType and entityId specified, returns events in reversed chronological order. Default is false. :returns: AsyncRequest object. """ qparam = { "accessKey" : self.access_key, "reversed": reversed } if startTime is not None: qparam["startTime"] = startTime if untilTime is not None: qparam["untilTime"] = untilTime if entityType is not None: qparam["entityType"] = entityType if entityId is not None: qparam["entityId"] = entityId if limit is not None: qparam["limit"] = limit if self.channel is not None: qparam["channel"] = self.channel path = "/events.json" request = AsyncRequest("GET", path, **qparam) request.set_rfunc(self._aget_resp) self._connection.make_request(request) return request