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_user.go
181 lines (138 loc) · 4.25 KB
/
ip_user.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
package twiliogo
import (
"encoding/json"
"net/url"
)
// IPUser is a IP Messaging User resource.
type IPUser struct {
Sid string `json:"sid"`
AccountSid string `json:"account_sid"`
ServiceSid string `json:"service_sid"`
RoleSid string `json:"role_sid"`
Identity string `json:"identity"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
URL string `json:"url"`
}
// IPUserList gives the results for querying the set of users. Returns the first page
// by default.
type IPUserList struct {
Client *TwilioIPMessagingClient
Users []IPUser `json:"users"`
Meta Meta `json:"meta"`
}
// NewIPUser creates a new IP Messaging User.
func NewIPUser(client *TwilioIPMessagingClient, serviceSid string, identity string, roleSid string) (*IPUser, error) {
var user *IPUser
params := url.Values{}
params.Set("Identity", identity)
params.Set("RoleSid", roleSid)
res, err := client.post(params, "/Services/"+serviceSid+"/Users.json")
if err != nil {
return user, err
}
user = new(IPUser)
err = json.Unmarshal(res, user)
return user, err
}
// GetIPUser returns information on the specified user.
func GetIPUser(client *TwilioIPMessagingClient, serviceSid, sid string) (*IPUser, error) {
var user *IPUser
res, err := client.get(url.Values{}, "/Services/"+serviceSid+"/Users/"+sid+".json")
if err != nil {
return nil, err
}
user = new(IPUser)
err = json.Unmarshal(res, user)
return user, err
}
// DeleteIPUser deletes the given IP user.
func DeleteIPUser(client *TwilioIPMessagingClient, serviceSid, sid string) error {
return client.delete("/Services/" + serviceSid + "/Users/" + sid)
}
// UpdateIPUser updates an existing IP Messaging user.
func UpdateIPUser(client *TwilioIPMessagingClient, serviceSid string, sid string, identity string, roleSid string) (*IPUser, error) {
var user *IPUser
params := url.Values{}
params.Set("Identity", identity)
params.Set("RoleSid", roleSid)
res, err := client.post(params, "/Services/"+serviceSid+"/Users/"+sid+".json")
if err != nil {
return user, err
}
user = new(IPUser)
err = json.Unmarshal(res, user)
return user, err
}
// ListIPUsers returns the first page of users.
func ListIPUsers(client *TwilioIPMessagingClient, serviceSid string) (*IPUserList, error) {
var userList *IPUserList
body, err := client.get(nil, "/Services/"+serviceSid+"/Users.json")
if err != nil {
return userList, err
}
userList = new(IPUserList)
userList.Client = client
err = json.Unmarshal(body, userList)
return userList, err
}
// GetUsers returns the current page of users.
func (s *IPUserList) GetUsers() []IPUser {
return s.Users
}
// GetAllUsers returns all of the users from all of the pages (from here forward).
func (s *IPUserList) GetAllUsers() ([]IPUser, error) {
users := s.Users
t := s
for t.HasNextPage() {
var err error
t, err = t.NextPage()
if err != nil {
return nil, err
}
users = append(users, t.Users...)
}
return users, nil
}
// HasNextPage returns whether or not there is a next page of users.
func (s *IPUserList) HasNextPage() bool {
return s.Meta.NextPageUri != ""
}
// NextPage returns the next page of users.
func (s *IPUserList) NextPage() (*IPUserList, error) {
if !s.HasNextPage() {
return nil, Error{"No next page"}
}
return s.getPage(s.Meta.NextPageUri)
}
// HasPreviousPage indicates whether or not there is a previous page of results.
func (s *IPUserList) HasPreviousPage() bool {
return s.Meta.PreviousPageUri != ""
}
// PreviousPage returns the previous page of users.
func (s *IPUserList) PreviousPage() (*IPUserList, error) {
if !s.HasPreviousPage() {
return nil, Error{"No previous page"}
}
return s.getPage(s.Meta.NextPageUri)
}
// FirstPage returns the first page of users.
func (s *IPUserList) FirstPage() (*IPUserList, error) {
return s.getPage(s.Meta.FirstPageUri)
}
// LastPage returns the last page of users.
func (s *IPUserList) LastPage() (*IPUserList, error) {
return s.getPage(s.Meta.LastPageUri)
}
func (s *IPUserList) getPage(uri string) (*IPUserList, error) {
var userList *IPUserList
client := s.Client
body, err := client.get(nil, uri)
if err != nil {
return userList, err
}
userList = new(IPUserList)
userList.Client = client
err = json.Unmarshal(body, userList)
return userList, err
}