-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
171 lines (137 loc) · 3.44 KB
/
api.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
package firebase
import (
"bufio"
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strings"
)
// firebaseAPI is the internal implementation of the Firebase API client.
type firebaseAPI struct{}
func doFirebaseRequest(client *http.Client, method, path, auth, accept string, body interface{}, params map[string]string) (*http.Response, error) {
// Every path needs to end in .json for the Firebase REST API
path += ".json"
qs := url.Values{}
// if the client has an auth, set it as a query string.
// the caller can also override this on a per-call basis
// which will happen via params below
if len(auth) > 0 {
qs.Set("auth", auth)
}
for k, v := range params {
qs.Set(k, v)
}
if len(qs) > 0 {
path += "?" + qs.Encode()
}
encodedBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, path, bytes.NewReader(encodedBody))
if err != nil {
return nil, err
}
if accept != "" {
req.Header.Add("Accept", accept)
}
req.Close = true
return client.Do(req)
}
// Call invokes the appropriate HTTP method on a given Firebase URL.
func (f *firebaseAPI) Call(method, path, auth string, body interface{}, params map[string]string, dest interface{}) error {
var response *http.Response
var err error
retries := 10
for {
response, err = doFirebaseRequest(httpClient, method, path, auth, "",
body, params)
if err != nil && retries == 0 {
return err
} else if err != nil {
retries--
log.Println("Retry: ", err)
continue
}
if response.StatusCode >= 400 && retries > 0 {
retries--
log.Println("Retry: status code == ", response.StatusCode)
response.Body.Close()
continue
}
break
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
if response.StatusCode >= 400 {
err := &FirebaseError{}
decoder.Decode(err)
return err
}
if dest != nil && response.ContentLength != 0 {
err = decoder.Decode(dest)
if err != nil {
return err
}
}
return nil
}
// Stream implements an SSE/Event Source client that watches for changes at a
// given Firebase location.
func (f *firebaseAPI) Stream(path, auth string, body interface{}, params map[string]string, stop <-chan bool) (<-chan RawEvent, error) {
response, err := doFirebaseRequest(streamClient, "GET", path, auth,
"text/event-stream", body, params)
if err != nil {
return nil, err
}
go func() {
<-stop
response.Body.Close()
}()
events := make(chan RawEvent, 1000)
// bufio.Scanner barfs on really long events, as its buffer size is pretty
// small, and it can't be overridden. This is not the most memory-optimal
// implementation of the streaming client, but each event is not expected
// to exceed several KB.
go func() {
var (
err error
firstLine string
lineBuf []byte
)
byteReader := bufio.NewReader(response.Body)
for {
var b byte
b, err = byteReader.ReadByte()
if err != nil {
break
}
if b != "\n"[0] {
lineBuf = append(lineBuf, b)
continue
}
if firstLine == "" {
firstLine = string(lineBuf)
lineBuf = []byte{}
continue
}
line := string(lineBuf)
event := RawEvent{}
event.Event = strings.Replace(firstLine, "event: ", "", 1)
event.Data = strings.Replace(line, "data: ", "", 1)
events <- event
firstLine = ""
lineBuf = []byte{}
}
if err == io.EOF {
err = nil
}
closeEvent := RawEvent{Error: err}
events <- closeEvent
close(events)
}()
return events, nil
}