-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
242 lines (182 loc) · 6.65 KB
/
main.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
package improvmx
import (
"encoding/json"
"fmt"
resty "github.com/go-resty/resty/v2"
)
type Client struct {
BaseURL string
AccessToken string
Http *resty.Request
}
type Response struct {
Success bool `json:"success"`
Total int `json:"total"`
Code int `json:"code"`
Errors struct {
Domain []string `json:"domain"`
Alias []string `json:"alias"`
Account []string `json:"account"`
} `json:"errors"`
Error string `json:"error"`
Records struct {
Valid bool `json:"valid"`
Provider string `json:"provider"`
Advanced bool `json:"advanced"`
Mx struct {
Valid bool `json:"valid"`
Expected []string `json:"expected"`
Values []string `json:"values"`
} `json:"mx"`
Spf struct {
Valid bool `json:"valid"`
Expected string `json:"expected"`
Values string `json:"values"`
} `json:"spf"`
}
Account struct {
Plan struct {
Display string `json:"display"`
} `json:"plan"`
} `json:"account"`
Alias struct {
Forward string `json:"forward"`
Alias string `json:"alias"`
Id int64 `json:"id"`
} `json:"alias"`
Domain struct {
Domain string `json:"domain"`
Whitelabel string `json:"whitelabel"`
NotificationEmail string `json:"notification_email"`
Aliases []struct {
Id int64 `json:"id"`
}
} `json:"domain"`
Credential struct {
Created int64 `json:"created"`
Usage int `json:"usage"`
Username string `json:"username"`
} `json:"credential"`
Credentials []struct {
Created int64 `json:"created"`
Usage int `json:"usage"`
Username string `json:"username"`
} `json:"credentials"`
}
func NewClient(accessToken string) *Client {
return &Client{
BaseURL: "https://api.improvmx.com/v3",
AccessToken: accessToken,
Http: resty.New().R(),
}
}
// https://improvmx.com/api/#authentication handler
func (client *Client) setHeaders() *resty.Request {
if client.AccessToken == "" {
fmt.Println("ERROR: An ImprovMX API access token is required. Create one at https://app.improvmx.com/api.")
return nil
}
return client.Http.SetAuthScheme("Basic").SetAuthToken(fmt.Sprintf("api:%s", client.AccessToken)).SetHeader("Content-Type", "application/json")
}
// https://improvmx.com/api/#account handler
func (client *Client) AccountDetails() Response {
resp, _ := client.setHeaders().Get(fmt.Sprintf("%s/account", client.BaseURL))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) GetDomain(domain string) Response {
resp, _ := client.setHeaders().Get(fmt.Sprintf("%s/domains/%s", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
// https://improvmx.com/api/#domains-add
func (client *Client) CreateDomain(domain, notificationEmail, whitelabel string) Response {
domainInput, _ := json.Marshal(map[string]string{
"domain": domain,
"notification_email": notificationEmail,
"whitelabel": whitelabel,
})
resp, _ := client.setHeaders().SetBody(domainInput).Post(fmt.Sprintf("%s/domains/", client.BaseURL))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) UpdateDomain(domain, notificationEmail, whitelabel string) Response {
domainInput, _ := json.Marshal(map[string]string{
"domain": domain,
"notification_email": notificationEmail,
"whitelabel": whitelabel,
})
resp, _ := client.setHeaders().SetBody(domainInput).Put(fmt.Sprintf("%s/domains/%s", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
// https://improvmx.com/api/#domain-delete
func (client *Client) DeleteDomain(domain string) Response {
resp, _ := client.setHeaders().Delete(fmt.Sprintf("%s/domains/%s", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) GetDomainCheck(domain string) Response {
resp, _ := client.setHeaders().Get(fmt.Sprintf("%s/domains/%s/check", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) CreateEmailForward(domain, alias, forward string) Response {
emailForwardInput, _ := json.Marshal(map[string]string{"alias": alias, "forward": forward})
resp, _ := client.setHeaders().SetBody(emailForwardInput).Post(fmt.Sprintf("%s/domains/%s/aliases", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) GetEmailForward(domain, alias string) Response {
resp, _ := client.setHeaders().Get(fmt.Sprintf("%s/domains/%s/aliases/%s", client.BaseURL, domain, alias))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) UpdateEmailForward(domain, alias, forward string) Response {
emailForwardInput, _ := json.Marshal(map[string]string{"forward": forward})
resp, _ := client.setHeaders().SetBody(emailForwardInput).Put(fmt.Sprintf("%s/domains/%s/aliases/%s", client.BaseURL, domain, alias))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
// https://improvmx.com/api/#alias-delete
func (client *Client) DeleteEmailForward(domain, alias string) Response {
resp, _ := client.setHeaders().Delete(fmt.Sprintf("%s/domains/%s/aliases/%s", client.BaseURL, domain, alias))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) GetSMTPCredential(domain string) Response {
resp, _ := client.setHeaders().Get(fmt.Sprintf("%s/domains/%s/credentials", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) CreateSMTPCredential(domain, username, password string) Response {
credentialsInput, _ := json.Marshal(map[string]string{"username": username, "password": password})
resp, _ := client.setHeaders().SetBody(credentialsInput).Post(fmt.Sprintf("%s/domains/%s/credentials", client.BaseURL, domain))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) UpdateSMTPCredential(domain, username, password string) Response {
credentialsInput, _ := json.Marshal(map[string]string{"password": password})
resp, _ := client.setHeaders().SetBody(credentialsInput).Put(fmt.Sprintf("%s/domains/%s/credentials/%s", client.BaseURL, domain, username))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}
func (client *Client) DeleteSMTPCredential(domain, username string) Response {
resp, _ := client.setHeaders().Delete(fmt.Sprintf("%s/domains/%s/credentials/%s", client.BaseURL, domain, username))
parsed := Response{}
json.Unmarshal(resp.Body(), &parsed)
return parsed
}