-
Notifications
You must be signed in to change notification settings - Fork 9
/
audiences.go
167 lines (131 loc) · 4.81 KB
/
audiences.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
package resend
import (
"context"
"errors"
"net/http"
)
type AudiencesSvc interface {
CreateWithContext(ctx context.Context, params *CreateAudienceRequest) (CreateAudienceResponse, error)
Create(params *CreateAudienceRequest) (CreateAudienceResponse, error)
ListWithContext(ctx context.Context) (ListAudiencesResponse, error)
List() (ListAudiencesResponse, error)
GetWithContext(ctx context.Context, audienceId string) (Audience, error)
Get(audienceId string) (Audience, error)
RemoveWithContext(ctx context.Context, audienceId string) (RemoveAudienceResponse, error)
Remove(audienceId string) (RemoveAudienceResponse, error)
}
type AudiencesSvcImpl struct {
client *Client
}
type CreateAudienceRequest struct {
Name string `json:"name"`
}
type CreateAudienceResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Object string `json:"object"`
}
type RemoveAudienceResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
}
type ListAudiencesResponse struct {
Object string `json:"object"`
Data []Audience `json:"data"`
}
type Audience struct {
Id string `json:"id"`
Name string `json:"name"`
Object string `json:"object"`
CreatedAt string `json:"created_at"`
}
// CreateWithContext creates a new Audience entry based on the given params
// https://resend.com/docs/api-reference/audiences/create-audience
func (s *AudiencesSvcImpl) CreateWithContext(ctx context.Context, params *CreateAudienceRequest) (CreateAudienceResponse, error) {
path := "audiences"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateAudienceResponse{}, errors.New("[ERROR]: Failed to create Audiences.Create request")
}
// Build response recipient obj
audiencesResp := new(CreateAudienceResponse)
// Send Request
_, err = s.client.Perform(req, audiencesResp)
if err != nil {
return CreateAudienceResponse{}, err
}
return *audiencesResp, nil
}
// Create creates a new Audience entry based on the given params
// https://resend.com/docs/api-reference/audiences/create-audience
func (s *AudiencesSvcImpl) Create(params *CreateAudienceRequest) (CreateAudienceResponse, error) {
return s.CreateWithContext(context.Background(), params)
}
// ListWithContext returns the list of all audiences
// https://resend.com/docs/api-reference/audiences/list-audiences
func (s *AudiencesSvcImpl) ListWithContext(ctx context.Context) (ListAudiencesResponse, error) {
path := "audiences"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListAudiencesResponse{}, errors.New("[ERROR]: Failed to create Audiences.List request")
}
audiences := new(ListAudiencesResponse)
// Send Request
_, err = s.client.Perform(req, audiences)
if err != nil {
return ListAudiencesResponse{}, err
}
return *audiences, nil
}
// List returns the list of all audiences
// https://resend.com/docs/api-reference/audiences/list-audiences
func (s *AudiencesSvcImpl) List() (ListAudiencesResponse, error) {
return s.ListWithContext(context.Background())
}
// RemoveWithContext removes a given audience by id
// https://resend.com/docs/api-reference/audiences/delete-audience
func (s *AudiencesSvcImpl) RemoveWithContext(ctx context.Context, audienceId string) (RemoveAudienceResponse, error) {
path := "audiences/" + audienceId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return RemoveAudienceResponse{}, errors.New("[ERROR]: Failed to create Audience.Remove request")
}
resp := new(RemoveAudienceResponse)
// Send Request
_, err = s.client.Perform(req, resp)
if err != nil {
return RemoveAudienceResponse{}, err
}
return *resp, nil
}
// Remove removes a given audience entry by id
// https://resend.com/docs/api-reference/audiences/delete-audience
func (s *AudiencesSvcImpl) Remove(audienceId string) (RemoveAudienceResponse, error) {
return s.RemoveWithContext(context.Background(), audienceId)
}
// GetWithContext Retrieve a single audience.
// https://resend.com/docs/api-reference/audiences/get-audience
func (s *AudiencesSvcImpl) GetWithContext(ctx context.Context, audienceId string) (Audience, error) {
path := "audiences/" + audienceId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return Audience{}, errors.New("[ERROR]: Failed to create Audience.Get request")
}
audience := new(Audience)
// Send Request
_, err = s.client.Perform(req, audience)
if err != nil {
return Audience{}, err
}
return *audience, nil
}
// Get Retrieve a single audience.
// https://resend.com/docs/api-reference/audiences/get-audience
func (s *AudiencesSvcImpl) Get(audienceId string) (Audience, error) {
return s.GetWithContext(context.Background(), audienceId)
}