author | Mikael Berthe <mikael@lilotux.net> |
Wed, 19 Apr 2017 10:43:38 +0200 | |
changeset 138 | 23d3a518d0ad |
parent 130 | c450bb73f59a |
child 140 | 7665451f74cc |
permissions | -rw-r--r-- |
130 | 1 |
/* |
2 |
Copyright 2017 Mikael Berthe |
|
3 |
||
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
117 | 8 |
|
9 |
import ( |
|
10 |
"bufio" |
|
11 |
"bytes" |
|
12 |
"encoding/json" |
|
13 |
"errors" |
|
14 |
"fmt" |
|
15 |
"io" |
|
16 |
"log" |
|
17 |
"net/http" |
|
18 |
"strings" |
|
19 |
"time" |
|
20 |
||
21 |
"github.com/sendgrid/rest" |
|
22 |
) |
|
23 |
||
24 |
// StreamEvent contains a single event from the streaming API |
|
25 |
type StreamEvent struct { |
|
26 |
Event string // Name of the event (error, update, notification or delete) |
|
27 |
Data interface{} // Status, Notification or status ID |
|
28 |
Error error // Error message from the StreamListener |
|
29 |
} |
|
30 |
||
31 |
// openStream opens a stream URL and returns an http.Response |
|
32 |
// Note that the caller should close the connection when it's done reading |
|
33 |
// the stream. |
|
34 |
// The stream name can be "user", "public" or "hashtag". |
|
35 |
// For "hashtag", the hashTag argument cannot be empty. |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
36 |
func (mc *Client) openStream(streamName, hashTag string) (*http.Response, error) { |
120 | 37 |
params := make(apiCallParams) |
117 | 38 |
|
39 |
switch streamName { |
|
40 |
case "user", "public": |
|
41 |
case "hashtag": |
|
42 |
if hashTag == "" { |
|
43 |
return nil, ErrInvalidParameter |
|
44 |
} |
|
120 | 45 |
params["tag"] = hashTag |
117 | 46 |
default: |
47 |
return nil, ErrInvalidParameter |
|
48 |
} |
|
49 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
50 |
req, err := mc.prepareRequest("streaming/"+streamName, rest.Get, params) |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
51 |
if err != nil { |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
52 |
return nil, fmt.Errorf("cannot build stream request: %s", err.Error()) |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
53 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
54 |
|
117 | 55 |
reqObj, err := rest.BuildRequestObject(req) |
56 |
if err != nil { |
|
57 |
return nil, fmt.Errorf("cannot build stream request: %s", err.Error()) |
|
58 |
} |
|
120 | 59 |
|
117 | 60 |
resp, err := rest.MakeRequest(reqObj) |
61 |
if err != nil { |
|
62 |
return nil, fmt.Errorf("cannot open stream: %s", err.Error()) |
|
63 |
} |
|
64 |
if resp.StatusCode != 200 { |
|
65 |
resp.Body.Close() |
|
66 |
return nil, errors.New(resp.Status) |
|
67 |
} |
|
68 |
return resp, nil |
|
69 |
} |
|
70 |
||
71 |
// readStream reads from the http.Response and sends events to the events channel |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
72 |
// It stops when the connection is closed or when the stopCh channel is closed. |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
73 |
// The foroutine will close the doneCh channel when it terminates. |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
74 |
func (mc *Client) readStream(events chan<- StreamEvent, stopCh <-chan bool, doneCh chan<- bool, r *http.Response) { |
117 | 75 |
defer r.Body.Close() |
76 |
||
77 |
reader := bufio.NewReader(r.Body) |
|
78 |
||
79 |
var line, eventName string |
|
80 |
for { |
|
81 |
select { |
|
82 |
case <-stopCh: |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
83 |
close(doneCh) |
117 | 84 |
return |
85 |
default: |
|
86 |
} |
|
87 |
||
88 |
lineBytes, partial, err := reader.ReadLine() |
|
89 |
if err != nil { |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
90 |
var e error |
117 | 91 |
if err == io.EOF { |
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
92 |
e = fmt.Errorf("connection closed: %s", err.Error()) |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
93 |
} else { |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
94 |
e = fmt.Errorf("read error: %s", err.Error()) |
117 | 95 |
} |
96 |
log.Printf("Stream Reader: %s", e.Error()) |
|
97 |
events <- StreamEvent{Event: "error", Error: e} |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
98 |
close(doneCh) |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
99 |
return |
117 | 100 |
} |
101 |
||
102 |
if partial { |
|
103 |
e := fmt.Errorf("received incomplete line; not supported yet") |
|
104 |
log.Printf("Stream Reader: %s", e.Error()) |
|
105 |
events <- StreamEvent{Event: "error", Error: e} |
|
106 |
time.Sleep(5 * time.Second) |
|
107 |
continue // Skip this |
|
108 |
} |
|
109 |
||
110 |
line = string(bytes.TrimSpace(lineBytes)) |
|
111 |
||
112 |
if line == "" { |
|
113 |
continue // Skip empty line |
|
114 |
} |
|
115 |
if strings.HasPrefix(line, ":") { |
|
116 |
continue // Skip comment |
|
117 |
} |
|
118 |
||
119 |
if strings.HasPrefix(line, "event: ") { |
|
120 |
eventName = line[7:] |
|
121 |
continue |
|
122 |
} |
|
123 |
||
124 |
if !strings.HasPrefix(line, "data: ") { |
|
125 |
// XXX Needs improvement |
|
126 |
e := fmt.Errorf("received unhandled event line '%s'", strings.Split(line, ":")[0]) |
|
127 |
log.Printf("Stream Reader: %s", e.Error()) |
|
128 |
events <- StreamEvent{Event: "error", Error: e} |
|
129 |
continue |
|
130 |
} |
|
131 |
||
132 |
// This is a data line |
|
133 |
data := []byte(line[6:]) |
|
134 |
||
135 |
var obj interface{} |
|
136 |
||
137 |
// Decode API object |
|
138 |
switch eventName { |
|
139 |
case "update": |
|
140 |
var s Status |
|
141 |
if err := json.Unmarshal(data, &s); err != nil { |
|
142 |
e := fmt.Errorf("could not unmarshal data: %s", err.Error()) |
|
143 |
log.Printf("Stream Reader: %s", e.Error()) |
|
144 |
events <- StreamEvent{Event: "error", Error: e} |
|
145 |
continue |
|
146 |
} |
|
147 |
obj = s |
|
148 |
case "notification": |
|
149 |
var notif Notification |
|
150 |
if err := json.Unmarshal(data, ¬if); err != nil { |
|
151 |
e := fmt.Errorf("could not unmarshal data: %s", err.Error()) |
|
152 |
log.Printf("Stream Reader: %s", e.Error()) |
|
153 |
events <- StreamEvent{Event: "error", Error: e} |
|
154 |
continue |
|
155 |
} |
|
156 |
obj = notif |
|
157 |
case "delete": |
|
158 |
var statusID int |
|
159 |
if err := json.Unmarshal(data, &statusID); err != nil { |
|
160 |
e := fmt.Errorf("could not unmarshal data: %s", err.Error()) |
|
161 |
log.Printf("Stream Reader: %s", e.Error()) |
|
162 |
events <- StreamEvent{Event: "error", Error: e} |
|
163 |
continue |
|
164 |
} |
|
165 |
obj = statusID |
|
166 |
case "": |
|
167 |
fallthrough |
|
168 |
default: |
|
169 |
e := fmt.Errorf("unhandled event '%s'", eventName) |
|
170 |
log.Printf("Stream Reader: %s", e.Error()) |
|
171 |
events <- StreamEvent{Event: "error", Error: e} |
|
172 |
continue |
|
173 |
} |
|
174 |
||
175 |
// Send event to the channel |
|
176 |
events <- StreamEvent{Event: eventName, Data: obj} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
// StreamListener listens to a stream from the Mastodon server |
|
181 |
// The stream 'name' can be "user", "public" or "hashtag". |
|
182 |
// For 'hashtag', the hashTag argument cannot be empty. |
|
183 |
// The events are sent to the events channel (the errors as well). |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
184 |
// The streaming is terminated if the 'stopCh' channel is closed. |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
185 |
// The 'doneCh' channel is closed if the connection is closed by the server. |
117 | 186 |
// Please note that this method launches a goroutine to listen to the events. |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
187 |
func (mc *Client) StreamListener(name, hashTag string, events chan<- StreamEvent, stopCh <-chan bool, doneCh chan<- bool) error { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
188 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
189 |
return ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
190 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
191 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
192 |
resp, err := mc.openStream(name, hashTag) |
117 | 193 |
if err != nil { |
194 |
return err |
|
195 |
} |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
196 |
go mc.readStream(events, stopCh, doneCh, resp) |
117 | 197 |
return nil |
198 |
} |