-
Notifications
You must be signed in to change notification settings - Fork 0
/
quescheetah-sdk-0.0.1.js
146 lines (123 loc) · 5.18 KB
/
quescheetah-sdk-0.0.1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function QuesCheetah(config){
this.apiKey = config.apiKey;
this.baseUrl = "http://www.quescheetah.com/v1/";
this.callBackUrl = config.callBackUrl;
this.receiveRealtimeResponse = config.receiveRealtimeResponse;
// If Realtime response is used.
if (this.receiveRealtimeResponse){
socket = io('http://localhost:5000');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
socket.on('reply', function(data){
// callback function after receiving data from socket server
alert(JSON.stringify(data));
});
}
}
var socket= "";
QuesCheetah.prototype.createGroup = function (params, success, error) {
if ( params['group_name'] === ""){
this.createQuestion(params, success, error);
}else{
var url = this.baseUrl+'groups';
this.doPost(url, "POST", params, success, error);
}
};
QuesCheetah.prototype.createQuestion = function (params, success, error) {
var url = this.baseUrl+'questions';
this.doPost(url, "POST", params, success, error)
};
QuesCheetah.prototype.createAnswer = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id+'/answers';
this.doPost(url, "POST", params, success, error)
};
QuesCheetah.prototype.createUserAnswer = function (params, success, error) {
var self = this;
var url = this.baseUrl+'questions/'+params.question_id+'/answers/'+params.answer_num+'/useranswers';
var successWithSend = function(){
success();
// If client realtime setting is true, send question_id to socket server.
if (self.receiveRealtimeResponse){
self.socketioAction($.extend({
"api-key": self.apiKey
},params));
}
};
this.doPost(url, "POST", params, successWithSend, error)
};
QuesCheetah.prototype.getGroup = function (params, success, error) {
var url = this.baseUrl+'groups/'+params.group_id;
this.doPost(url, "GET", params, success, error)
};
QuesCheetah.prototype.getQuestion = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id;
this.doPost(url, "GET", params, success, error)
};
QuesCheetah.prototype.getAnswer = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id+'/answers/'+params.answer_num;
this.doPost(url, "GET", params, success, error)
};
QuesCheetah.prototype.getUseranswersOfGroup = function (params, success, error) {
var url = this.baseUrl+'groups/'+params.group_id+'/answers/useranswers';
this.doPost(url, "GET", params, success, error)
};
QuesCheetah.prototype.updateQuestion = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id;
this.doPost(url, "PUT", params, success, error)
};
QuesCheetah.prototype.deleteAnswer = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id+'/answers/'+params.answer_num;
this.doPost(url, "DELETE", params, success, error)
};
QuesCheetah.prototype.deleteUserAnswer = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id+'/answers/useranswers/'+params.unique_user;
this.doPost(url, "DELETE", params, success, error)
};
QuesCheetah.prototype.deleteQuestion = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id;
this.doPost(url, "DELETE", params, success, error)
};
QuesCheetah.prototype.deleteGroup = function (params, success, error) {
var url = this.baseUrl+'groups/'+params.group_id;
this.doPost(url, "DELETE", params, success, error)
};
QuesCheetah.prototype.updateUserAnswer = function (params, success, error) {
var url = this.baseUrl+'questions/'+params.question_id+'/answers/useranswers/'+params.unique_user;
this.doPost(url, "PUT", params, success, error)
};
QuesCheetah.prototype.doPost = function (url, type, post_body, success, errorCallback) {
var self = this;
var request = new XMLHttpRequest();
request.open(type, url, true);
// you can use jwt with kid header instead of api-key header for more secure connection.
//request.setRequestHeader("jwt", "your jwt value");
//request.setRequestHeader("kid", "2");
request.setRequestHeader("api-key", self.apiKey);
request.setRequestHeader("Content-Type", "application/json");
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
var json = request.responseText;
if (json.error) {
if (errorCallback) {
errorCallback()
}
console.log(json.description);
} else {
if (success) {
success(json);
}
}
} else {
alert('There was a problem with the request.');
}
};
request.onerror = function (e) {
alert("Error " + e.target.status + " occurred while receiving the document.");
};
request.send(JSON.stringify(post_body));
};
// Submit data to socket server
QuesCheetah.prototype.socketioAction = function(data){
socket.emit('send', data);
};