-
Notifications
You must be signed in to change notification settings - Fork 9
/
domains.go
248 lines (197 loc) · 7.2 KB
/
domains.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package resend
import (
"context"
"encoding/json"
"errors"
"net/http"
)
type TlsOption = string
const (
Enforced TlsOption = "enforced"
Opportunistic TlsOption = "opportunistic"
)
type DomainsSvc interface {
CreateWithContext(ctx context.Context, params *CreateDomainRequest) (CreateDomainResponse, error)
Create(params *CreateDomainRequest) (CreateDomainResponse, error)
VerifyWithContext(ctx context.Context, domainId string) (bool, error)
Verify(domainId string) (bool, error)
ListWithContext(ctx context.Context) (ListDomainsResponse, error)
List() (ListDomainsResponse, error)
GetWithContext(ctx context.Context, domainId string) (Domain, error)
Get(domainId string) (Domain, error)
RemoveWithContext(ctx context.Context, domainId string) (bool, error)
Remove(domainId string) (bool, error)
UpdateWithContext(ctx context.Context, domainId string, params *UpdateDomainRequest) (Domain, error)
Update(domainId string, params *UpdateDomainRequest) (Domain, error)
}
type DomainsSvcImpl struct {
client *Client
}
type CreateDomainRequest struct {
Name string `json:"name"`
Region string `json:"region,omitempty"`
}
type CreateDomainResponse struct {
Id string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"createdAt"`
Status string `json:"status"`
Records []Record `json:"records"`
Region string `json:"region"`
DnsProvider string `json:"dnsProvider"`
}
type ListDomainsResponse struct {
Data []Domain `json:"data"`
}
type UpdateDomainRequest struct {
OpenTracking bool `json:"open_tracking,omitempty"`
ClickTracking bool `json:"click_tracking,omitempty"`
Tls TlsOption `json:"tls,omitempty"`
}
type Domain struct {
Id string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
Region string `json:"region,omitempty"`
}
type Record struct {
Record string `json:"record"`
Name string `json:"name"`
Type string `json:"type"`
Ttl string `json:"ttl"`
Status string `json:"status"`
Value string `json:"value"`
Priority json.Number `json:"priority,omitempty"`
}
// UpdateWithContext updates an existing Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/update-domain
func (s *DomainsSvcImpl) UpdateWithContext(ctx context.Context, domainId string, params *UpdateDomainRequest) (Domain, error) {
path := "domains/" + domainId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, params)
if err != nil {
return Domain{}, errors.New("[ERROR]: Failed to create Domains.Update request")
}
domainUpdatedResp := new(Domain)
// Send Request
_, err = s.client.Perform(req, domainUpdatedResp)
if err != nil {
return Domain{}, err
}
return *domainUpdatedResp, nil
}
// Update is a wrapper around UpdateWithContext
func (s *DomainsSvcImpl) Update(domainId string, params *UpdateDomainRequest) (Domain, error) {
return s.UpdateWithContext(context.Background(), domainId, params)
}
// CreateWithContext creates a new Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/create-domain
func (s *DomainsSvcImpl) CreateWithContext(ctx context.Context, params *CreateDomainRequest) (CreateDomainResponse, error) {
path := "domains"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateDomainResponse{}, errors.New("[ERROR]: Failed to create Domains.Create request")
}
// Build response recipient obj
domainsResp := new(CreateDomainResponse)
// Send Request
_, err = s.client.Perform(req, domainsResp)
if err != nil {
return CreateDomainResponse{}, err
}
return *domainsResp, nil
}
// Create creates a new Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/create-domain
func (s *DomainsSvcImpl) Create(params *CreateDomainRequest) (CreateDomainResponse, error) {
return s.CreateWithContext(context.Background(), params)
}
// VerifyWithContext verifies a given domain Id
// https://resend.com/docs/api-reference/domains/verify-domain
func (s *DomainsSvcImpl) VerifyWithContext(ctx context.Context, domainId string) (bool, error) {
path := "domains/" + domainId + "/verify"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return false, errors.New("[ERROR]: Failed to create Domains.Verify request")
}
// Send Request
_, err = s.client.Perform(req, nil)
if err != nil {
return false, err
}
return true, nil
}
// Verify verifies a given domain Id
// https://resend.com/docs/api-reference/domains/verify-domain
func (s *DomainsSvcImpl) Verify(domainId string) (bool, error) {
return s.VerifyWithContext(context.Background(), domainId)
}
// ListWithContext returns the list of all domains
// https://resend.com/docs/api-reference/domains/list-domains
func (s *DomainsSvcImpl) ListWithContext(ctx context.Context) (ListDomainsResponse, error) {
path := "domains"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListDomainsResponse{}, errors.New("[ERROR]: Failed to create Domains.List request")
}
domains := new(ListDomainsResponse)
// Send Request
_, err = s.client.Perform(req, domains)
if err != nil {
return ListDomainsResponse{}, err
}
return *domains, nil
}
// List returns the list of all domains
// https://resend.com/docs/api-reference/domains/list-domains
func (s *DomainsSvcImpl) List() (ListDomainsResponse, error) {
return s.ListWithContext(context.Background())
}
// RemoveWithContext removes a given domain entry by id
// https://resend.com/docs/api-reference/domains/delete-domain
func (s *DomainsSvcImpl) RemoveWithContext(ctx context.Context, domainId string) (bool, error) {
path := "domains/" + domainId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return false, errors.New("[ERROR]: Failed to create Domains.Remove request")
}
// Send Request
_, err = s.client.Perform(req, nil)
if err != nil {
return false, err
}
return true, nil
}
// Remove removes a given domain entry by id
// https://resend.com/docs/api-reference/domains/delete-domain
func (s *DomainsSvcImpl) Remove(domainId string) (bool, error) {
return s.RemoveWithContext(context.Background(), domainId)
}
// GetWithContext retrieves a domain object
// https://resend.com/docs/api-reference/domains/get-domain
func (s *DomainsSvcImpl) GetWithContext(ctx context.Context, domainId string) (Domain, error) {
path := "domains/" + domainId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return Domain{}, errors.New("[ERROR]: Failed to create Domains.Get request")
}
domain := new(Domain)
// Send Request
_, err = s.client.Perform(req, domain)
if err != nil {
return Domain{}, err
}
return *domain, nil
}
// Get retrieves a domain object
// https://resend.com/docs/api-reference/domains/get-domain
func (s *DomainsSvcImpl) Get(domainId string) (Domain, error) {
return s.GetWithContext(context.Background(), domainId)
}