Completed
Push — master ( 1dace6...dc420e )
by Jean
01:31
created

parse-response.js ➔ ... ➔ request   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 3
dl 7
loc 7
rs 9.4285
1
/**
2
 * @file Module 'codingame/parse-response'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse response for Codingame's services
7
 * @module codingame/parse-response
8
 */
9 View Code Duplication
import request from 'request';
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable login already seems to be declared on line 22. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable test already seems to be declared on line 59. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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