-
Notifications
You must be signed in to change notification settings - Fork 14
/
api-example.js
98 lines (58 loc) · 1.62 KB
/
api-example.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
// Astrid JS API example
var assert = function(result) {
if(!result)
throw "Assertion error: " + result;
}
var print = function(message) {
if(window.console)
window.console.log(message); // authorized
}
// --- initialize
var SERVER = "http://astrid.com";
var APIKEY = "apikey";
var SECRET = "secret";
var astrid = new Astrid(SERVER, APIKEY, SECRET);
// --- sign in
assert(false == astrid.isSignedIn());
var token = localStorage.getItem("astrid-token");
if(!token) {
print("Signing In");
astrid.signInAs("example@example.com", "example!", function(user) {
assert(true == astrid.isSignedIn());
localStorage.setItem("astrid-token", user.token);
getListsAndTasks();
}, function(message) {
throw message;
});
} else {
astrid.setToken(token);
getListsAndTasks();
}
// --- get lists
function getListsAndTasks() {
print("Getting Lists");
astrid.getLists(function(lists) {
assert(lists.length > 0);
print(lists[0].name);
// --- create tasks
print("Creating a Task");
var task = {
title: "A great task",
notes: "With great tasks come great responsibility",
source: "http://www.google.com",
due: new Date(2012, 6, 30).getTime() / 1000,
has_due_time: false,
tag_ids: [ lists[0].id ]
};
astrid.createTask(task, function(result) {
assert(task.title == result.title);
assert(result.created_at > 0);
});
var badTask = {};
astrid.createTask(badTask, function() {
throw "Created task when we weren't expecting it";
}, function(message) {
assert(message.length > 0);
});
});
}