-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathps-client.js
More file actions
211 lines (182 loc) · 5.84 KB
/
Copy pathps-client.js
File metadata and controls
211 lines (182 loc) · 5.84 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 2015 Dato, Inc.
// Predictive Service Client - Javascript
// Licensed under BSD-3
(function (root, factory) {
if(typeof define === "function" && define.amd) {
define([], factory);
} else if(typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.PredictiveServiceClient = factory();
}
}(this, function() {
var request = function(options) {
var url = options.url;
var method = options.method || "GET";
var callback = options.callback || function() {};
var data = options.data;
var timeout = options.timeout || 10000;
var response = "";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(method, url, true);
//set headers
xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
if (options.headers && Object.keys(options.headers).length) {
for (var header in options.headers) {
xmlhttp.setRequestHeader(header, options.headers[header]);
}
}
//set timeout
xmlhttp.timeout = timeout;
xmlhttp.ontimeout = function () { callback(new Error('Request timeout'), null); };
//setup callback
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status == 200) {
try {
response = {'statusCode': xmlhttp.status, 'data': JSON.parse(xmlhttp.responseText)};
}
catch (err) {
callback(new Error('Invalid response: ' + err.message), null);
return;
}
callback(null, response);
} else if (xmlhttp.readyState === 4) {
callback(new Error('Request was not successful.'), response = {'statusCode': xmlhttp.status, 'data': JSON.parse(xmlhttp.responseText)});
}
};
//send data in body
if (Object.keys(data).length > 0) {
try {
xmlhttp.send(JSON.stringify(data) );
} catch(err) {
console.log('Could not send request to Dato Predictive Service.');
}
return;
}
//send empty request
xmlhttp.send();
};
var PredictiveServiceClient = function(endpoint, api_key) {
if ( !(this instanceof PredictiveServiceClient) ) {
return new PredictiveServiceClient(endpoint, api_key);
}
this.endpoint = endpoint;
this.api_key = api_key;
this.schema_version = -1;
this.timeout = 10000; // default to 10 seconds timeout
};
PredictiveServiceClient.prototype.setEndpoint = function(endpoint) {
this.endpoint = endpoint;
};
PredictiveServiceClient.prototype.setApikey = function(api_key) {
this.api_key = api_key;
};
PredictiveServiceClient.prototype.setTimeout = function(timeout) {
this.timeout = timeout;
};
PredictiveServiceClient.prototype.setSchema = function(cb) {
var callback = function(error, resp) {
if (resp == null) {
cb(error);
} else {
if (resp.statusCode != 200) {
cb("Error connecting to Dato Predictive Service %s." % this.end_point);
} else {
if (typeof(resp.data) === "object" && 'schema_version' in resp.data) {
this.schema_version = resp.data.schema_version;
} else {
this.schema_version = 0;
}
cb(null);
}
}
}.bind(this);
var options = {
url: this.endpoint + '/',
method: 'GET',
data: {},
callback: callback
};
request(options);
};
PredictiveServiceClient.prototype.__constructRequestData = function(request_data, request_id) {
var postData = {};
if (this.schema_version < 7) {
postData = { "api key": this.api_key, "data" : {} };
} else {
postData = { "data" : {} };
}
if ('method' in request_data) {
postData.data.method = request_data.method;
if ('data' in request_data) {
postData.data.data = request_data.data;
}
} else {
// handling the case where querying a user defined function instead of a model
postData.data = request_data
}
if (request_id !== null) {
postData.id = request_id;
}
return postData;
};
PredictiveServiceClient.prototype.query = function(po_name, data, callback) {
var schema_callback = function(error) {
if (error != null) {
throw new Error(error);
} else {
this._query(po_name, data, callback);
}
}.bind(this);
if (this.schema_version == -1) {
this.setSchema(schema_callback);
} else {
this._query(po_name, data, callback);
}
};
PredictiveServiceClient.prototype._query = function(po_name, data, callback) {
var postData = this.__constructRequestData(data);
var encodedString = btoa('api_key:' + this.api_key); // IE>=10
var options = {
url: this.endpoint + '/query/' + po_name,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + encodedString
},
data: postData,
callback: callback
};
request(options);
};
PredictiveServiceClient.prototype.feedback = function(request_id, data, callback) {
var schema_callback = function(error) {
if (error != null) {
throw new Error(error);
} else {
this._feedback(request_id, data, callback);
}
}.bind(this);
if (this.schema_version == -1) {
this.setSchema(schema_callback);
} else {
this._feedback(request_id, data, callback);
}
};
PredictiveServiceClient.prototype._feedback = function(request_id, data, callback) {
var postData = this.__constructRequestData(data, request_id);
var encodedString = btoa('api_key:' + this.api_key); // IE>=10
var options = {
url: this.endpoint + '/feedback',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + encodedString
},
data: postData,
callback: callback
};
request(options);
};
return PredictiveServiceClient;
}));