-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscribers.go
166 lines (137 loc) · 4.52 KB
/
subscribers.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
package mailerlite
import (
"context"
"fmt"
"net/http"
)
const subscriberEndpoint = "/subscribers"
type SubscriberService service
// subscribers - subscribers response
type rootSubscribers struct {
Data []Subscriber `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
// subscribers - subscribers response
type rootSubscriber struct {
Data Subscriber `json:"data"`
}
type count struct {
Total int `json:"total"`
}
type Subscriber struct {
ID string `json:"id"`
Email string `json:"email"`
Status string `json:"status"`
Source string `json:"source"`
Sent int `json:"sent"`
OpensCount int `json:"opens_count"`
ClicksCount int `json:"clicks_count"`
OpenRate float64 `json:"open_rate"`
ClickRate float64 `json:"click_rate"`
IPAddress interface{} `json:"ip_address"`
SubscribedAt string `json:"subscribed_at"`
UnsubscribedAt interface{} `json:"unsubscribed_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Fields map[string]interface{} `json:"fields"`
Groups []Group `json:"groups"`
OptedInAt string `json:"opted_in_at"`
OptinIP string `json:"optin_ip"`
}
type Fields struct {
Name string `json:"name"`
LastName string `json:"last_name"`
}
// ListSubscriberOptions - modifies the behavior of SubscriberService.List method
type ListSubscriberOptions struct {
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
// GetSubscriberOptions - modifies the behavior of SubscriberService.Get method
type GetSubscriberOptions struct {
SubscriberID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
}
func (s *SubscriberService) List(ctx context.Context, options *ListSubscriberOptions) (*rootSubscribers, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, subscriberEndpoint, 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
}
// Count - get a count of subscribers
func (s *SubscriberService) Count(ctx context.Context) (*count, *Response, error) {
path := fmt.Sprintf("%s?limit=0", subscriberEndpoint)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(count)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
// Get - get a single subscriber by email or ID
func (s *SubscriberService) Get(ctx context.Context, options *GetSubscriberOptions) (*rootSubscriber, *Response, error) {
param := options.SubscriberID
if options.Email != "" {
param = options.Email
}
path := fmt.Sprintf("%s/%s", subscriberEndpoint, param)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *SubscriberService) Create(ctx context.Context, subscriber *Subscriber) (*rootSubscriber, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, subscriberEndpoint, subscriber)
if err != nil {
return nil, nil, err
}
root := new(rootSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *SubscriberService) Update(ctx context.Context, subscriber *Subscriber) (*rootSubscriber, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, subscriberEndpoint, subscriber)
if err != nil {
return nil, nil, err
}
root := new(rootSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *SubscriberService) Delete(ctx context.Context, subscriberID string) (*rootSubscriber, *Response, error) {
path := fmt.Sprintf("%s/%s", subscriberEndpoint, subscriberID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}