forked from CyCoreSystems/ari-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
89 lines (74 loc) · 1.99 KB
/
application.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
package server
import (
"context"
"errors"
"strings"
"github.com/CyCoreSystems/ari-proxy/v5/proxy"
)
func (s *Server) applicationData(ctx context.Context, reply string, req *proxy.Request) {
data, err := s.ari.Application().Data(req.Key)
if err != nil {
s.sendError(reply, err)
return
}
s.publish(reply, &proxy.Response{
Data: &proxy.EntityData{
Application: data,
},
})
}
func (s *Server) applicationList(ctx context.Context, reply string, req *proxy.Request) {
list, err := s.ari.Application().List(nil)
if err != nil {
s.sendError(reply, err)
return
}
s.publish(reply, &proxy.Response{
Keys: list,
})
}
func (s *Server) applicationGet(ctx context.Context, reply string, req *proxy.Request) {
data, err := s.ari.Application().Data(req.Key)
if err != nil {
s.sendError(reply, err)
return
}
s.publish(reply, &proxy.Response{
Key: data.Key,
})
}
func parseEventSource(src string) (string, string, error) {
var err error
pieces := strings.Split(src, ":")
if len(pieces) != 2 {
return "", "", errors.New("Invalid EventSource")
}
switch pieces[0] {
case "channel":
case "bridge":
case "endpoint":
case "deviceState":
default:
err = errors.New("Unhandled EventSource type")
}
return pieces[0], pieces[1], err
}
func (s *Server) applicationSubscribe(ctx context.Context, reply string, req *proxy.Request) {
err := s.ari.Application().Subscribe(req.Key, req.ApplicationSubscribe.EventSource)
if err != nil {
s.sendError(reply, err)
return
}
if req.Key.Dialog != "" {
eType, eID, err := parseEventSource(req.ApplicationSubscribe.EventSource)
if err != nil {
s.Log.Warn("failed to parse event source", "error", err, "eventsource", req.ApplicationSubscribe.EventSource)
} else {
s.Dialog.Bind(req.Key.Dialog, eType, eID)
}
}
s.sendError(reply, nil)
}
func (s *Server) applicationUnsubscribe(ctx context.Context, reply string, req *proxy.Request) {
s.sendError(reply, s.ari.Application().Unsubscribe(req.Key, req.ApplicationSubscribe.EventSource))
}