forked from JoseBarrios/api-mturk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
238 lines (202 loc) · 8.68 KB
/
index.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
////////////////////////////////////////////////////
// Imports
////////////////////////////////////////////////////
var PromiseThrottle = require('promise-throttle');
var parseXML = require('xml2js').parseString;
var querystring = require('querystring');
var CryptoJS = require('crypto-js');
var Promise = require('promise');
const https = require('https');
var _ = require('lodash');
var PRODUCTION = 'mechanicalturk.amazonaws.com';
var SANDBOX = 'mechanicalturk.sandbox.amazonaws.com';
var SERVICE = 'AWSMechanicalTurkRequester';
var VERSION = '2014-08-15';
var METHOD = 'POST';
var specialResponses = {};
specialResponses['CreateQualificationType'] = 'QualificationType';
specialResponses['CreateHIT'] = 'HIT';
specialResponses['GetHIT'] = 'HIT';
//Legacy support
var npmPackageVersion = 2; //Latest by default
var promiseThrottle = new PromiseThrottle({
requestsPerSecond: 3, // up to 1 request per second
promiseImplementation: Promise // the Promise library you are using
});
function MTurkAPI() {
var api = this;
//Legacy support for V1 methods (temporary)
api.connect = function(options){
return new Promise(function(resolve, reject){
var client = api.createClient(options);
var error = new Error('Could not initialize client. Please notify the mantainer (ERR1-3-5L)');
if(client){
resolve(client);
npmPackageVersion = 1;
} else { reject(error); }
})
};
api.createClient = function(options) {
var client = {};
// add some items to the queue
client.req = function (operation, params) {
return new Promise(function(resolve, reject){
promiseThrottle.add(throttledRequest.bind(this, client, options, operation, params))
.then(resolve)
.catch(reject);
})
};
//TODO: Improve method by using JSON schemas
// for error checking
//Checks for two types of errors:
//OPERATION ERR: (wrong API keys, etc) which are located in the OPERATION response group
//RESULTS ERR: (wrong params, etc) which are located in the RESULT response group
client.isValidResponse = function(response){
var result = {};
result.errorType = [];
result.errorMessage = null;
result.isValid = null;
var responseKey = Object.keys(response)[0];
var charIndex = responseKey.indexOf("Response");
var operationName = responseKey.slice(0, charIndex)
//OPERATION ERROR CHECK
var responseProperty = operationName + 'Response';
var hasResponse = response.hasOwnProperty(responseProperty);
var responseGroup = hasResponse? response[operationName + 'Response'] : null;
var operationRequest = responseGroup? responseGroup.OperationRequest[0] : null;
var operationErrors = operationRequest? operationRequest.hasOwnProperty('Errors') : null;
var operationError = operationErrors? true : false;
var error = operationError? operationRequest.Errors[0].Error[0]: null;
var errCode = error? error.Code[0] : '';
var errMsg = error? error.Message[0] : '';
var errKey = error && error.hasOwnProperty('Data')? error.Data[0].Key : '';
var errVal = error && error.hasOwnProperty('Data')? error.Data[0].Value : '';
var errData = errKey && errVal? errKey +':'+errVal : '';
result.errMessage = error? errCode +' - '+ errMsg + ' - '+ errData: '';
result.isValid = error? false : true;
result.errData = errData;
result.errVal = errVal[0];
result.errKey = errKey[0];
result.errCode = errCode;
if(error){return result}
//RESULT ERROR CHECK
var specialCase = specialResponses.hasOwnProperty(operationName);
var resultProperty = specialCase ? specialResponses[operationName] : operationName + 'Result';
var hasResult = responseGroup.hasOwnProperty(resultProperty);
var requestResult = hasResult? responseGroup[resultProperty][0].Request[0] : null;
var hasValidity = requestResult? requestResult.hasOwnProperty('IsValid'): null;
var validResult = hasValidity? requestResult.IsValid[0] === 'True': false;
var resultError = hasResult && !validResult;
var error = resultError? requestResult.Errors[0].Error[0]: null;
var errCode = error? error.Code[0] : '';
var errMsg = error? error.Message[0] : '';
var errKey = error && error.hasOwnProperty('Data')? error.Data[0].Key : '';
var errVal = error && error.hasOwnProperty('Data')? error.Data[0].Value : '';
var errData = errKey && errVal? errKey +':'+errVal : '';
result.errMessage = error? errCode +' - '+ errMsg + ' - '+ errData : '';
result.isValid = error? false : true;
result.errData = errData;
result.errVal = errVal[0];
result.errKey = errKey[0];
result.errCode = errCode;
//Returns object
// with various props
// isValid, errMessage, etc
return result;
}
return client;
}
return api;
}
function restify(params){
_.forOwn(params, function(value, key, obj) {
if(_.isArray(value)){
_.forEach(value, function(item, index){
_.forEach(item, function(value, subkey){
params[key+'.'+index+'.'+subkey] = value;
});
});
}
if(_.isPlainObject(value)){
var subkeys = _.keys(value);
_.forEach(subkeys, function(subkey){
var path = key +'.1.'+subkey;
params[path] = value[subkey];
})
}
});
return params;
}
function throttledRequest(client, options, operation, params){
var params = params || {};
params = restify(params);
params.RequestGroup = params.RequestGroup || 'Request, Minimal';
params.Operation = operation;
params.Version = VERSION;
return new Promise(function(resolve, reject){
var signedRequest = signRequest(options, params);
signedRequest = querystring.stringify(signedRequest);
var payloadSize = Buffer.byteLength(signedRequest);
var reqOptions = {};
reqOptions.host = options.sandbox? SANDBOX : PRODUCTION;
reqOptions.method= METHOD;
reqOptions.headers = {};
reqOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded';
reqOptions.headers['Content-Length'] = payloadSize;
var req = https.request(reqOptions, function(res) {
if(res.statusCode !== 200){
var error = new Error(res.statusCode +' - '+ res.statusMessage)
reject(error);
}
var xmlBuffer = new Buffer([]);
res.on('data', function(chunk) {
xmlBuffer = Buffer.concat([xmlBuffer, chunk]);
})
res.on('end', function(){
var xmlString = xmlBuffer.toString('utf-8');
convertXMLToJSObject(xmlString).then(function(response){
var test = client.isValidResponse(response);
if(test.isValid){
response = formatResponse(npmPackageVersion, response)
resolve(response);
} else {
var customError = new Error();
customError.message = test.errMessage;
reject(customError);
}
}).catch(reject);
})
res.on('error', reject)
});
req.write(signedRequest);
req.end();
})
};
function formatResponse(version, res){
switch( version ){
case 1:
var keys = Object.keys(res);
var rootKey = keys[0];
return res[rootKey];
break;
default:
return res;
}
}
function convertXMLToJSObject(xml){
return new Promise(function(resolve, reject){
parseXML(xml, function (err, response) {
err? reject(err) : resolve(response);
});
})
};
function signRequest(credentials, params){
params.AWSAccessKeyId= credentials.access;
params.Timestamp = new Date().toISOString();
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA1, credentials.secret);
hmac.update(SERVICE + params.Operation + params.Timestamp);
params.Signature = hmac.finalize().toString(CryptoJS.enc.Base64);
return params;
}
//EXPORT
module.exports = new MTurkAPI();