-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
51 lines (46 loc) · 1.22 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
var ID = require("./id");
var Request = require("sdk/request").Request;
var preferences = require('sdk/simple-prefs').prefs;
var url = {
post_visits: function() {
return preferences.api_url + "/visits/" + ID.unique_id();
},
get_visits: function() {
return preferences.api_url + "/visits/" + ID.unique_id();
},
get_all_visits: function() {
return preferences.api_url + "/visits";
}
};
var Transmitter = function(
onSuccess, onClientError, onServerError, onNetworkError) {
this.submit = function(data) {
var req = Request({
url: url.post_visits(),
contentType: 'application/json',
content: JSON.stringify({visits: data}, function(key, value) {
if (key == "visited_at") return this.visited_at.getTime();
return value;
}),
onComplete: function(result) {
if (result.status == 0) {
onNetworkError(data);
return;
}
if (result.status < 400) {
onSuccess(data);
return;
}
if (result.status < 500) {
onClientError(data);
return;
}
onServerError(data);
return;
}
});
req.post();
};
};
exports.Transmitter = Transmitter;
exports.url = url;