This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_api.js
195 lines (150 loc) · 5.04 KB
/
google_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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// api_getTasks(token, callback(response))
// HTTP GET
// gets all non-completed tasks from detault task list
function api_getTasks(token, callback){
console.log("google_api: getTaskData()");
var reqURL = "https://www.googleapis.com/tasks/v1/lists/@default/tasks";
reqURL += "?showDeleted=false";
reqURL += "&showHidden=false";
reqURL += "&showCompleted=false";
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('GET', reqURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.onerror = function () {
console.log("google_api: HTTP GET ERROR [TASK]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP GET SUCCESS [TASK]:");
console.log(this.response);
callback(this.response);
};
console.log("Request URL: " + reqURL);
xhr.send();
}
// api_getCalendarEvents(token, callback(response))
// HTTP GET
// gets all calendar events on the primary calendar in the future
function api_getCalendarEvents(token, callback){
console.log("google_api: getCalendarData()");
var reqURL = "https://www.googleapis.com/calendar/v3/calendars/primary/events";
reqURL += "?calendarId=primary";
reqURL += "&maxResults=200";
reqURL += "&orderBy=startTime";
reqURL += "&showDeleted=false";
reqURL += "&singleEvents=true";
reqURL += "&timeMin=" + moment.tz(EST).format(fullDateFormat);
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('GET', reqURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.onerror = function () {
console.log("google_api: HTTP GET ERROR [CAL]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP GET SUCCESS [CAL]");
console.log(this.response);
callback(this.response);
};
console.log("Request URL: " + reqURL);
xhr.send();
}
// api_postTask(token, task)
// HTTP POST
// creates the task on the task list
function api_postTask(token, task){
var postURL = "https://www.googleapis.com/";
postURL += "tasks/v1/lists/@default/tasks";
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('POST', postURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onerror = function () {
console.log("google_api: HTTP POST ERROR [TASK]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP POST SUCCESS [TASK]");
console.log(this.response);
task.id = this.response.id;
};
var body = JSON.stringify(task.getCreateBody());
console.log("Post URL: " + postURL);
console.log(" BODY: " + body);
xhr.send(JSON.stringify(task.getCreateBody()));
}
// api_postCalendarEvent(token, calEvent)
// HTTP POST
// creates the calendar event on the primary calendar
function api_postCalendarEvent(token, calEvent){
var postURL = "https://www.googleapis.com/";
postURL += "calendar/v3/calendars/primary/events";
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('POST', postURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onerror = function () {
console.log("google_api: HTTP POST ERROR [EVENT]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP POST SUCCESS [EVENT]");
console.log(this.response);
calEvent.id = this.response.id;
};
var body = JSON.stringify(calEvent.getCreateBody());
console.log("Post URL: " + postURL);
console.log(" BODY: " + body);
xhr.send(body);
}
// api_putTaskCompleted(token, task)
// HTTP PUT
// marks the given task as completed
function api_putTaskCompleted(token, task){
var delURL = "https://www.googleapis.com/";
delURL += "tasks/v1/lists/@default/tasks/";
delURL += task.id;
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('PUT', delURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onerror = function () {
console.log("google_api: HTTP PUT ERROR [TASK]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP PUT SUCCESS [TASK]");
console.log(this.response);
};
var body = JSON.stringify( { id: task.id, status: "completed" } );
console.log("Put URL: " + delURL);
console.log(" BODY: " + body);
xhr.send(body);
}
// api_deleteCalendarEvent(token, calEvent)
// HTTP DELETE
// deletes the given event from the calendar
function api_deleteCalendarEvent(token, calEvent){
var delURL = "https://www.googleapis.com/";
delURL += "calendar/v3/calendars/primary/events/";
delURL += calEvent.id;
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('DELETE', delURL);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.onerror = function () {
console.log("google_api: HTTP DELETE ERROR [EVENT]");
console.log(this);
};
xhr.onload = function() {
console.log("google_api: HTTP DELETE SUCCESS [EVENT]");
console.log(this.response);
};
console.log("DELETE URL: " + delURL);
xhr.send();
}