-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.js
103 lines (94 loc) · 2.51 KB
/
api.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
var tttapi = {
gameWatcher: null,
ttt: 'http://ttt.wdibos.com',
ajax: function(config, cb) {
$.ajax(config).done(function(data, textStatus, jqxhr) {
cb(null, data);
}).fail(function(jqxhr, status, error) {
cb({jqxher: jqxhr, status: status, error: error});
});
},
register: function register(credentials, callback) {
this.ajax({
method: 'POST',
url: this.ttt + '/users',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(credentials),
dataType: 'json'
}, callback);
},
login: function login(credentials, callback) {
this.ajax({
method: 'POST',
url: this.ttt + '/login',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(credentials),
dataType: 'json'
}, callback);
},
//Authenticated api actions
listGames: function (token, callback) {
this.ajax({
method: 'GET',
url: this.ttt + '/games',
headers: {
Authorization: 'Token token=' + token
},
dataType: 'json'
}, callback);
},
createGame: function (token, callback) {
this.ajax({
method: 'POST',
url: this.ttt + '/games',
headers: {
Authorization: 'Token token=' + token
},
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({}),
dataType: 'json',
}, callback);
},
showGame: function (id, token, callback) {
this.ajax({
method: 'GET',
url: this.ttt + '/games/' + id,
headers: {
Authorization: 'Token token=' + token
},
dataType: 'json'
}, callback);
},
joinGame: function (id, token, callback) {
this.ajax({
method: 'PATCH',
url: this.ttt + '/games/' + id,
headers: {
Authorization: 'Token token=' + token
},
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({}),
dataType: 'json'
}, callback);
},
markCell: function (id, data, token, callback) {
this.ajax({
method: 'PATCH',
url: this.ttt + '/games/' + id,
headers: {
Authorization: 'Token token=' + token
},
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
dataType: 'json'
}, callback);
},
watchGame: function (id, token) {
var url = this.ttt + '/games/' + id + '/watch';
var auth = {
Authorization: 'Token token=' + token
};
this.gameWatcher = resourceWatcher(url, auth); //jshint ignore: line
return this.gameWatcher;
}
};