forked from ezsystems/EzSystemsRecommendationBundle
/
recommendationrestclient.js
130 lines (112 loc) · 4.05 KB
/
recommendationrestclient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
(function (global, doc) {
var eZ = global.eZ = global.eZ || {};
/**
* YooChoose recommender REST client.
*
* @class RecommendationRestClient
* @param {Object} config user settings
*/
eZ.RecommendationRestClient = function (config) {
this.endpointUrl = config.endpointUrl || '';
this.fields = config.fields || [];
this.scenario = config.scenario || '';
this.limit = config.limit || 0;
this.language = config.language || '';
this.fields = config.fields || [];
this.contentType = config.contentType || '';
this.outputType = config.outputType || '';
this.contextItems = config.contextItems || '';
this.categoryPath = config.categoryPath || '';
this.errorMessage = config.errorMessage || 'Error occurred while loading recommendations';
this.notSupportedMessage = config.notSupportedMessage || 'Cannot display recommendations, this browser is not supported';
this.unauthorizedMessage = config.unauthorizedMessage || 'Unauthorized access';
};
/**
* Requests recommendations from recommender engine.
*
* @param {RecommendationRestClient~onSuccess} responseCallback
* @param {RecommendationRestClient~onFailure} errorCallback
*/
eZ.RecommendationRestClient.prototype.fetchRecommendations = function (responseCallback, errorCallback) {
var xmlhttp = eZ.RecommendationRestClient.getXMLHttpRequest(),
attributes = '',
requestQueryString;
if (xmlhttp === null) {
errorCallback(this.notSupportedMessage);
return;
}
xmlhttp.onreadystatechange = function () {
var jsonResponse;
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
jsonResponse = JSON.parse(xmlhttp.response);
responseCallback(jsonResponse.recommendationResponseList, this);
} else if (xmlhttp.status == 401) {
errorCallback(this.unauthorizedMessage);
} else {
errorCallback(this.errorMessage);
}
}
};
for (var i = 0; i < this.fields.length; i++) {
attributes = attributes + '&attribute=' + this.fields[i];
}
requestQueryString = [
this.endpointUrl,
this.scenario,
'.json?numrecs=', this.limit,
'&contextitems=', this.contextItems,
'&contenttype=', this.contentType,
'&outputtype=', this.outputType,
'&categorypath=', encodeURIComponent(this.categoryPath),
'&lang=', this.language,
attributes
];
xmlhttp.open('GET', requestQueryString.join(''), true);
xmlhttp.send();
};
/**
* Sends notification ping.
*
* @static
* @method ping
* @param {String} url
*/
eZ.RecommendationRestClient.ping = function (url) {
var xmlhttp = eZ.RecommendationRestClient.getXMLHttpRequest();
if (!xmlhttp) {
return true;
}
xmlhttp.open('GET', url, false);
xmlhttp.send();
return true;
};
/**
* Returns available XMLHttpRequest object (depending on the browser).
*
* @static
* @method getXMLHttpRequest
* @returns {Object} XMLHttpRequest
*/
eZ.RecommendationRestClient.getXMLHttpRequest = function () {
var xmlHttp;
if (global.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
xmlHttp = null;
}
}
}
return xmlHttp;
};
})(window, document);