forked from pili-engineering/pili-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
75 lines (69 loc) · 1.69 KB
/
hub.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
package pili
import (
"fmt"
)
type Hub struct {
rpc *RPC
hubName string
}
func NewHub(creds *Credentials, hubName string) Hub {
return Hub{rpc: NewRPC(creds), hubName: hubName}
}
func (c Hub) CreateStream(args OptionalArguments) (stream Stream, err error) {
data := map[string]interface{}{"hub": c.hubName}
if args.Title != "" {
data["title"] = args.Title
}
if args.PublishKey != "" {
data["publishKey"] = args.PublishKey
}
if args.PublishSecurity != "" {
data["publishSecurity"] = args.PublishSecurity
}
url := fmt.Sprintf("%s/streams", getApiBaseUrl())
err = c.rpc.PostCall(&stream, url, data)
if err != nil {
return
}
stream.rpc = c.rpc
return
}
func (c Hub) GetStream(id string) (stream Stream, err error) {
url := fmt.Sprintf("%s/streams/%s", getApiBaseUrl(), id)
err = c.rpc.GetCall(&stream, url)
if err != nil {
return
}
stream.rpc = c.rpc
return
}
func (c Hub) ListStreams(args OptionalArguments) (ret StreamList, err error) {
url := fmt.Sprintf("%s/streams?hub=%s", getApiBaseUrl(), c.hubName)
if args.Status != "" {
url = fmt.Sprintf("%s&status=%s", url, args.Status)
}
if args.Marker != "" {
url = fmt.Sprintf("%s&marker=%s", url, args.Marker)
}
if args.Limit > 0 {
url = fmt.Sprintf("%s&limit=%d", url, args.Limit)
}
if args.Title != "" {
url = fmt.Sprintf("%s&title=%s", url, args.Title)
}
resultWrapper := StreamList{}
err = c.rpc.GetCall(&resultWrapper, url)
if err != nil {
return
}
count := len(resultWrapper.Items)
streams := make([]*Stream, count)
for i := 0; i < count; i++ {
streams[i] = resultWrapper.Items[i]
streams[i].rpc = c.rpc
}
ret.Items = streams
ret.Marker = resultWrapper.Marker
ret.End = resultWrapper.End
return
}