| @@ 9-120 (lines=112) @@ | ||
| 6 | * API for Codingame's services |
|
| 7 | * @module codingame-api |
|
| 8 | */ |
|
| 9 | import request from 'request'; |
|
| 10 | ||
| 11 | /** |
|
| 12 | * Codingame's API to log in |
|
| 13 | * |
|
| 14 | * @name login |
|
| 15 | * @function |
|
| 16 | * @param {string} username The login to authenticate |
|
| 17 | * @param {string} password The password to authenticate |
|
| 18 | * @returns {Promise<Object>} Body of the response |
|
| 19 | * @memberof module:codingame-api |
|
| 20 | * @instance |
|
| 21 | */ |
|
| 22 | let login = function login(username, password) { |
|
| 23 | return new Promise(function(resolve, reject) { |
|
| 24 | let options = { |
|
| 25 | "method": `POST`, |
|
| 26 | "baseUrl": `https://www.codingame.com`, |
|
| 27 | "uri": `/services/CodingamerRemoteService/loginSiteV2`, |
|
| 28 | "headers": { |
|
| 29 | "Accept": `application/json`, |
|
| 30 | "Content-Type": `application/json;charset=utf-8` |
|
| 31 | }, |
|
| 32 | "body": [username, password, true], |
|
| 33 | "jar": true, |
|
| 34 | "json": true |
|
| 35 | }; |
|
| 36 | request(options, function(error, response, body) { |
|
| 37 | if (response.statusCode >= 400) { |
|
| 38 | reject(error); |
|
| 39 | } else { |
|
| 40 | resolve(body); |
|
| 41 | } |
|
| 42 | }); |
|
| 43 | }); |
|
| 44 | }; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Codingame's API for launching a test |
|
| 48 | * |
|
| 49 | * @name test |
|
| 50 | * @function |
|
| 51 | * @param {string} exercise Hash of the exercise to test |
|
| 52 | * @param {number} test Test's number |
|
| 53 | * @param {string} language Language of the program to send |
|
| 54 | * @param {string} bundle Content of the program to send |
|
| 55 | * @returns {Promise<Object>} The result only if request succeed and test passed |
|
| 56 | * @memberof module:codingame-api |
|
| 57 | * @instance |
|
| 58 | */ |
|
| 59 | let test = function test(exercise, test, language, bundle) { |
|
| 60 | return new Promise(function(resolve, reject) { |
|
| 61 | let options = { |
|
| 62 | "method": `POST`, |
|
| 63 | "baseUrl": `https://www.codingame.com`, |
|
| 64 | "uri": `/services/TestSessionRemoteService/play`, |
|
| 65 | "headers": { |
|
| 66 | "Accept": `application/json`, |
|
| 67 | "Content-Type": `application/json;charset=utf-8` |
|
| 68 | }, |
|
| 69 | "body": [ |
|
| 70 | exercise, |
|
| 71 | { |
|
| 72 | "code": bundle, |
|
| 73 | "programmingLanguageId": language, |
|
| 74 | "multipleLanguages": { |
|
| 75 | "testIndex": test |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ], |
|
| 79 | "jar": true, |
|
| 80 | "json": true |
|
| 81 | }; |
|
| 82 | request(options, function(error, response, body) { |
|
| 83 | if (response.statusCode >= 400) { |
|
| 84 | reject(error); |
|
| 85 | } else { |
|
| 86 | let meta = { |
|
| 87 | "exercise": exercise, |
|
| 88 | "test": test, |
|
| 89 | "language": language, |
|
| 90 | "bundle": bundle |
|
| 91 | }; |
|
| 92 | let error = new Error(`Codingame may have changed its API, contact owner of this application.`); |
|
| 93 | Object.assign(error, meta); |
|
| 94 | if (body.success !== undefined) { |
|
| 95 | if (body.success.comparison !== undefined) { |
|
| 96 | if(body.success.comparison.success) { |
|
| 97 | Object.assign(body.success, meta); |
|
| 98 | resolve(body.success); |
|
| 99 | } else { |
|
| 100 | let error = new Error(`Expected <${body.success.comparison.expected}> but found <${body.success.comparison.found}>`); |
|
| 101 | Object.assign(error, meta); |
|
| 102 | reject(error); |
|
| 103 | } |
|
| 104 | } else if (body.success.error !== undefined) { |
|
| 105 | Object.assign(body.success.error, meta); |
|
| 106 | reject(body.success.error); |
|
| 107 | } else { |
|
| 108 | reject(error); |
|
| 109 | } |
|
| 110 | } |
|
| 111 | reject(error); |
|
| 112 | } |
|
| 113 | }); |
|
| 114 | }); |
|
| 115 | }; |
|
| 116 | ||
| 117 | export default { |
|
| 118 | "login": login, |
|
| 119 | "test": test |
|
| 120 | }; |
|
| 121 | ||
| @@ 9-120 (lines=112) @@ | ||
| 6 | * Parse response for Codingame's services |
|
| 7 | * @module codingame/parse-response |
|
| 8 | */ |
|
| 9 | import request from 'request'; |
|
| 10 | ||
| 11 | /** |
|
| 12 | * Codingame's API to log in |
|
| 13 | * |
|
| 14 | * @name login |
|
| 15 | * @function |
|
| 16 | * @param {string} username The login to authenticate |
|
| 17 | * @param {string} password The password to authenticate |
|
| 18 | * @returns {Promise<Object>} Body of the response |
|
| 19 | * @memberof module:codingame/api |
|
| 20 | * @instance |
|
| 21 | */ |
|
| 22 | let login = function login(username, password) { |
|
| 23 | return new Promise(function(resolve, reject) { |
|
| 24 | let options = { |
|
| 25 | "method": `POST`, |
|
| 26 | "baseUrl": `https://www.codingame.com`, |
|
| 27 | "uri": `/services/CodingamerRemoteService/loginSiteV2`, |
|
| 28 | "headers": { |
|
| 29 | "Accept": `application/json`, |
|
| 30 | "Content-Type": `application/json;charset=utf-8` |
|
| 31 | }, |
|
| 32 | "body": [username, password, true], |
|
| 33 | "jar": true, |
|
| 34 | "json": true |
|
| 35 | }; |
|
| 36 | request(options, function(error, response, body) { |
|
| 37 | if (response.statusCode >= 400) { |
|
| 38 | reject(error); |
|
| 39 | } else { |
|
| 40 | resolve(body); |
|
| 41 | } |
|
| 42 | }); |
|
| 43 | }); |
|
| 44 | }; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Codingame's API for launching a test |
|
| 48 | * |
|
| 49 | * @name test |
|
| 50 | * @function |
|
| 51 | * @param {string} exercise Hash of the exercise to test |
|
| 52 | * @param {number} test Test's number |
|
| 53 | * @param {string} language Language of the program to send |
|
| 54 | * @param {string} bundle Content of the program to send |
|
| 55 | * @returns {Promise<Object>} The result only if request succeed and test passed |
|
| 56 | * @memberof module:codingame/api |
|
| 57 | * @instance |
|
| 58 | */ |
|
| 59 | let test = function test(exercise, test, language, bundle) { |
|
| 60 | return new Promise(function(resolve, reject) { |
|
| 61 | let options = { |
|
| 62 | "method": `POST`, |
|
| 63 | "baseUrl": `https://www.codingame.com`, |
|
| 64 | "uri": `/services/TestSessionRemoteService/play`, |
|
| 65 | "headers": { |
|
| 66 | "Accept": `application/json`, |
|
| 67 | "Content-Type": `application/json;charset=utf-8` |
|
| 68 | }, |
|
| 69 | "body": [ |
|
| 70 | exercise, |
|
| 71 | { |
|
| 72 | "code": bundle, |
|
| 73 | "programmingLanguageId": language, |
|
| 74 | "multipleLanguages": { |
|
| 75 | "testIndex": test |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ], |
|
| 79 | "jar": true, |
|
| 80 | "json": true |
|
| 81 | }; |
|
| 82 | request(options, function(error, response, body) { |
|
| 83 | if (response.statusCode >= 400) { |
|
| 84 | reject(error); |
|
| 85 | } else { |
|
| 86 | let meta = { |
|
| 87 | "exercise": exercise, |
|
| 88 | "test": test, |
|
| 89 | "language": language, |
|
| 90 | "bundle": bundle |
|
| 91 | }; |
|
| 92 | let error = new Error(`Codingame may have changed its API, contact owner of this application.`); |
|
| 93 | Object.assign(error, meta); |
|
| 94 | if (body.success !== undefined) { |
|
| 95 | if (body.success.comparison !== undefined) { |
|
| 96 | if(body.success.comparison.success) { |
|
| 97 | Object.assign(body.success, meta); |
|
| 98 | resolve(body.success); |
|
| 99 | } else { |
|
| 100 | let error = new Error(`Expected <${body.success.comparison.expected}> but found <${body.success.comparison.found}>`); |
|
| 101 | Object.assign(error, meta); |
|
| 102 | reject(error); |
|
| 103 | } |
|
| 104 | } else if (body.success.error !== undefined) { |
|
| 105 | Object.assign(body.success.error, meta); |
|
| 106 | reject(body.success.error); |
|
| 107 | } else { |
|
| 108 | reject(error); |
|
| 109 | } |
|
| 110 | } |
|
| 111 | reject(error); |
|
| 112 | } |
|
| 113 | }); |
|
| 114 | }); |
|
| 115 | }; |
|
| 116 | ||
| 117 | export default { |
|
| 118 | "login": login, |
|
| 119 | "test": test |
|
| 120 | }; |
|
| 121 | ||