-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirebaseConnector.gs
150 lines (128 loc) · 4.74 KB
/
FirebaseConnector.gs
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
/**
* class to connect firebase and manage dataSheetNode
* @param {string} dbName dbName on firebase
* @param {string} errorEmail (optional) email to send the error, if not set no email will be sent
* @return {object}
*/
var FirebaseConnector=function(dbName, errorEmail){
/**
* email address for errors informations
* @type {String}
*/
this.errorEmail=errorEmail,
/**
* setter for the firebase token
* @param {string} token
*/
this.setToken = function( token ) {
//Utilities.sleep(300);
PropertiesService.getUserProperties().setProperty("tokenFireBase", token);
};
this.getToken = function( ) {
return PropertiesService.getUserProperties().getProperty("tokenFireBase");
};
/**
* return firebase url to be update/fetched
* @param {string} firebase node
* @param {string} auth token
*/
this.getFirebaseUrl=function(jsonPath,userToken) {
/*
We then make a URL builder
This takes in a path, and
returns a URL that updates the data in that path
*/
return 'https://'+dbName+'.firebaseio.com/' + jsonPath + '.json?auth=' + userToken;
};
/**
* write data on firebase
* @param {object} data data to save NOT PARSED
* @param {string} saveNode firebase note where to save
* @param {string} userToken auth token
* @param {function} onError error callback with two params: responseCode, and error description
* @return {number} the HTTP status code (200 for OK, etc.) of an HTTP response.
*/
this.writeOnFirebase = function(data,saveNode,userToken, onError){
onError=(onError || function(){});
var responseCode,options = {
'method': 'put',
'contentType': 'application/json',
'payload': JSON.stringify(data),
'muteHttpExceptions' : true
};
var fireBaseUrl = this.getFirebaseUrl(saveNode,userToken);
var response=UrlFetchApp.fetch(fireBaseUrl, options);
responseCode=response.getResponseCode();
if (responseCode!==200) {
Utility.sendErrorEmails(
"method: FirebaseConnector.writeOnFirebase()\n\n"+
"response.getResponseCode(): "+response.getResponseCode()+"\n\n"+
"saveNode: "+saveNode+"\n\n"+
"data:"+data+"\n\n"+
"getAllHeaders(): "+JSON.stringify(response.getAllHeaders())+"\n\n"+
"getContentText(): "+response.getContentText()+"\n\n",
this.errorEmail
);
onError(responseCode, JSON.parse(response.toString()).error);
}
return responseCode;
};
/**
* fetch data from Firebase
* @param {string} firebase note where to fetch
* @param {string} auth token
* @param {function} onError error callback with two params: responseCode, and error description
* @return {string} data fetched NOT PARSED
*/
this.getFireBaseData = function( node, userToken, onError ) {
onError=(onError || function(){});
var options = {
'muteHttpExceptions': true
};
var fireBaseUrl = this.getFirebaseUrl( node, userToken );
var ft = UrlFetchApp.fetch( fireBaseUrl, options );
var responseCode=ft.getResponseCode();
if ( responseCode !== 200 ) {
Utility.sendErrorEmails(
"method: FirebaseConnector.getFireBaseData()\n\n" +
"ft.getResponseCode(): " + ft.getResponseCode() + "\n\n" +
"node: " + node + "\n\n" +
"getAllHeaders(): " + JSON.stringify( ft.getAllHeaders() ) + "\n\n" +
"getContentText(): " + ft.getContentText() + "\n\n",
this.errorEmail
);
onError(responseCode, JSON.parse(ft.toString()).error);
return null;
}
return ft.toString();
};
/**
* fetch data from Firebase
* @param {string} firebase note where to fetch
* @param {string} auth token
* @param {function} onError error callback with two params: responseCode, and error description
* @return {object} data fetched PARSED
*/
this.getFireBaseDataParsed = function( node, userToken, onError ) {
onError=(onError || function(){});
var options = {
'muteHttpExceptions': true
};
var fireBaseUrl = this.getFirebaseUrl( node, userToken );
var ft = UrlFetchApp.fetch( fireBaseUrl, options );
var responseCode=ft.getResponseCode();
if ( responseCode !== 200 ) {
Utility.sendErrorEmails(
"method: FirebaseConnector.getFireBaseData()\n\n" +
"ft.getResponseCode(): " + ft.getResponseCode() + "\n\n" +
"node: " + node + "\n\n" +
"getAllHeaders(): " + JSON.stringify( ft.getAllHeaders() ) + "\n\n" +
"getContentText(): " + ft.getContentText() + "\n\n",
this.errorEmail
);
onError(responseCode, JSON.parse(ft.toString()).error);
return null;
}
return JSON.parse(ft.toString());
};
};