using Javascript from the client is a problem because you will make it possible for some malicious agent to see your access key by examining your client code. Although PIO support SSL it does not have an authentication mechanism so a malicious agent could use this access key to screw up your data.

It is only safe to have PredictionIO accessed from a trusted application server, not a client. So though there may be android (Java), iOS, and Javascript SDKs please be aware of the security implications of connecting from mobile devices or browsers.



On Apr 10, 2017, at 5:43 AM, Mohamed Zouga <mohamed@wizacha.com> wrote:

This code seems as a good start indeed, is this somewhere on GitHub ? so i could put some additional stuff or even some modifications !

2017-04-10 14:32 GMT+02:00 Gustavo Frederico <gustavo.frederico@thinkwrap.com>:
You can use this as a starting point.

Gustavo


var _recommendationInner = {
    baseAjaxPromise: function (theData, url) {
        return new Promise(function (resolve, reject) {
            $.ajax({
                type: 'POST',
                url: url,
                data: JSON.stringify(theData),
                contentType: 'text/plain',
                xhrFields: {
                    withCredentials: false
                },
                dataType: "json",
                success: function(data) {
                    resolve(data);
                },
                failure: function(errorMsg) {
                    console.error('Error ' + errorMsg);
                    reject();
                }
            })
                .done(function(data) {
                    resolve(data);
                })
                .fail(function(jqXHR, textStatus) {
                    console.error("error : " + textStatus);
                    console.error("error status text: " + jqXHR.statusText);
                    console.error("error status: " + jqXHR.status);
                    reject();
                });
        });
    }

};

var Recommendations = new function() {

    this.set = function(accessKey, eventsUrl, queriesUrl) {
        Recommendations.eventsUrl = eventsUrl + '/events.json?accessKey=' + accessKey;
        Recommendations.queriesUrl = queriesUrl + '/queries.json?accessKey=' + accessKey;
    };

    /** Events **/
    var viewProductPromise = function(user,product) {
        var data = {
            "event": 'view',
            "entityType": "user",
            "entityId": user,
            "targetEntityType": "item",
            "targetEntityId": product,
            "eventTime" : new Date().toISOString()
        };
        return _recommendationInner.baseAjaxPromise(data, Recommendations.eventsUrl);
    };

    var purchaseProductPromise = function (user,product) {
        var data = {
            "event": "purchase",
            "entityType": "user",
            "entityId": user,
            "targetEntityType": "item",
            "targetEntityId": product,
            "eventTime" : new Date().toISOString()
        };
        return _recommendationInner.baseAjaxPromise(data, Recommendations.eventsUrl);
    };

    /** Queries **/
    var getGeneralRecommendationsPromise = function (number) {
        var data = {
            "num" : number
        };
        return _recommendationInner.baseAjaxPromise(data, Recommendations.queriesUrl);
    };

    var getRecommendationsForUserPromise = function (user) {
        var data = {
            "user" : user
        };
        return _recommendationInner.baseAjaxPromise(data, Recommendations.queriesUrl);
    };

    var getRecommendationsPromise = function (user,contextProduct,category) {
        var fields = [];

        if (category) {
            fields.push({
                "name": "category",
                "values": [category],
                "bias": -1
            });
        }

        var data = {
            "user" : user,
            "item" :  contextProduct,
            "fields" : fields
        };
        return _recommendationInner.baseAjaxPromise(data, Recommendations.queriesUrl);
    };


    /** Events **/
    this.purchase = function (user,product,quantity,amount) {
        return purchaseProductPromise(user,product,quantity,amount)
            .then(nil => { return nil; })
            .catch(error => { throw error; });
    };

    this.viewProduct = function (user,product, isStrong = false) {
        return viewProductPromise(user, product, isStrong);
    };

    /** Queries **/
    this.getGeneralRecommendations = function (number) {
        return getGeneralRecommendationsPromise(number);
    };

    this.getRecommendationsForUser = function (user) {
        return getRecommendationsForUserPromise(user);
    };

    this.getRecommendations = function (user,contextProduct,category) {
        return getRecommendationsPromise(user,contextProduct,category);
    };

};


On Mon, Apr 10, 2017 at 8:31 AM, Vaghawan Ojha <vaghawan781@gmail.com> wrote:
Hi, 

Ok, in  that case I am currently not aware of any js SDK like you wanted. Someone else may help. 

Thanks

On Mon, Apr 10, 2017 at 6:14 PM, Mohamed Zouga <mohamed@wizacha.com> wrote:
@vaghawan : i don't want to go posting my accessKey allover my requests and writing verbose code when all i might use is a function with one parameter or so.
@ Marius : Why have 3 dependencies (3 more files, supposing those files don't have additional dependencies...) when i can use just Ajax to send my events and requests 
so any JS SDK out there ? 

2017-04-10 14:23 GMT+02:00 Marius Rabenarivo <mariusrabenarivo@gmail.com>:
Hello,

The NodeJS SDK has the following dependencies :

lodash
request
bluebird

You can download them if you want.

Regards,

Marius

2017-04-10 16:18 GMT+04:00 Mohamed Zouga <mohamed@wizacha.com>:
I'am using PredictionIO in a web site, and i want to send the event using Javascript (instead of PHP) is there any know SDK JS for PredictionIO (lastest) ? 
i found some outdated ones in Github, i didn't want to rush into coding one from scratch until i'am sure there is no existing ones, thank you  
P.S : I know there is a NodeJS SDK, i don't have npm and i don't want any dependencies.
--
Mohamed ZOUGA
Stagiaire Data Scientist, Wizaplace
https://www.linkedin.com/in/zouga-mohamed-44b02974/




--
Mohamed ZOUGA
Stagiaire Data Scientist, Wizaplace
https://www.linkedin.com/in/zouga-mohamed-44b02974/



--
You received this message because you are subscribed to the Google Groups "actionml-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actionml-user+unsubscribe@googlegroups.com.
To post to this group, send email to actionml-user@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/actionml-user/CAGRFSSOcpSctEN5up894VAbG_qorUOpUFE5FtA9ZTLFMdyXwGA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Mohamed ZOUGA
Stagiaire Data Scientist, Wizaplace
https://www.linkedin.com/in/zouga-mohamed-44b02974/

--
You received this message because you are subscribed to the Google Groups "actionml-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actionml-user+unsubscribe@googlegroups.com.
To post to this group, send email to actionml-user@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/actionml-user/CAF%3DHCxR5nr78%3D5%3DwQFpajT%2BOx4XDxJJpf544NU3A%2B8A%3Dvv4c5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.