@@ -21,6 +21,8 @@ type Client struct {
21
21
}
22
22
23
23
// Default timeout options.
24
+ //
25
+ //nolint:gochecknoglobals
24
26
var (
25
27
DialTimeout = time .Second * 5
26
28
AuthTimeout = time .Second * 2
@@ -61,6 +63,7 @@ func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client
61
63
62
64
if err := conn .AuthTimeout (password , AuthTimeout ); err != nil {
63
65
rwc .Close ()
66
+
64
67
return nil , fmt .Errorf ("failed to auth: %w" , err )
65
68
}
66
69
@@ -80,7 +83,8 @@ func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client
80
83
// Close closes the client connection.
81
84
func (c * Client ) Close () error {
82
85
c .sendRecv (cmd ("exit" )) //nolint:errcheck // ignore send error
83
- return c .closer .Close ()
86
+
87
+ return c .closer .Close () //nolint:wrapcheck
84
88
}
85
89
86
90
// API sends a command to the API and returns the response body or an error.
@@ -107,7 +111,7 @@ func (c *Client) API(command string) (string, error) {
107
111
// When the command is done executing, FreeSWITCH fires an event with the result
108
112
// and you can compare that to the Job-UUID to see what the result was. In order
109
113
// to receive this event, you will need to subscribe to BACKGROUND_JOB events.
110
- func (c * Client ) Job (command string ) (id string , err error ) {
114
+ func (c * Client ) Job (command string ) (id string , err error ) { //nolint:nonamedreturns
111
115
resp , err := c .sendRecv (cmd ("bgapi" , command ))
112
116
if err != nil {
113
117
return "" , err
@@ -127,6 +131,7 @@ func (c *Client) Job(command string) (id string, err error) {
127
131
// to receive this event, you will need to subscribe to BACKGROUND_JOB events.
128
132
func (c * Client ) JobWithID (command , id string ) error {
129
133
_ , err := c .sendRecv (cmd ("bgapi" , command ).WithJobUUID (id ))
134
+
130
135
return err
131
136
}
132
137
@@ -138,14 +143,17 @@ func (c *Client) JobWithID(command, id string) error {
138
143
func (c * Client ) Subscribe (names ... string ) error {
139
144
cmdNames := buildEventNamesCmd (names ... )
140
145
_ , err := c .sendRecv (cmd ("event" , cmdNames ))
146
+
141
147
return err
142
148
}
143
149
144
150
// Unsubscribe unsubscribes the client from one or more events.
145
151
//
146
152
// Suppress the specified type of event.
147
153
// If name is empty then all events will be suppressed.
148
- func (c * Client ) Unsubscribe (names ... string ) (err error ) {
154
+ func (c * Client ) Unsubscribe (names ... string ) error {
155
+ var err error
156
+
149
157
cmdNames := buildEventNamesCmd (names ... )
150
158
if cmdNames == eventAll {
151
159
_ , err = c .sendRecv (cmd ("noevents" ))
@@ -176,6 +184,7 @@ func (c *Client) Unsubscribe(names ...string) (err error) {
176
184
// events for multiple users on a particular conference.
177
185
func (c * Client ) Filter (eventHeader , valueToFilter string ) error {
178
186
_ , err := c .sendRecv (cmd ("filter" , eventHeader , valueToFilter ))
187
+
179
188
return err
180
189
}
181
190
@@ -186,6 +195,7 @@ func (c *Client) Filter(eventHeader, valueToFilter string) error {
186
195
// is no use of the filter.
187
196
func (c * Client ) FilterDelete (eventHeader , valueToFilter string ) error {
188
197
_ , err := c .sendRecv (cmd ("filter delete" , eventHeader , valueToFilter ))
198
+
189
199
return err
190
200
}
191
201
@@ -202,6 +212,7 @@ func (c *Client) FilterDelete(eventHeader, valueToFilter string) error {
202
212
// use a filter.
203
213
func (c * Client ) MyEvent (uuid string ) error {
204
214
_ , err := c .sendRecv (cmd ("myevents" , uuid ))
215
+
205
216
return err
206
217
}
207
218
@@ -220,13 +231,15 @@ func (c *Client) DivertEvents(on ...bool) error {
220
231
}
221
232
222
233
_ , err := c .sendRecv (cmd ("divert_events" , val ))
234
+
223
235
return err
224
236
}
225
237
226
238
// Send an event into the event system.
227
239
func (c * Client ) SendEvent (name string , headers map [string ]string , body string ) error {
228
240
_ , err := c .sendRecv (
229
241
cmd ("sendevent" , name ).WithMessage (headers , body ))
242
+
230
243
return err
231
244
}
232
245
@@ -235,33 +248,44 @@ func (c *Client) SendEvent(name string, headers map[string]string, body string)
235
248
func (c * Client ) SendMsg (uuid string , headers map [string ]string , body string ) error {
236
249
_ , err := c .sendRecv (
237
250
cmd ("sendmsg" , uuid ).WithMessage (headers , body ))
251
+
238
252
return err
239
253
}
240
254
255
+ const (
256
+ commandReply = "command/reply"
257
+ disconnectNotice = "text/disconnect-notice"
258
+ eventPlain = "text/event-plain"
259
+ )
260
+
241
261
// runReader is a method of the Client struct that reads responses from the connection and handles them accordingly.
242
262
func (c * Client ) runReader (events chan <- Event , autoClose bool ) {
243
263
c .conn .log .Info ("esl: run response reading" )
264
+
244
265
defer func () {
245
266
close (c .chResp )
246
267
close (c .chErr )
268
+
247
269
if autoClose && events != nil {
248
270
close (events )
249
271
}
272
+
250
273
c .conn .log .Info ("esl: response reader stopped" )
251
274
}()
252
275
253
276
for {
254
277
resp , err := c .conn .Read ()
255
278
if err != nil {
256
279
c .chErr <- err
280
+
257
281
return // break on read error
258
282
}
259
283
260
- switch ct := resp .ContentType (); ct {
261
- case "api/response" , "command/reply" :
284
+ switch contentType := resp .ContentType (); contentType {
285
+ case "api/response" , commandReply :
262
286
c .chResp <- resp
263
287
264
- case "text/event-plain" :
288
+ case eventPlain :
265
289
if events == nil {
266
290
continue // ignore events if no events channel is provided
267
291
}
@@ -270,18 +294,19 @@ func (c *Client) runReader(events chan<- Event, autoClose bool) {
270
294
if err != nil {
271
295
c .conn .log .Error ("esl: failed to parse event" ,
272
296
slog .String ("err" , err .Error ()))
297
+
273
298
continue // ignore bad event
274
299
}
275
300
276
301
c .conn .log .Info ("esl: handle" , slog .Any ("event" , event ))
277
302
events <- event
278
303
279
- case "text/disconnect-notice" :
304
+ case disconnectNotice :
280
305
return // disconnect
281
306
282
307
default :
283
308
c .conn .log .Warn ("esl: unexpected response" ,
284
- slog .String ("content-type" , ct ))
309
+ slog .String ("content-type" , contentType ))
285
310
}
286
311
}
287
312
}
@@ -302,12 +327,14 @@ func (c *Client) read() (response, error) {
302
327
if ok {
303
328
return response {}, err
304
329
}
330
+
305
331
return response {}, io .EOF // connection closed
306
332
307
333
case resp := <- c .chResp :
308
334
if err := resp .AsErr (); err != nil {
309
335
return response {}, err // response with error message
310
336
}
337
+
311
338
return resp , nil
312
339
}
313
340
}
0 commit comments