-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgroups.go
181 lines (146 loc) · 4.93 KB
/
groups.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 mailerlite
import (
"context"
"fmt"
"net/http"
)
const groupEndpoint = "/groups"
// GroupService defines an interface for group-related operations.
type GroupService interface {
List(ctx context.Context, options *ListGroupOptions) (*RootGroups, *Response, error)
Create(ctx context.Context, groupName string) (*RootGroup, *Response, error)
Update(ctx context.Context, groupID string, groupName string) (*RootGroup, *Response, error)
Delete(ctx context.Context, groupID string) (*Response, error)
Subscribers(ctx context.Context, options *ListGroupSubscriberOptions) (*RootSubscribers, *Response, error)
Assign(ctx context.Context, groupID, subscriberID string) (*RootGroup, *Response, error)
UnAssign(ctx context.Context, groupID, subscriberID string) (*Response, error)
}
// groupService implements GroupService.
type groupService struct {
*service
}
type RootGroup struct {
Data Group `json:"data"`
}
type RootGroups struct {
Data []Group `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type Group struct {
ID string `json:"id"`
Name string `json:"name"`
ActiveCount int `json:"active_count"`
SentCount int `json:"sent_count"`
OpensCount int `json:"opens_count"`
OpenRate OpenRate `json:"open_rate"`
ClicksCount int `json:"clicks_count"`
ClickRate ClickRate `json:"click_rate"`
UnsubscribedCount int `json:"unsubscribed_count"`
UnconfirmedCount int `json:"unconfirmed_count"`
BouncedCount int `json:"bounced_count"`
JunkCount int `json:"junk_count"`
CreatedAt string `json:"created_at"`
}
// ListGroupOptions - modifies the behavior of GroupService.List method
type ListGroupOptions struct {
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
Sort string `url:"sort,omitempty"`
}
type ListGroupSubscriberOptions struct {
GroupID string `url:"-"`
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
func (s *groupService) List(ctx context.Context, options *ListGroupOptions) (*RootGroups, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, groupEndpoint, options)
if err != nil {
return nil, nil, err
}
root := new(RootGroups)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *groupService) Create(ctx context.Context, groupName string) (*RootGroup, *Response, error) {
body := map[string]interface{}{"name": groupName}
req, err := s.client.newRequest(http.MethodPost, groupEndpoint, body)
if err != nil {
return nil, nil, err
}
root := new(RootGroup)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *groupService) Update(ctx context.Context, groupID, groupName string) (*RootGroup, *Response, error) {
body := map[string]interface{}{"name": groupName}
path := fmt.Sprintf("%s/%s", groupEndpoint, groupID)
req, err := s.client.newRequest(http.MethodPut, path, body)
if err != nil {
return nil, nil, err
}
root := new(RootGroup)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *groupService) Delete(ctx context.Context, groupID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", groupEndpoint, groupID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
res, err := s.client.do(ctx, req, nil)
if err != nil {
return res, err
}
return res, nil
}
func (s *groupService) Subscribers(ctx context.Context, options *ListGroupSubscriberOptions) (*RootSubscribers, *Response, error) {
path := fmt.Sprintf("%s/%s/subscribers", groupEndpoint, options.GroupID)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(RootSubscribers)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *groupService) Assign(ctx context.Context, groupID, subscriberID string) (*RootGroup, *Response, error) {
path := fmt.Sprintf("%s/%s/groups/%s", subscriberEndpoint, subscriberID, groupID)
req, err := s.client.newRequest(http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootGroup)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *groupService) UnAssign(ctx context.Context, groupID, subscriberID string) (*Response, error) {
path := fmt.Sprintf("%s/%s/groups/%s", subscriberEndpoint, subscriberID, groupID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
res, err := s.client.do(ctx, req, nil)
if err != nil {
return res, err
}
return res, nil
}