-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_subscription_preference.go
229 lines (197 loc) · 8.86 KB
/
user_subscription_preference.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ilert
import (
"encoding/json"
"errors"
"fmt"
)
// UserNotificationPreference definition https://api.ilert.com/api-docs/#tag/Notification-Preferences
type UserSubscriptionPreference struct {
ID int64 `json:"id,omitempty"`
Method string `json:"method"`
Contact *UserContactShort `json:"contact"` // is either UserEmailContact or UserPhoneNumberContact
}
// UserSubscriptionPreferenceMethodAll defines user subscription notification preference method list
var UserSubscriptionPreferenceMethodAll = []string{
UserPreferenceMethod.Email,
UserPreferenceMethod.Sms,
UserPreferenceMethod.Push,
}
// CreateUserSubscriptionPreferenceInput represents the input of a CreateUserSubscriptionPreference operation.
type CreateUserSubscriptionPreferenceInput struct {
_ struct{}
UserID *int64
UserSubscriptionPreference *UserSubscriptionPreference
}
// CreateUserSubscriptionPreferenceOutput represents the output of a CreateUserSubscriptionPreference operation.
type CreateUserSubscriptionPreferenceOutput struct {
_ struct{}
UserSubscriptionPreference *UserSubscriptionPreference
}
// CreateUserSubscriptionPreference creates a new subscription notification preference for a user. Requires ADMIN privileges or user id equals your current user. https://api.ilert.com/api-docs/#tag/Notification-Preferences/paths/~1users~1{user-id}~1notification-preferences~1subscriptions/post
func (c *Client) CreateUserSubscriptionPreference(input *CreateUserSubscriptionPreferenceInput) (*CreateUserSubscriptionPreferenceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil {
return nil, errors.New("user id is required")
}
if input.UserSubscriptionPreference == nil {
return nil, errors.New("user subscription notification preference input is required")
}
url := fmt.Sprintf("%s/%d/notification-preferences/subscriptions", apiRoutes.users, *input.UserID)
resp, err := c.httpClient.R().SetBody(input.UserSubscriptionPreference).Post(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
preference := &UserSubscriptionPreference{}
err = json.Unmarshal(resp.Body(), preference)
if err != nil {
return nil, err
}
return &CreateUserSubscriptionPreferenceOutput{UserSubscriptionPreference: preference}, nil
}
// GetUserSubscriptionPreferenceInput represents the input of a GetUserSubscriptionPreference operation.
type GetUserSubscriptionPreferenceInput struct {
_ struct{}
UserID *int64
UserSubscriptionPreferenceID *int64
}
// GetUserSubscriptionPreferenceOutput represents the output of a GetUserSubscriptionPreference operation.
type GetUserSubscriptionPreferenceOutput struct {
_ struct{}
UserSubscriptionPreference *UserSubscriptionPreference
}
// GetUserSubscriptionPreference gets an subscription notification preference of a user by id. https://api.ilert.com/api-docs/#tag/Notification-Preferences/paths/~1users~1{user-id}~1notification-preferences~1subscriptions~1{id}/get
func (c *Client) GetUserSubscriptionPreference(input *GetUserSubscriptionPreferenceInput) (*GetUserSubscriptionPreferenceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil {
return nil, errors.New("user id is required")
}
if input.UserSubscriptionPreferenceID == nil {
return nil, errors.New("user subscription notification preference id is required")
}
url := fmt.Sprintf("%s/%d/notification-preferences/subscriptions/%d", apiRoutes.users, *input.UserID, *input.UserSubscriptionPreferenceID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
preference := &UserSubscriptionPreference{}
err = json.Unmarshal(resp.Body(), preference)
if err != nil {
return nil, err
}
return &GetUserSubscriptionPreferenceOutput{UserSubscriptionPreference: preference}, nil
}
// GetUserSubscriptionPreferencesInput represents the input of a GetUserSubscriptionPreferences operation.
type GetUserSubscriptionPreferencesInput struct {
_ struct{}
UserID *int64
}
// GetUserSubscriptionPreferencesOutput represents the output of a GetUserSubscriptionPreferences operation.
type GetUserSubscriptionPreferencesOutput struct {
_ struct{}
UserSubscriptionPreferences []*UserSubscriptionPreference
}
// GetUserSubscriptionPreferences lists existing subscription notification preferences of a user. https://api.ilert.com/api-docs/#tag/Notification-Preferences/paths/~1users~1{user-id}~1notification-preferences~1subscriptions/get
func (c *Client) GetUserSubscriptionPreferences(input *GetUserSubscriptionPreferencesInput) (*GetUserSubscriptionPreferencesOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil {
return nil, errors.New("user id is required")
}
url := fmt.Sprintf("%s/%d/notification-preferences/subscriptions", apiRoutes.users, *input.UserID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
preferences := make([]*UserSubscriptionPreference, 0)
err = json.Unmarshal(resp.Body(), &preferences)
if err != nil {
return nil, err
}
return &GetUserSubscriptionPreferencesOutput{UserSubscriptionPreferences: preferences}, nil
}
// UpdateUserSubscriptionPreferenceInput represents the input of a UpdateUserSubscriptionPreference operation.
type UpdateUserSubscriptionPreferenceInput struct {
_ struct{}
UserID *int64
UserSubscriptionPreferenceID *int64
UserSubscriptionPreference *UserSubscriptionPreference
}
// UpdateUserSubscriptionPreferenceOutput represents the output of a UpdateUserSubscriptionPreference operation.
type UpdateUserSubscriptionPreferenceOutput struct {
_ struct{}
UserSubscriptionPreference *UserSubscriptionPreference
}
// UpdateUserSubscriptionPreference updates an existing subscription notification preference of a user. https://api.ilert.com/api-docs/#tag/Notification-Preferences/paths/~1users~1{user-id}~1notification-preferences~1subscriptions~1{id}/put
func (c *Client) UpdateUserSubscriptionPreference(input *UpdateUserSubscriptionPreferenceInput) (*UpdateUserSubscriptionPreferenceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil {
return nil, errors.New("user id is required")
}
if input.UserSubscriptionPreferenceID == nil {
return nil, errors.New("user subscription notification preference id is required")
}
if input.UserSubscriptionPreference == nil {
return nil, errors.New("user input is required")
}
url := fmt.Sprintf("%s/%d/notification-preferences/subscriptions/%d", apiRoutes.users, *input.UserID, *input.UserSubscriptionPreferenceID)
resp, err := c.httpClient.R().SetBody(input.UserSubscriptionPreference).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
preference := &UserSubscriptionPreference{}
err = json.Unmarshal(resp.Body(), preference)
if err != nil {
return nil, err
}
return &UpdateUserSubscriptionPreferenceOutput{UserSubscriptionPreference: preference}, nil
}
// DeleteUserSubscriptionPreferenceInput represents the input of a DeleteUserSubscriptionPreference operation.
type DeleteUserSubscriptionPreferenceInput struct {
_ struct{}
UserID *int64
UserSubscriptionPreferenceID *int64
}
// DeleteUserSubscriptionPreferenceOutput represents the output of a DeleteUserSubscriptionPreference operation.
type DeleteUserSubscriptionPreferenceOutput struct {
_ struct{}
}
// DeleteUserSubscriptionPreference deletes the specified subscription notification preference of a user. https://api.ilert.com/api-docs/#tag/Notification-Preferences/paths/~1users~1{user-id}~1notification-preferences~1subscriptions~1{id}/delete
func (c *Client) DeleteUserSubscriptionPreference(input *DeleteUserSubscriptionPreferenceInput) (*DeleteUserSubscriptionPreferenceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil {
return nil, errors.New("user id is required")
}
if input.UserSubscriptionPreferenceID == nil {
return nil, errors.New("user subscription notification preference id is required")
}
url := fmt.Sprintf("%s/%d/notification-preferences/subscriptions/%d", apiRoutes.users, *input.UserID, *input.UserSubscriptionPreferenceID)
resp, err := c.httpClient.R().Delete(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteUserSubscriptionPreferenceOutput{}, nil
}