-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
143 lines (122 loc) · 3.98 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
const transformer = require("./transformer/2014-2017");
const PromiseThrottle = require("promise-throttle");
const https = require("https");
const aws4 = require("aws4");
class MTurkAPI {
//Backwards compatibility
static createClient(config){
return new Promise((resolve) => {
const mturk = new MTurkAPI(config);
mturk.legacySupportEnabled = true;
resolve(mturk);
})
}
constructor(config) {
this.region = config.region || null;
this.sandbox = config.sandbox || null;
this.accessKeyId = config.accessKeyId || null;
this.secretAccessKey = config.secretAccessKey || null;
this.legacySupportEnabled = false;
this.requests = new PromiseThrottle({
requestsPerSecond: 2, // up to 2 requests per second
promiseImplementation: Promise // the Promise library you are using
});
};
get host() {
return `mturk-requester${this.sandbox? "-sandbox" : ""}.${this.region}.amazonaws.com`;
}
get service() {
return "mturk-requester";
}
get contentType() {
return "application/x-amz-json-1.1";
}
get path() {
return "/";
}
get credentials() {
return {
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey
}
}
getRequestHeaders(operation){
return {
"Host": this.host,
"Content-Type": this.contentType,
"X-Amz-Target": this.xAmzTarget(operation),
}
}
getRequestOptions(headers, body){
return {
service: this.service,
region: this.region,
path: this.path,
body,
headers
};
}
xAmzTarget(operation) {
return `MTurkRequesterServiceV20170117.${operation}`;
}
async req(operation, params, shouldSkipUndeprecator) {
const transformerInfo = transformer.get(operation);
if(transformerInfo && this.legacySupportEnabled && !shouldSkipUndeprecator){
return await this.undeprecator(operation, params, transformerInfo)
}
return new Promise((resolve, reject) => {
let body = params || {};
body = JSON.stringify(body);
const headers = this.getRequestHeaders(operation)
const options = this.getRequestOptions(headers, body )
aws4.sign(options, this.credentials);
const requestPromise = this.response.bind(this, options, body);
this.requests.add(requestPromise)
.then(resolve)
.catch(reject);
})
}
async undeprecator(operation, params, transformerInfo){
const updatedOperation = transformerInfo.updateOperation;
const updatedParams = await transformerInfo.updateParams(params, this);
const response = await this.req(updatedOperation, updatedParams, true);
return transformerInfo.updateResponse(response, updatedParams, this);
}
getErrorMessage(response){
const header = response.hasOwnProperty("__type")? `${response.__type} -` : "";
const code = response.hasOwnProperty("TurkErrorCode")? `${response.TurkErrorCode}.` : "";
const message = response.hasOwnProperty("Message")? `${response.Message}:` : "Invalid Request";
return `${header} ${code} ${message}`
}
isValid(response){
const isPrivateType = response.hasOwnProperty("__type");
const hasMessage = response.hasOwnProperty("Message");
const hasCode = response.hasOwnProperty("TurkErrorCode");
const isValid = !isPrivateType && !hasMessage && !hasCode;
return isValid;
}
async response(options, body) {
body = body || "{}";
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", ()=> {
let response = Buffer.concat(chunks).toString();
response = JSON.parse(response);
if(this.isValid(response)) {
resolve(response);
} else {
throw new Error(this.getErrorMessage(response));
}
});
}).on("error", reject);
req.write(body);
req.end();
})
}
}
//EXPORT
module.exports = MTurkAPI;