-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestAPI.gs
More file actions
92 lines (80 loc) · 2.16 KB
/
RestAPI.gs
File metadata and controls
92 lines (80 loc) · 2.16 KB
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
// Adapted from https://github.com/uu1t/gas-trello for Slack webhook
var endpoint_ = PropertiesService.getScriptProperties().getProperty('Slack_incoming_webhook');
/**
* Send GET request.
* @param {string} path
* @param {{ [key: string]: string }} [params]
* @return {any}
*/
function get(path, params) {
return rest('GET', path, params);
}
/**
* Send POST request.
* @param {string} path
* @param {{ [key: string]: string }} [params]
* @return {any}
*/
function post(path, params, body = '') {
return rest('POST', path, params, body);
}
/**
* Send PUT request.
* @param {string} path
* @param {{ [key: string]: string }} [params]
* @param {string} body of the request
* @return {any}
*/
function put(path, params, body = '') {
return rest('PUT', path, params, body);
}
/**
* Send DELETE request.
* @param {string} path
* @param {{ [key: string]: string }} [params]
* @return {any}
*/
function del(path, params) {
return rest('DELETE', path, params);
}
/**
* Send REST request.
* @param {'GET' | 'POST' | 'PUT' | 'DELETE'} method HTTP method to use when making the request to the Trello API
* @param {string} path API path to use, such as "members/me"
* @param {{ [key: string]: string }} [params={}] Query parameters to the API path, such as { fields: "username,fullName" }
* @param {string} request body
* @return {any} Result object
*/
function rest(method, path, params, bodydata = '') {
params = params || {};
path = ""; //Slack webhooks have a fixed endpoint (there is no API path)
// Get rid of any leading /
path = path.replace(/^\/*/, '');
var url = endpoint_ + path + toQuery_(params);
var response = UrlFetchApp.fetch(url, {
method: method,
contentType: 'application/json',
payload: bodydata
});
response = response.getContentText();
try{
//if the response is json, try to parse it
response = JSON.parse();
}
catch(e){}
return response;
}
/**
* @param {{ [key: string]: string }} params
* @return {string}
*/
function toQuery_(params) {
return (
'?' +
Object.keys(params)
.map(function(key) {
return key + '=' + encodeURIComponent(params[key]);
})
.join('&')
);
}