-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.js
43 lines (38 loc) · 924 Bytes
/
http.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
/**
* Issues an HTTP GET request with given auth token.
* USAGE: $http('www.something.com', {Authorization: 'token', method: 'get'}).data('json');
*/
function $http(url, headers) {
$http.response = UrlFetchApp.fetch(url, {
headers : headers//,
//muteHttpExceptions : true
});
return $http;
};
/**
* Get last response data in specified format or as raw text.
* USAGE:
* $http.data(), $http.data('json')
*/
$http.data = function(fmt) {
if ($http.error()) {
return null;
}
var data = $http.response.getContentText();
if ((typeof fmt) == 'undefined')
return data;
if (fmt == 'json') {
$http.jsonData = JSON.parse(data);
return $http.jsonData;
}
return data;
};
$http.error = function() {
if ($http.response.getResponseCode() == 200) {
return null;
}
return {
code: $http.response.getResponseCode(),
message: $http.response.getContentText()
};
};