-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector.js
executable file
·131 lines (97 loc) · 3.28 KB
/
connector.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
131
var GOOGLE_AUTH_SCOPES = ['https://www.googleapis.com/auth/devstorage.read_only', 'https://www.googleapis.com/auth/prediction'];
var TokenCache = Npm.require('google-oauth-jwt').TokenCache;
var tokens = new TokenCache();
GooglePrediction = function GooglePrediction(options) {
this._authOptions = {
email: options.serviceEmail,
keyFile: this._assetsFolderAbsolutePath(options.pemFile),
scopes: GOOGLE_AUTH_SCOPES
};
this.projectName = options.projectName;
this._timeoutTreshold = options.timeoutTreshold || 20000;
};
GooglePrediction.prototype.list = function () {
return this._makeRequest('GET', this._baseUrl('list'));
};
GooglePrediction.prototype.insert = function (modelName, bucketName, fileName) {
return this._makeRequest('POST', this._baseUrl(), {
id: modelName,
storageDataLocation: bucketName + "/" + fileName
});
};
GooglePrediction.prototype.get = function (modelName) {
return this._makeRequest('GET', this._baseUrl(modelName));
};
GooglePrediction.prototype.predict = function (modelName, inputDataVector) {
return this._makeRequest('POST', this._baseUrl(modelName, 'predict'), {
"input": {
"csvInstance": inputDataVector
}
});
};
GooglePrediction.prototype.analyze = function (modelName) {
return this._makeRequest('GET', this._baseUrl(modelName, 'analyze'));
};
GooglePrediction.prototype.remove = function (modelName) {
return this._makeRequest('DELETE', this._baseUrl(modelName));
};
//alias to remove
GooglePrediction.prototype['delete'] = function (modelName) {
return this.remove(modelName);
};
GooglePrediction.prototype.update = function (modelName, updateData) {
return this._makeRequest('PUT', this._baseUrl(modelName), updateData);
};
GooglePrediction.prototype._auth = function () {
var syncTokenFn = Meteor.wrapAsync(tokens.get, tokens);
var token = syncTokenFn(this._authOptions);
return {
accessToken: token,
tokenType: 'Bearer'
};
};
GooglePrediction.prototype._makeRequest = function (method, url, data) {
var authCredentials = this._auth();
var result = false;
var timeout = 200;
while (!result) {
try {
result = HTTP.call(method, url, {
headers: {
"Content-Type": "application/json",
"Authorization": authCredentials.tokenType + ' ' + authCredentials.accessToken
},
data: data
});
} catch (err) {
console.error('Google Prediction API Error:', err.response.statusCode);
if (timeout > this._timeoutTreshold) {
throw err;
} else {
timeout *= 2;
}
} finally {
Meteor._sleepForMs(timeout);
}
}
return result.data;
};
GooglePrediction.prototype._baseUrl = function (/* arguments */) {
var suffix = '';
Array.prototype.forEach.call(arguments, function (argument) {
if (argument) {
suffix += '/' + argument;
}
});
return 'https://www.googleapis.com/prediction/v1.6/projects/' + this.projectName + '/trainedmodels' + suffix;
};
GooglePrediction.prototype._assetsFolderAbsolutePath = function (fileName) {
var fs = Npm.require('fs');
var path = Npm.require('path');
var meteorRoot = fs.realpathSync(process.cwd() + '/../');
var assetsFolder = meteorRoot + '/server/assets/app';
if (fileName) {
assetsFolder += '/' + fileName;
}
return assetsFolder;
};