-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
328 lines (273 loc) · 8.05 KB
/
client.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Package firebase gives a thin wrapper around the firebase REST API. It tries
// to mirror the Official firebase API somewhat closely. https://www.firebase.com/docs/web/api/
package firebase
import (
"encoding/json"
"errors"
"regexp"
"strconv"
"time"
)
var keyExtractor = regexp.MustCompile(`https://.*/([^/]+)/?$`)
// ServerTimestamp is a Go binding for Firebase's ServerValue.TIMESTAMP fields.
// When marshalling a variable of ServerTimestamp type into JSON (i.e. to send
// to Firebase), it takes the following JSON representation, no matter what
// time value the variable has been assigned:
//
// {".sv":"timestamp"}
//
// When this JSON value is sent to Firebase, it is substituted into a number
// equal to milliseconds since the epoch, as measured by Firebase's servers.
//
// When reading a value of this type from Firebase, you receive a number equal
// to milliseconds since the epoch. That value was computed by Firebase when
// the JSON detailed above was written. A JSON unmarshal of this type will
// convert a number of ms since the epoch into a time.Time value.
//
// NOTE: This approach results in non-symmetric marshal/unmarshal behavior
// (i.e. unmarshal(marshal(ServerTimestamp{})) will return an error). It is
// intended to be used only when reading/writing this value from Firebase.
//
// See the Firebase's documentation of ServerValues and timestamps for more
// details:
// https://www.firebase.com/docs/rest/api/#section-server-values
type ServerTimestamp time.Time
func (t ServerTimestamp) MarshalJSON() ([]byte, error) {
var serverValue struct {
Value string `json:".sv"`
}
serverValue.Value = "timestamp"
return json.Marshal(serverValue)
}
func (t *ServerTimestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
// Firebase reports milliseconds since the epoch, Go counts in ns.
*t = ServerTimestamp(time.Unix(0, ts*int64(time.Millisecond)))
return nil
}
// FirebaseError is a Go representation of the error message sent back by Firebase when a
// request results in an error.
type FirebaseError struct {
Message string `json:"error"`
}
func (f *FirebaseError) Error() string {
return f.Message
}
// This is the actual default implementation
type client struct {
// The ordering being enforced on this client
Order string
// url is the client's base URL used for all calls.
url string
// auth is authentication token used when making calls.
// The token is optional and can also be overwritten on an individual
// call basis via params.
auth string
// api is the underlying client used to make calls.
api Api
params map[string]string
}
func NewClient(root, auth string, api Api) Client {
if api == nil {
api = new(firebaseAPI)
}
return &client{url: root, auth: auth, api: api}
}
func (c *client) String() string {
return c.url
}
func (c *client) Key() string {
matches := keyExtractor.FindAllStringSubmatch(c.url, 1)
// This is kind of an error. There should always be a / somewhere,
// but if you just have the raw domain you don't really need one. So
// we assume this is the case and return ""
if len(matches) == 0 {
return ""
}
return matches[0][1]
}
func (c *client) Value(destination interface{}) error {
err := c.api.Call("GET", c.url, c.auth, nil, c.params, destination)
if err != nil {
return err
}
return nil
}
var defaultUnmarshaller = func(path string, data []byte) (interface{}, error) {
var object map[string]interface{}
err := json.Unmarshal(data, &object)
return object, err
}
func handlePatchPut(event *StreamEvent, unmarshaller EventUnmarshaller) {
var halfParsedData struct {
Path string
Data json.RawMessage
}
err := json.Unmarshal([]byte(event.RawData), &halfParsedData)
if err != nil {
event.Error = err
return
}
event.Path = halfParsedData.Path
object, err := unmarshaller(halfParsedData.Path, halfParsedData.Data)
if err != nil {
event.UnmarshallerError = err
} else {
event.Resource = object
}
}
func (c *client) Watch(unmarshaller EventUnmarshaller, stop <-chan bool) (<-chan StreamEvent, error) {
rawEvents, err := c.api.Stream(c.url, c.auth, nil, c.params, stop)
if err != nil {
return nil, err
}
processedEvents := make(chan StreamEvent, 1000)
if unmarshaller == nil {
unmarshaller = defaultUnmarshaller
}
go func() {
for rawEvent := range rawEvents {
event := StreamEvent{
Event: rawEvent.Event,
RawData: rawEvent.Data,
Error: rawEvent.Error,
}
// connection error: just forward it along
if event.Error != nil {
processedEvents <- event
continue
}
switch event.Event {
case "patch", "put":
handlePatchPut(&event, unmarshaller)
processedEvents <- event
case "keep-alive":
break
case "cancel":
event.Error = errors.New("Permission Denied")
processedEvents <- event
case "auth_revoked":
event.Error = errors.New("Auth Token Revoked")
processedEvents <- event
close(processedEvents)
return
}
}
close(processedEvents)
}()
return processedEvents, nil
}
func (c *client) Shallow() Client {
newParams := make(map[string]string)
for key, value := range c.params {
newParams[key] = value
}
newParams["shallow"] = "true"
return &client{
api: c.api,
auth: c.auth,
url: c.url,
params: newParams,
}
}
func (c *client) Child(path string) Client {
u := c.url + "/" + path
return &client{
api: c.api,
auth: c.auth,
url: u,
params: c.params,
}
}
const (
KeyProp = "$key"
)
// These are some shenanigans, golang. Shenanigans I say.
func (c *client) newParamMap(key string, value interface{}) map[string]string {
ret := make(map[string]string, len(c.params)+1)
for key, value := range c.params {
ret[key] = value
}
jsonVal, _ := json.Marshal(value)
ret[key] = string(jsonVal)
return ret
}
func (c *client) clientWithNewParam(key string, value interface{}) *client {
return &client{
api: c.api,
auth: c.auth,
url: c.url,
params: c.newParamMap(key, value),
}
}
// Query functions. They map directly to the Firebase operations.
// https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-queries
func (c *client) OrderBy(prop string) Client {
newC := c.clientWithNewParam("orderBy", prop)
newC.Order = prop
return newC
}
func (c *client) EqualTo(value interface{}) Client {
return c.clientWithNewParam("equalTo", value)
}
func (c *client) StartAt(value interface{}) Client {
return c.clientWithNewParam("startAt", value)
}
func (c *client) EndAt(value interface{}) Client {
return c.clientWithNewParam("endAt", value)
}
func (c *client) LimitToFirst(limit uint) Client {
return c.clientWithNewParam("limitToFirst", limit)
}
func (c *client) LimitToLast(limit uint) Client {
return c.clientWithNewParam("limitToLast", limit)
}
func (c *client) Push(value interface{}, params map[string]string) (Client, error) {
res := map[string]string{}
err := c.api.Call("POST", c.url, c.auth, value, params, &res)
if err != nil {
return nil, err
}
return &client{
api: c.api,
auth: c.auth,
url: c.url + "/" + res["name"],
params: c.params,
}, nil
}
func (c *client) Set(path string, value interface{}, params map[string]string) (Client, error) {
u := c.url + "/" + path
err := c.api.Call("PUT", u, c.auth, value, params, nil)
if err != nil {
return nil, err
}
return &client{
api: c.api,
auth: c.auth,
url: u,
params: c.params,
}, nil
}
func (c *client) Update(path string, value interface{}, params map[string]string) error {
err := c.api.Call("PATCH", c.url+"/"+path, c.auth, value, params, nil)
return err
}
func (c *client) Remove(path string, params map[string]string) error {
err := c.api.Call("DELETE", c.url+"/"+path, c.auth, nil, params, nil)
return err
}
func (c *client) Rules(params map[string]string) (*Rules, error) {
res := &Rules{}
err := c.api.Call("GET", c.url+"/.settings/rules", c.auth, nil, params, res)
if err != nil {
return nil, err
}
return res, nil
}
func (c *client) SetRules(rules *Rules, params map[string]string) error {
err := c.api.Call("PUT", c.url+"/.settings/rules", c.auth, rules, params, nil)
return err
}