-
Notifications
You must be signed in to change notification settings - Fork 1
/
Firebase.cfc
168 lines (143 loc) · 4.46 KB
/
Firebase.cfc
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
/**
* Firebase CFML REST Client Library
*
* @author Tim Brown <[email protected]>
* @url https://github.com/timmaybrown/firebase-cfml/
* @link https://www.firebase.com/docs/rest-api.html
*
* @output false
*
*/
component accessors="true" implements="IFirebase" {
property name='baseURI' type='string' getter="true" setter="false";
property name='timeout' type='numeric' default="10" getter="false" setter="false";
property name='token' type='string' getter="false" setter="false";
/**
* init()
*
* @hint constructor function
*/
public Firebase function init(
required string baseURI,
string token = '',
string timeout = '10'
) {
setToken( arguments.token );
setBaseURI( arguments.baseURI );
setTimeout( arguments.timeout );
return this;
}
/**
* setToken()
*
* @hint sets the baseURI after normalizing it with a trailing slash
*/
public void function setToken( required string token ) {
variables.token = arguments.token;
}
/**
* setBaseURI
*
* @hint sets the baseURI after normalizing it with a trailing slash
*/
public void function setBaseURI( required string uri ) {
arguments.uri &= (right(arguments.uri, 1) == '/' ? '' : '/');
variables.baseURI = arguments.uri;
}
/**
* setTimeout()
*
* @hint sets the baseURI after normalizing it with a trailing slash
*
*/
public void function setTimeout(required numeric seconds) {
variables.timeout = arguments.seconds;
}
/**
* get()
*
* @hint retreive the data from the firebase resource path
*
*/
public any function get( required string path ) {
return _sendRequest( path, 'GET' );
}
/**
* set()
*
* @hint sets the data in the firebase path specified
*
*/
public any function set( required string path, required any data ) {
return _sendRequest( path, 'PUT', data );
}
/**
* push()
*
* @hint does a post the
*
*/
public any function push( required string path, required any data ) {
return _sendRequest( path, 'POST', data );
}
/**
* update()
*
* @hint Named children in the data being written with this method will be PATCHed, but omitted children will not be deleted
*
*/
public any function update( required string path, required any data ) {
return _sendRequest( path, 'PATCH', data );
}
/**
* delete()
*
* @hint delete the data at the specified path
*/
public any function delete( required string path ) {
return _sendRequest( path, 'DELETE' );
}
/**
* _sendRequest()
*
* @hint do the HTTP request
*/
private function _sendRequest( path, method = 'PUT', data = "", querystring = "" ) {
//default headers
var headers = [
{ "name" = "Content-Type", value = "application/json" }
];
// instantiate the service
var httpService = new http();
// set the URL to the path provided ?auth will be appended by _getJsonPath() if token has been set
var urlPath = _getJsonPath( path );
httpService.setURL( urlPath );
httpService.setCharset( "utf-8" );
httpService.setMethod( arguments.method );
// if data is supplied serialize it, set length header and add it as the body param
if ( !isSimpleValue(arguments.data) ) {
var jsonData = serializeJSON( arguments.data );
arrayAppend(headers, { "name" = "Content-Length", "value" = len( jsonData ) } );
httpService.addParam( type = "body", value = jsonData );
}
for ( var h in headers ) {
httpService.addParam( type = "header", name = h.name, value = h.name );
}
var result = httpService.send().getPrefix();
if (!isJson(result.fileContent)) {
throw('Error: ' & result.fileContent );
} else {
return deserializeJson(result.fileContent);
}
}
/**
* _getJsonPath
*
* @hint Returns with the normalized JSON absolute path
*/
private string function _getJsonPath( required string path ) {
path = reReplace(trim(path), "^/", "") & ".json";
var auth = (variables.token == "") ? "" : "?auth=" & variables.token;
return variables.baseURI & path & auth;
}
}