Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Change prod to v4 challenge api and m3m token except close challenge #40

Merged
merged 1 commit into from
Dec 13, 2019
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"homepage": "https://gitlab.com/luettich/processor#README",
"dependencies": {
"@topcoder-platform/topcoder-api-challenges-v4-wrapper": "^1.0.5",
"@topcoder-platform/topcoder-api-challenges-v4-wrapper-dev": "^1.0.5",
"axios": "^0.19.0",
"circular-json": "^0.5.7",
Expand Down
54 changes: 30 additions & 24 deletions utils/topcoder-api-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const _ = require('lodash');
const moment = require('moment');
const circularJSON = require('circular-json');

// const m2mAuth = require('tc-core-library-js').auth.m2m;
const m2mAuth = require('tc-core-library-js').auth.m2m;

// const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']));
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']));

let topcoderApiProjects = require('topcoder-api-projects');
let topcoderApiChallenges = require('topcoder-api-challenges');
let topcoderApiChallenges = require('@topcoder-platform/topcoder-api-challenges-v4-wrapper');
let topcoderApiChallengesV3 = require('topcoder-api-challenges');

const topcoderDevApiProjects = require('topcoder-dev-api-projects');
const topcoderDevApiChallenges = require('@topcoder-platform/topcoder-api-challenges-v4-wrapper-dev');
Expand All @@ -45,6 +46,7 @@ let cachedAccessToken;
// Init the API instances
const projectsClient = topcoderApiProjects.ApiClient.instance;
const challengesClient = topcoderApiChallenges.ApiClient.instance;
const challengesClientV3 = topcoderApiChallengesV3.ApiClient.instance;

// Timeout increase to 5 minutes
challengesClient.timeout = 300000;
Expand Down Expand Up @@ -101,21 +103,21 @@ async function getAccessToken() {
return cachedAccessToken;
}

// /**
// * Function to get M2M token
// * @returns {Promise} The promised token
// */
// async function getM2Mtoken() {
// return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET);
// }
/**
* Function to get M2M token
* @returns {Promise} The promised token
*/
async function getM2Mtoken() {
return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET);
}

/**
* Create a new project.
* @param {String} projectName the project name
* @returns {Number} the created project id
*/
async function createProject(projectName) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
// eslint-disable-next-line new-cap
const projectBody = new topcoderApiProjects.ProjectRequestBody.constructFromObject({
projectName
Expand Down Expand Up @@ -147,7 +149,7 @@ async function createProject(projectName) {
* @returns {Number} the created challenge id
*/
async function createChallenge(challenge) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
const start = new Date();
const startTime = moment(start).toISOString();
const end = moment(start).add(config.NEW_CHALLENGE_DURATION_IN_DAYS, 'days').toISOString();
Expand Down Expand Up @@ -188,7 +190,7 @@ async function createChallenge(challenge) {
* @param {Object} challenge the challenge to update
*/
async function updateChallenge(id, challenge) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`Updating challenge ${id} with ${circularJSON.stringify(challenge)}`);
// eslint-disable-next-line new-cap
const challengeBody = new topcoderApiChallenges.UpdateChallengeBodyParam.constructFromObject({
Expand Down Expand Up @@ -226,7 +228,7 @@ async function updateChallenge(id, challenge) {
* @param {Number} id the challenge id
*/
async function activateChallenge(id) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`Activating challenge ${id}`);
try {
const response = await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -266,7 +268,7 @@ async function getChallengeById(id) {
if (!_.isNumber(id)) {
throw new Error('The challenge id must valid number');
}
const apiKey = await getAccessToken();
const apiKey = await getM2Mtoken();
logger.debug('Getting topcoder challenge details');
try {
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}`, {
Expand Down Expand Up @@ -300,7 +302,11 @@ async function closeChallenge(id, winnerId) {
const apiKey = await getAccessToken();
logger.debug(`Closing challenge ${id}`);
try {
const response = await axios.post(`${challengesClient.basePath}/challenges/${id}/close?winnerId=${winnerId}`, null, {
let basePath = challengesClient.basePath;
if (!config.TC_DEV_ENV) {
basePath = challengesClientV3.basePath;
}
const response = await axios.post(`${basePath}/challenges/${id}/close?winnerId=${winnerId}`, null, {
headers: {
authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json'
Expand All @@ -327,7 +333,7 @@ async function closeChallenge(id, winnerId) {
* @returns {Number} the billing account id
*/
async function getProjectBillingAccountId(id) {
const apiKey = await getAccessToken();
const apiKey = await getM2Mtoken();
logger.debug(`Getting project billing detail ${id}`);
try {
const response = await axios.get(`${projectsClient.basePath}/direct/projects/${id}`, {
Expand Down Expand Up @@ -359,7 +365,7 @@ async function getProjectBillingAccountId(id) {
* @returns {Number} the user id
*/
async function getTopcoderMemberId(handle) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
try {
const response = await axios.get(`${projectsClient.basePath}/members/${handle}`);
const statusCode = response ? response.status : null;
Expand All @@ -378,7 +384,7 @@ async function getTopcoderMemberId(handle) {
* @param {Object} resource the resource resource to add
*/
async function addResourceToChallenge(id, resource) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`adding resource to challenge ${id}`);
try {
const response = await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -419,7 +425,7 @@ async function getResourcesFromChallenge(id) {
if (!_.isNumber(id)) {
throw new Error('The challenge id must valid number');
}
const apiKey = await getAccessToken();
const apiKey = await getM2Mtoken();
logger.debug(`fetch resource from challenge ${id}`);
try {
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}/resources`, {
Expand Down Expand Up @@ -464,7 +470,7 @@ async function roleAlreadySet(id, role) {
* @param {Object} resource the resource resource to remove
*/
async function unregisterUserFromChallenge(id) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`removing resource from challenge ${id}`);
try {
const response = await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -501,7 +507,7 @@ async function unregisterUserFromChallenge(id) {
* @param {Number} id the challenge id
*/
async function cancelPrivateContent(id) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`Cancelling challenge ${id}`);
try {
const response = await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -549,7 +555,7 @@ async function assignUserAsRegistrant(topcoderUserId, challengeId) {
* @param {Object} resource the resource resource to remove
*/
async function removeResourceToChallenge(id, resource) {
bearer.apiKey = await getAccessToken();
bearer.apiKey = await getM2Mtoken();
logger.debug(`removing resource from challenge ${id}`);
try {
const response = await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -579,7 +585,7 @@ async function removeResourceToChallenge(id, resource) {
* @returns {Array} the resources of challenge
*/
async function getChallengeResources(id) {
const apiKey = await getAccessToken();
const apiKey = await getM2Mtoken();
logger.debug(`getting resource from challenge ${id}`);
try {
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}/resources`, {
Expand Down