-
Notifications
You must be signed in to change notification settings - Fork 0
/
examiner.js
61 lines (49 loc) · 1.37 KB
/
examiner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
http = require('http');
var minisculusServer = {
// host: 'minisculus.edendevelopment.co.uk',
host: 'minisculuschallenge.com',
port: 80,
getHeader: function(path,bodyLength) {
return {
host: this.host,
port: this.port,
path: path,
'content-length': bodyLength,
accept: 'application/json',
'content-Type': 'application/json',
}
}
};
function doRequest(httpVerb,path,body,callback) {
var result = {};
var header = minisculusServer.getHeader(path,body.length);
var httpClient = http.createClient(minisculusServer.port, minisculusServer.host);
var request = httpClient.request(httpVerb, path, header);
request.end(body);
request.on('response', function (jsonResponse) {
if (jsonResponse.statusCode === 303) {
doRequest('GET',jsonResponse.headers.location,'',callback);
}
else if (jsonResponse.statusCode < 400) {
jsonResponse.on('data', function(buffer) {
result = JSON.parse(buffer.toString('utf-8'));
});
jsonResponse.on('end',function() {
result.result = "ok";
callback(result);
});
}
else {
callback({
httpStatus: jsonResponse.statusCode,
result: "error!"
});
}
});
}
exports.getQuestion = function (questionPath, callback) {
doRequest('GET',questionPath,'',callback);
}
exports.putAnswer = function (answerPath, answer, callback) {
doRequest('PUT',answerPath,answer,callback);
}