-
Notifications
You must be signed in to change notification settings - Fork 9
/
contacts.go
233 lines (185 loc) · 7.33 KB
/
contacts.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
230
231
232
233
package resend
import (
"context"
"errors"
"net/http"
)
type ContactsSvc interface {
CreateWithContext(ctx context.Context, params *CreateContactRequest) (CreateContactResponse, error)
Create(params *CreateContactRequest) (CreateContactResponse, error)
ListWithContext(ctx context.Context, audienceId string) (ListContactsResponse, error)
List(audienceId string) (ListContactsResponse, error)
GetWithContext(ctx context.Context, audienceId, contactId string) (Contact, error)
Get(audienceId, contactId string) (Contact, error)
RemoveWithContext(ctx context.Context, audienceId, contactId string) (RemoveContactResponse, error)
Remove(audienceId, contactId string) (RemoveContactResponse, error)
UpdateWithContext(ctx context.Context, params *UpdateContactRequest) (UpdateContactResponse, error)
Update(params *UpdateContactRequest) (UpdateContactResponse, error)
}
type ContactsSvcImpl struct {
client *Client
}
type CreateContactRequest struct {
Email string `json:"email"`
AudienceId string `json:"audience_id"`
Unsubscribed bool `json:"unsubscribed,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
}
type UpdateContactRequest struct {
Id string `json:"id"`
AudienceId string `json:"audience_id"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Unsubscribed bool `json:"unsubscribed,omitempty"`
}
type UpdateContactResponse struct {
Data Contact `json:"data"`
Error struct{} `json:"error"` // Fix this
}
type CreateContactResponse struct {
Object string `json:"object"`
Id string `json:"id"`
}
type RemoveContactResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
}
type ListContactsResponse struct {
Object string `json:"object"`
Data []Contact `json:"data"`
}
type Contact struct {
Id string `json:"id"`
Email string `json:"email"`
Object string `json:"object"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
CreatedAt string `json:"created_at"`
Unsubscribed bool `json:"unsubscribed"`
}
// CreateWithContext creates a new Contact based on the given params
// https://resend.com/docs/api-reference/contacts/create-contact
func (s *ContactsSvcImpl) CreateWithContext(ctx context.Context, params *CreateContactRequest) (CreateContactResponse, error) {
if params.AudienceId == "" {
return CreateContactResponse{}, errors.New("[ERROR]: AudienceId is missing")
}
path := "audiences/" + params.AudienceId + "/contacts"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateContactResponse{}, errors.New("[ERROR]: Failed to create Contacts.Create request")
}
// Build response recipient obj
contactsResp := new(CreateContactResponse)
// Send Request
_, err = s.client.Perform(req, contactsResp)
if err != nil {
return CreateContactResponse{}, err
}
return *contactsResp, nil
}
// Create creates a new Contact entry based on the given params
// https://resend.com/docs/api-reference/contacts/create-contact
func (s *ContactsSvcImpl) Create(params *CreateContactRequest) (CreateContactResponse, error) {
return s.CreateWithContext(context.Background(), params)
}
// ListWithContext returns the list of all contacts in an audience
// https://resend.com/docs/api-reference/contacts/list-contacts
func (s *ContactsSvcImpl) ListWithContext(ctx context.Context, audienceId string) (ListContactsResponse, error) {
path := "audiences/" + audienceId + "/contacts"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListContactsResponse{}, errors.New("[ERROR]: Failed to create Contacts.List request")
}
contacts := new(ListContactsResponse)
// Send Request
_, err = s.client.Perform(req, contacts)
if err != nil {
return ListContactsResponse{}, err
}
return *contacts, nil
}
// List returns the list of all contacts in an audience
// https://resend.com/docs/api-reference/contacts/list-contacts
func (s *ContactsSvcImpl) List(audienceId string) (ListContactsResponse, error) {
return s.ListWithContext(context.Background(), audienceId)
}
// RemoveWithContext same as Remove but with context
// https://resend.com/docs/api-reference/contacts/delete-contact
func (s *ContactsSvcImpl) RemoveWithContext(ctx context.Context, audienceId, contactId string) (RemoveContactResponse, error) {
path := "audiences/" + audienceId + "/contacts/" + contactId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return RemoveContactResponse{}, errors.New("[ERROR]: Failed to create Contact.Remove request")
}
resp := new(RemoveContactResponse)
// Send Request
_, err = s.client.Perform(req, resp)
if err != nil {
return RemoveContactResponse{}, err
}
return *resp, nil
}
// Remove removes a given contact entry by id or email
//
// @param [contactId] - the contact id or contact email
//
// https://resend.com/docs/api-reference/contacts/delete-contact
func (s *ContactsSvcImpl) Remove(audienceId, contactId string) (RemoveContactResponse, error) {
return s.RemoveWithContext(context.Background(), audienceId, contactId)
}
// GetWithContext Retrieve a single contact.
// https://resend.com/docs/api-reference/contacts/get-contact
func (s *ContactsSvcImpl) GetWithContext(ctx context.Context, audienceId, contactId string) (Contact, error) {
path := "audiences/" + audienceId + "/contacts/" + contactId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return Contact{}, errors.New("[ERROR]: Failed to create Contact.Get request")
}
contact := new(Contact)
// Send Request
_, err = s.client.Perform(req, contact)
if err != nil {
return Contact{}, err
}
return *contact, nil
}
// Get Retrieve a single contact.
// https://resend.com/docs/api-reference/contacts/get-contact
func (s *ContactsSvcImpl) Get(audienceId, contactId string) (Contact, error) {
return s.GetWithContext(context.Background(), audienceId, contactId)
}
// UpdateWithContext updates an existing Contact based on the given params
// https://resend.com/docs/api-reference/contacts/update-contact
func (s *ContactsSvcImpl) UpdateWithContext(ctx context.Context, params *UpdateContactRequest) (UpdateContactResponse, error) {
if params.AudienceId == "" {
return UpdateContactResponse{}, errors.New("[ERROR]: AudienceId is missing")
}
if params.Id == "" {
return UpdateContactResponse{}, errors.New("[ERROR]: Id is missing")
}
path := "audiences/" + params.AudienceId + "/contacts/" + params.Id
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, params)
if err != nil {
return UpdateContactResponse{}, errors.New("[ERROR]: Failed to create Contacts.Update request")
}
// Build response recipient obj
contactsResp := new(UpdateContactResponse)
// Send Request
_, err = s.client.Perform(req, contactsResp)
if err != nil {
return UpdateContactResponse{}, err
}
return *contactsResp, nil
}
// UpdateWithContext updates an existing Contact based on the given params
// https://resend.com/docs/api-reference/contacts/update-contact
func (s *ContactsSvcImpl) Update(params *UpdateContactRequest) (UpdateContactResponse, error) {
return s.UpdateWithContext(context.Background(), params)
}