Skip to content

Commit 10d8b57

Browse files
committed
Support full insert API.
1 parent 2693c2c commit 10d8b57

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

google/calendar.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
<p>Create an entry in a <a href="https://www.google.com/calendar">Google Calendar</a>.</p>
3434
<p>The incoming message can provide the following properties:
3535
<ul>
36-
<li><b>payload</b> - a string to describe the event using <a href="https://support.google.com/calendar/answer/36604?hl=en">quick add format</a></li>
36+
<li><b>payload</b> - either a string to describe the event using <a href="https://support.google.com/calendar/answer/36604?hl=en">quick add format</a> or an object representing the request body for an <a href="https://developers.google.com/google-apps/calendar/v3/reference/events/insert">insert request</a></li>
3737
<li><b>calendar</b> - the calendar to add the event to (optional, defaults to the node calendar property or the users primary calendar)</li>
38+
<li><b>sendNotifications</b> - a boolean to determine if notifications should be sent to attendees (optional, defaults to false)</li>
3839
</ul>
3940
</p>
4041
</script>

google/calendar.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,24 @@ module.exports = function(RED) {
5959
node.status({fill:"red",shape:"ring",text:"invalid calendar"});
6060
return;
6161
}
62-
node.google.request({
62+
var request = {
6363
method: 'POST',
64-
url: 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events/quickAdd',
65-
form: {
66-
text: msg.payload,
67-
},
68-
}, function(err, data) {
64+
};
65+
if (typeof msg.payload === 'object') {
66+
request.url = 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events';
67+
request.body = msg.payload;
68+
} else {
69+
request.url = 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events/quickAdd';
70+
request.form = {
71+
text: RED.util.ensureString(msg.payload)
72+
};
73+
}
74+
if (node.sendNotifications || msg.sendNotifications) {
75+
request.query = {
76+
sendNotifications: true
77+
};
78+
}
79+
node.google.request(request, function(err, data) {
6980
if (err) {
7081
node.error(err.toString());
7182
node.status({fill:"red",shape:"ring",text:"failed"});

google/google.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ module.exports = function(RED) {
8787
req = { url: req };
8888
}
8989
req.method = req.method || 'GET';
90-
req.json = req.json || true;
90+
if (!req.hasOwnProperty("json")) {
91+
req.json = true;
92+
}
9193
// always set access token to the latest ignoring any already present
9294
req.auth = { bearer: this.credentials.accessToken };
9395
//console.log(require('util').inspect(req));
@@ -110,6 +112,13 @@ module.exports = function(RED) {
110112
request(req, function(err, result, data) {
111113
if (err) {
112114
// handled in callback
115+
} else if (result.statusCode >= 400) {
116+
data = {
117+
error: {
118+
code: result.statusCode,
119+
message: result.body,
120+
},
121+
};
113122
} else if (data.error) {
114123
if (data.error.code === 401 && retries > 0) {
115124
retries--;

0 commit comments

Comments
 (0)