Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update to official Amazon SDK #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ Meteor.methods({
"ts-admin-account-balance"() {
TurkServer.checkAdmin();
try {
return TurkServer.mturk("GetAccountBalance", {});
const { AvailableBalance } = TurkServer.mturk.getAccountBalanceSync();
return AvailableBalance;
} catch (e) {
throw new Meteor.Error(403, e.toString());
}
Expand Down
6 changes: 4 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Package.describe({
});

Npm.depends({
"mturk-api": "1.3.2",
jspath: "0.3.2",
deepmerge: "0.2.7" // For merging config parameters
});

Expand Down Expand Up @@ -40,6 +38,10 @@ Package.onUse(function(api) {
// Non-core packages
api.use("aldeed:[email protected]");

// AWS SDK: Mitar's version uses Fibers on the server, which is easier than
// dealing with async callbacks.
api.use("peerlibrary:[email protected]_1");

api.use("mizzao:[email protected]");
api.use("iron:[email protected]");
api.use("iron:[email protected]"); // Fixes route error in Chrome 51+
Expand Down
68 changes: 9 additions & 59 deletions server/mturk.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,18 @@
const mturk = Npm.require("mturk-api");
const JSPath = Npm.require("jspath");

let api = undefined;

if (!TurkServer.config.mturk.accessKeyId || !TurkServer.config.mturk.secretAccessKey) {
Meteor._debug("Missing Amazon API keys for connecting to MTurk. Please configure.");
} else {
const config = {
access: TurkServer.config.mturk.accessKeyId,
secret: TurkServer.config.mturk.secretAccessKey,
sandbox: TurkServer.config.mturk.sandbox
AWS.config = {
accessKeyId: TurkServer.config.mturk.accessKeyId,
secretAccessKey: TurkServer.config.mturk.secretAccessKey,
region: "us-east-1",
sslEnabled: "true"
};

const promise = mturk
.connect(config)
.then(api => api)
.catch(console.error);
api = Promise.resolve(promise).await();
}

TurkServer.mturk = function(op, params) {
if (!api) {
console.log("Ignoring operation " + op + " because MTurk is not configured.");
return;
}

const promise = api.req(op, params).then(resp => resp);
const result = Promise.resolve(promise).await();

return transform(op, result);
};

/*
Translate results to be a little more similar to the original code:
https://github.com/jefftimesten/mturk/blob/master/index.js

Docs at https://github.com/dfilatov/jspath:
expressions always return an array;
with [0] at the end return the first match.

XXX we may not necessarily want to continue using these in the future.
This is just for compatibility with what the previous API returned.
*/
function transform(op, result) {
switch (op) {
case "CreateHIT":
return JSPath.apply("..HITId[0]", result);
case "GetAccountBalance":
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old AWS SDK used to be all XML-based, which means we have this ugly parser stuff. We can remove all that now and access the JSON data directly, but note that this will have to be added to each TurkServer.mturk call as in the example code above.

return JSPath.apply("..GetAccountBalanceResult.AvailableBalance.Amount[0]", result);
case "GetAssignment":
return JSPath.apply("..Assignment[0]", result);
case "GetAssignmentsForHIT":
return JSPath.apply("..GetAssignmentsForHITResult", result);
case "GetHIT":
return JSPath.apply("..HIT[0]", result);
case "GetReviewableHITs":
return JSPath.apply("..GetReviewableHITsResult", result);
case "RegisterHITType":
return JSPath.apply("..HITTypeId[0]", result);
case "SearchHITs":
return JSPath.apply("..SearchHITsResult", result);
}
const endpoint = TurkServer.config.mturk.sandbox
? "https://mturk-requester-sandbox.us-east-1.amazonaws.com"
: "https://mturk-requester.us-east-1.amazonaws.com";

return result;
TurkServer.mturk = new AWS.MTurk({ endpoint });
}

TurkServer.Util = TurkServer.Util || {};
Expand Down