forked from Virtomize/mailtrain-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.go
160 lines (131 loc) · 3.52 KB
/
subscribe.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
package gomailtrain
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// Subscription type
type Subscription struct {
CID string `json:"-"`
Email string `json:"email"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Timezone string `json:"timezone"`
ForceSubscribe bool `json:"force_subscribe"`
RequireConfirmation bool `json:"require_confirmation"`
}
// SubscribeResponse type
type SubscribeResponse struct {
Data SubscribeData `json:"data"`
}
// SubscribeData type
type SubscribeData struct {
ID string `json:"id"`
}
// UnsubscribeResponse type
type UnsubscribeResponse struct {
Data UnsubscribeData `json:"data"`
}
// UnsubscribeData type
type UnsubscribeData struct {
ID int `json:"id"`
Unsubscribed bool `json:"unsubscribed"`
}
// DeleteSubscriptionResponse type
type DeleteSubscriptionResponse struct {
Data DeleteSubscriptionData `json:"data"`
}
// DeleteSubscriptionData type
type DeleteSubscriptionData struct {
ID int `json:"id"`
Deleted bool `json:"deleted"`
}
// Subscribe to a list
func (a *API) Subscribe(s Subscription) (*SubscribeResponse, error) {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/subscribe/" + s.CID)
if err != nil {
return nil, err
}
// validate timezone
_, err = time.LoadLocation(s.Timezone)
if err != nil {
return nil, err
}
data := createData(s)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
res, err := a.Request(req)
if err != nil {
return nil, err
}
var resp SubscribeResponse
err = json.Unmarshal(res, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// Unsubscribe from a list
func (a *API) Unsubscribe(listCID string, email string) (*UnsubscribeResponse, error) {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/unsubscribe/" + listCID)
if err != nil {
return nil, err
}
if email == "" {
return nil, fmt.Errorf("email is empty")
}
data := url.Values{}
data.Set("email", email)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
res, err := a.Request(req)
if err != nil {
return nil, err
}
var resp UnsubscribeResponse
err = json.Unmarshal(res, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// DeleteSubscription .
func (a *API) DeleteSubscription(listCID string, email string) (*DeleteSubscriptionResponse, error) {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/delete/" + listCID)
if err != nil {
return nil, err
}
if email == "" {
return nil, fmt.Errorf("email is empty")
}
data := url.Values{}
data.Set("email", email)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
res, err := a.Request(req)
if err != nil {
return nil, err
}
var resp DeleteSubscriptionResponse
err = json.Unmarshal(res, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}