From 4eae9788957221d0c92ce31550ff928ac9bfe256 Mon Sep 17 00:00:00 2001 From: Andrew Mao Date: Sun, 3 Nov 2019 22:39:49 -0500 Subject: [PATCH 1/4] switching sdk --- package.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.js b/package.js index 7c0779a..d2f9534 100644 --- a/package.js +++ b/package.js @@ -6,7 +6,7 @@ Package.describe({ }); Npm.depends({ - "mturk-api": "1.3.2", + "aws-sdk": "2.562.0", jspath: "0.3.2", deepmerge: "0.2.7" // For merging config parameters }); From 4b4b572192673e6ed865b29fafc26339ad6befc8 Mon Sep 17 00:00:00 2001 From: Andrew Mao Date: Sun, 10 Nov 2019 02:12:42 -0500 Subject: [PATCH 2/4] basic test --- admin/admin.js | 2 +- package.js | 6 +++-- server/mturk.js | 68 +++++++------------------------------------------ 3 files changed, 14 insertions(+), 62 deletions(-) diff --git a/admin/admin.js b/admin/admin.js index e46da95..655d53f 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -213,7 +213,7 @@ Meteor.methods({ "ts-admin-account-balance"() { TurkServer.checkAdmin(); try { - return TurkServer.mturk("GetAccountBalance", {}); + return TurkServer.mturk.getAccountBalance(); } catch (e) { throw new Meteor.Error(403, e.toString()); } diff --git a/package.js b/package.js index d2f9534..65de427 100644 --- a/package.js +++ b/package.js @@ -6,8 +6,6 @@ Package.describe({ }); Npm.depends({ - "aws-sdk": "2.562.0", - jspath: "0.3.2", deepmerge: "0.2.7" // For merging config parameters }); @@ -40,6 +38,10 @@ Package.onUse(function(api) { // Non-core packages api.use("aldeed:template-extension@3.4.3"); + // AWS SDK: Mitar's version uses Fibers on the server, which is easier than + // dealing with async callbacks. + api.use("peerlibrary:aws-sdk@2.531.0_1"); + api.use("mizzao:bootboxjs@4.4.0"); api.use("iron:router@1.0.11"); api.use("iron:middleware-stack@1.1.0"); // Fixes route error in Chrome 51+ diff --git a/server/mturk.js b/server/mturk.js index 3248cd7..87ff123 100644 --- a/server/mturk.js +++ b/server/mturk.js @@ -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": - 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 || {}; From 7a1c8b619a240731224ac261c7c662a3ca35bb62 Mon Sep 17 00:00:00 2001 From: Andrew Mao Date: Sun, 10 Nov 2019 02:30:29 -0500 Subject: [PATCH 3/4] whoops --- admin/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/admin.js b/admin/admin.js index 655d53f..c895a3c 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -213,7 +213,7 @@ Meteor.methods({ "ts-admin-account-balance"() { TurkServer.checkAdmin(); try { - return TurkServer.mturk.getAccountBalance(); + return TurkServer.mturk.getAccountBalanceSync().AvailableBalance; } catch (e) { throw new Meteor.Error(403, e.toString()); } From 06a28fe5e130bb1ee976aaebc3246fb79a1bbf8c Mon Sep 17 00:00:00 2001 From: Andrew Mao Date: Sun, 10 Nov 2019 13:07:53 -0500 Subject: [PATCH 4/4] better way to do it --- admin/admin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/admin/admin.js b/admin/admin.js index c895a3c..13b0784 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -213,7 +213,8 @@ Meteor.methods({ "ts-admin-account-balance"() { TurkServer.checkAdmin(); try { - return TurkServer.mturk.getAccountBalanceSync().AvailableBalance; + const { AvailableBalance } = TurkServer.mturk.getAccountBalanceSync(); + return AvailableBalance; } catch (e) { throw new Meteor.Error(403, e.toString()); }