/
subscribers.go
135 lines (112 loc) · 3.9 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
package mailerlite
import (
"context"
"fmt"
"net/http"
)
// SubscribersService handles communication with the subscriber related
// methods of the MailerLite API.
//
// MailerLite API docs: https://developers.mailerlite.com/reference/subscribers
type SubscribersService service
// Subscriber represents an email subscriber.
type Subscriber struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Sent int `json:"sent"`
Opened int `json:"opened"`
Clicked int `json:"clicked"`
Type SubscriptionType `json:"type"`
// CountryID string `json:"country_id"`
SignupIP *string `json:"signup_ip"`
SignupTimestamp *string `json:"signup_timestamp"`
ConfirmationIP *string `json:"confirmation_ip"`
ConfirmationTimestamp *string `json:"confirmation_timestamp"`
Fields []SubscriberField `json:"fields"`
DateSubscribe *Timestamp `json:"date_subscribe"`
DateUnsubscribe *Timestamp `json:"date_unsubscribe"`
DateCreated Timestamp `json:"date_created"`
DateUpdated *Timestamp `json:"date_updated"`
}
// SubscriptionType represents the current state of the subscription.
type SubscriptionType string
const (
Unsubscribed SubscriptionType = "unsubscribed"
Active SubscriptionType = "active"
Unconfirmed SubscriptionType = "unconfirmed"
Bounced SubscriptionType = "bounced"
Junk SubscriptionType = "junk"
)
// SubscriberField represents a custom field and its value.
type SubscriberField struct {
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
}
// SubscriberListOptions specifies the optional parameters to the
// SubscribersService.List method.
type SubscriberListOptions struct {
Type SubscriptionType `url:"type,omitempty"`
ListOptions
}
// List all subscribers.
//
// MailerLite API docs: https://developers.mailerlite.com/reference/subscribers
func (s *SubscribersService) List(ctx context.Context, opts *SubscriberListOptions) ([]Subscriber, *Response, error) {
u := "subscribers"
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var subscribers []Subscriber
resp, err := s.client.Do(req, &subscribers)
if err != nil {
return nil, resp, err
}
return subscribers, resp, nil
}
// TODO: create
// Get fetches a subscriber.
//
// MailerLite API docs: https://developers.mailerlite.com/reference/single-subscriber
func (s *SubscribersService) Get(ctx context.Context, email string) (*Subscriber, *Response, error) {
u := fmt.Sprintf("subscribers/%s", email)
req, err := s.client.NewRequest(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var subscriber Subscriber
resp, err := s.client.Do(req, &subscriber)
if err != nil {
return nil, resp, err
}
return &subscriber, resp, nil
}
// SubscriberUpdate
type SubscriberUpdate struct {
Name string `json:"name,omitempty"`
Type SubscriptionType `json:"type,omitempty"`
Fields []SubscriberField `json:"fields,omitempty"`
ResendAutoresponders *bool `json:"resend_autoresponders,omitempty"`
}
// Update updates a subscriber.
//
// MailerLite API docs: https://developers.mailerlite.com/reference/update-subscriber
func (s *SubscribersService) Update(ctx context.Context, email string, update SubscriberUpdate) (*Subscriber, *Response, error) {
u := fmt.Sprintf("subscribers/%s", email)
req, err := s.client.NewRequest(ctx, http.MethodPut, u, update)
if err != nil {
return nil, nil, err
}
var subscriber Subscriber
resp, err := s.client.Do(req, &subscriber)
if err != nil {
return nil, resp, err
}
return &subscriber, resp, nil
}