This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
ip_channel.go
197 lines (154 loc) · 5.16 KB
/
ip_channel.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package twiliogo
import (
"encoding/json"
"net/url"
)
// IPChannel is a IP Messaging Channel resource.
type IPChannel struct {
Sid string `json:"sid"`
AccountSid string `json:"account_sid"`
ServiceSid string `json:"service_sid"`
FriendlyName string `json:"friendly_name"`
UniqueName string `json:"unique_name"`
Attributes string `json:"attributes"`
Type string `json:"type"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
CreatedBy string `json:"created_by"`
URL string `json:"url"`
Links map[string]string `json:"links"`
}
// IPChannelList gives the results for querying the set of channels. Returns the first page
// by default.
type IPChannelList struct {
Client Client
Channels []IPChannel `json:"channels"`
Meta Meta `json:"meta"`
}
// NewIPChannel creates a new IP Messaging Channel.
func NewIPChannel(client *TwilioIPMessagingClient, serviceSid string, friendlyName string, uniqueName string, public bool, attributes string) (*IPChannel, error) {
var channel *IPChannel
params := url.Values{}
params.Set("FriendlyName", friendlyName)
params.Set("UniqueName", uniqueName)
kind := "private"
if public {
kind = "public"
}
params.Set("Type", kind)
params.Set("Attributes", attributes)
res, err := client.post(params, "/Services/"+serviceSid+"/Channels.json")
if err != nil {
return channel, err
}
channel = new(IPChannel)
err = json.Unmarshal(res, channel)
return channel, err
}
// UpdateIPChannel updates ane existing IP Messaging Channel.
func UpdateIPChannel(client *TwilioIPMessagingClient, serviceSid string, sid string, friendlyName string, uniqueName string, public bool, attributes string) (*IPChannel, error) {
var channel *IPChannel
params := url.Values{}
params.Set("FriendlyName", friendlyName)
params.Set("UniqueName", uniqueName)
kind := "private"
if public {
kind = "public"
}
params.Set("Type", kind)
params.Set("Attributes", attributes)
res, err := client.post(params, "/Services/"+serviceSid+"/Channels/"+sid+".json")
if err != nil {
return channel, err
}
channel = new(IPChannel)
err = json.Unmarshal(res, channel)
return channel, err
}
// GetIPChannel returns the specified IP Channel.
func GetIPChannel(client *TwilioIPMessagingClient, serviceSid string, sid string) (*IPChannel, error) {
var channel *IPChannel
res, err := client.get(url.Values{}, "/Services/"+serviceSid+"/Channels/"+sid+".json")
if err != nil {
return nil, err
}
channel = new(IPChannel)
err = json.Unmarshal(res, channel)
return channel, err
}
// DeleteIPChannel deletes the given IP Channel.
func DeleteIPChannel(client *TwilioIPMessagingClient, serviceSid, sid string) error {
return client.delete("/Services/" + serviceSid + "/Channels/" + sid)
}
// ListIPChannels returns the first page of channels.
func ListIPChannels(client *TwilioIPMessagingClient, serviceSid string) (*IPChannelList, error) {
var channelList *IPChannelList
body, err := client.get(nil, "/Services/"+serviceSid+"/Channels.json")
if err != nil {
return channelList, err
}
channelList = new(IPChannelList)
channelList.Client = client
err = json.Unmarshal(body, channelList)
return channelList, err
}
// GetChannels recturns the current page of channels.
func (c *IPChannelList) GetChannels() []IPChannel {
return c.Channels
}
// GetAllChannels returns all of the channels from all of the pages (from here forward).
func (c *IPChannelList) GetAllChannels() ([]IPChannel, error) {
channels := c.Channels
t := c
for t.HasNextPage() {
var err error
t, err = t.NextPage()
if err != nil {
return nil, err
}
channels = append(channels, t.Channels...)
}
return channels, nil
}
// HasNextPage returns whether or not there is a next page of channels.
func (c *IPChannelList) HasNextPage() bool {
return c.Meta.NextPageUri != ""
}
// NextPage returns the next page of channels.
func (c *IPChannelList) NextPage() (*IPChannelList, error) {
if !c.HasNextPage() {
return nil, Error{"No next page"}
}
return c.getPage(c.Meta.NextPageUri)
}
// HasPreviousPage indicates whether or not there is a previous page of results.
func (c *IPChannelList) HasPreviousPage() bool {
return c.Meta.PreviousPageUri != ""
}
// PreviousPage returns the previous page of channels.
func (c *IPChannelList) PreviousPage() (*IPChannelList, error) {
if !c.HasPreviousPage() {
return nil, Error{"No previous page"}
}
return c.getPage(c.Meta.NextPageUri)
}
// FirstPage returns the first page of channels.
func (c *IPChannelList) FirstPage() (*IPChannelList, error) {
return c.getPage(c.Meta.FirstPageUri)
}
// LastPage returns the last page of channels.
func (c *IPChannelList) LastPage() (*IPChannelList, error) {
return c.getPage(c.Meta.LastPageUri)
}
func (c *IPChannelList) getPage(uri string) (*IPChannelList, error) {
var channelList *IPChannelList
client := c.Client
body, err := client.get(nil, uri)
if err != nil {
return channelList, err
}
channelList = new(IPChannelList)
channelList.Client = client
err = json.Unmarshal(body, channelList)
return channelList, err
}