-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
122 lines (104 loc) · 4.91 KB
/
account.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
package elasticemail
import (
"context"
"encoding/json"
"github.com/pkg/errors"
)
// Subaccount is the JSON structure accepted by ElasticEmail Subaccounts API.
// list of fields: https://api.elasticemail.com/public/help#Account_AddSubAccount
type Subaccount struct {
Email string
Password string
ConfirmPassword string
DailySendLimit int `json:",omitempty"`
EmailSizeLimit int `json:",omitempty"` // MB
EnableContactFeatures bool `json:",omitempty"`
EnableLitmusTest bool `json:",omitempty"`
EnablePrivateIPRequest bool `json:",omitempty"`
MaxContacts int `json:",omitempty"`
PoolName string `json:",omitempty"`
RequiresEmailCredits bool `json:",omitempty"`
RequiresLitmusCredits bool `json:",omitempty"`
SendActivation bool `json:",omitempty"`
SendingPermission sendingPermission `json:",omitempty"`
APIKey string `json:",omitempty"`
}
type sendingPermission byte
// sending permission enum
// https://api.elasticemail.com/public/help#classes_SendingPermission
const (
SendingPermissionAll sendingPermission = 255
SendingPermissionHTTPAPI = 2
SendingPermissionHTTPAPIAndInterface = 6
SendingPermissionInterface = 4
SendingPermissionNone = 0
SendingPermissionSMTP = 1
SendingPermissionSMTPAndHTTPAPI = 3
SendingPermissionSMTPAndInterface = 5
)
// AddSubAccount create new subaccount and provide most important data about it.
// https://api.elasticemail.com/public/help#Account_AddSubAccount
func (c *Client) AddSubAccount(s *Subaccount) *Response {
return c.AddSubAccountContext(context.Background(), s)
}
// AddSubAccountContext is the same as AddSubAccount, and it allows the caller to pass in a context
func (c *Client) AddSubAccountContext(ctx context.Context, s *Subaccount) *Response {
if s.ConfirmPassword == "" {
s.ConfirmPassword = s.Password
}
res := c.HTTPGet(ctx, "account/addsubaccount", s)
if res.Success {
s.APIKey = res.Data.(string)
}
return res
}
// DeleteSubAccount deletes specified Subaccount
// one of subAccountEmail or publicAccountID must be provided
// https://api.elasticemail.com/public/help#Account_DeleteSubAccount
func (c *Client) DeleteSubAccount(params map[string]string) *Response {
return c.DeleteSubAccountContext(context.Background(), params)
}
// DeleteSubAccountContext is the same as DeleteSubAccount, and it allows the caller to pass in a context
func (c *Client) DeleteSubAccountContext(ctx context.Context, params map[string]string) *Response {
_, emailPrs := params["subAccountEmail"]
_, IDPrs := params["publicAccountID"]
if !emailPrs && !IDPrs {
return &Response{Error: errors.New("DeleteSubAccount called without email or ID")}
}
return c.HTTPGet(ctx, "account/deletesubaccount", params)
}
// UpdateSubAccountSettings updates fields from Subaccount object of specified subaccount referenced by ID from params map
// https://api.elasticemail.com/public/help#Account_UpdateSubAccountSettings
func (c *Client) UpdateSubAccountSettings(s *Subaccount, params map[string]string) *Response {
return c.UpdateSubAccountSettingsContext(context.Background(), s, params)
}
// UpdateSubAccountSettingsContext is the same as UpdateSubAccountSettings, and it allows the caller to pass in a context
func (c *Client) UpdateSubAccountSettingsContext(ctx context.Context, s *Subaccount, params map[string]string) *Response {
_, emailPrs := params["subAccountEmail"]
_, IDPrs := params["publicAccountID"]
if !emailPrs && !IDPrs {
return &Response{Error: errors.New("UpdateSubAccountSettings called without email or ID")}
}
jsonBytes, _ := json.Marshal(s)
var rawParams map[string]string
json.Unmarshal(jsonBytes, &rawParams)
for k := range params {
rawParams[k] = params[k]
}
return c.HTTPGet(ctx, "account/updatesubaccountsettings", rawParams)
}
// GetSubAccountAPIKey attempts to retrieve subaccount API Key
// one of subAccountEmail or publicAccountID must be provided
// https://api.elasticemail.com/public/help#Account_GetSubAccountApiKey
func (c *Client) GetSubAccountAPIKey(params map[string]string) *Response {
return c.GetSubAccountAPIKeyContext(context.Background(), params)
}
// GetSubAccountAPIKeyContext is the same as GetSubAccountAPIKey, and it allows the caller to pass in a context
func (c *Client) GetSubAccountAPIKeyContext(ctx context.Context, params map[string]string) *Response {
_, emailPrs := params["subAccountEmail"]
_, IDPrs := params["publicAccountID"]
if !emailPrs && !IDPrs {
return &Response{Error: errors.New("GetSubAccountAPIKey called without email or ID")}
}
return c.HTTPGet(ctx, "account/getsubaccountapikey", params)
}