-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAPI.js
73 lines (64 loc) · 2.55 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
//API for habitica
var _ = require("sdk/l10n").get;
var Request = require("sdk/request").Request;
var Prefs = require("sdk/simple-prefs").prefs;
var Self = require("sdk/self");
var { on, once, off, emit } = require('sdk/event/core');
//make a http-object for request to habitica.com
//after accomplishing request run callback with two arguments: api and json from response
function make_request(arg)
{
let r = Request({
url: "https://habitica.com" + arg.path,
contentType: "application/json",
headers: {"x-api-user": Prefs.userId, "x-api-key": Prefs.apiKey},
content: arg.content
});
r.on("complete", function(response) {
if (200 != response.status) {
if (401 == response.status) {
emit(arg.api, "invalid_credentials", _("invalid_credentials"));
} else {
emit(arg.api, "error", _("notification_title_error") + " " +_("notification_text_error_p1") + response.status + " " +_("notification_text_error_p2"));
}
} else if (! response.json){
emit (arg.api, "error", _("server_return_not_json:") + response.text);
} else {
if (! response.json.success)
emit(arg.api, "error", _("notification_title_error") + " " +_("notification_text_error_p1") + response.status + " " +_("notification_text_error_p2"));
else
arg.callback(arg.api, response.json.data);
}
});
return r;
}
var API = {
update_info: function ()
{
let r = make_request({api:this,
path:"/api/v3/user",
callback:function (api, response) {emit (api, "updated", response)}});
r.get();
},
change_task_rate : function(taskId, direction)
{
//FIXME: wait for bug https://github.com/lefnire/habitrpg/issues/786
//as workaround sent '{}' in POST body.
let r = make_request({api:this,
path:"/api/v3/tasks/" + taskId + "/score/" + direction,
callback:function (api, response) {api.update_info();},
content: "{}"});
r.post();
},
request_task_update:function()
{
let r = make_request({api:this,
path:"/api/v3/tasks/user",
callback:function (api, response) {
emit(api, "user-tasks-updated", response)
}
});
r.get()
}
}
exports.API = API;