-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
81 lines (67 loc) · 2.2 KB
/
client.go
File metadata and controls
81 lines (67 loc) · 2.2 KB
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
package emailctl
import (
"fmt"
"github.com/lyubenblagoev/goprsc"
"github.com/spf13/viper"
)
// Client is the Postfix REST server client.
type Client struct {
client *goprsc.Client
Auth *AuthService
Domains *DomainService
Accounts *AccountService
Aliases *AliasService
InputBccs *InputBccService
OutputBccs *OutputBccService
}
// GetLogin returns the user login associated with the client
func (c *Client) GetLogin() string {
return c.client.Login
}
// GetAuthToken returns the authentication token associated with the client
func (c *Client) GetAuthToken() string {
return c.client.AuthToken
}
// GetRefreshToken returns the refresh token associated with the client
func (c *Client) GetRefreshToken() string {
return c.client.RefreshToken
}
type service struct {
client *goprsc.Client
}
// NewClient creates an instance of Client.
func NewClient() (*Client, error) {
goprscClient, err := newGoprscClient()
if err != nil {
return nil, fmt.Errorf("unable to initialize Postfix REST Server API client: %s", err)
}
c := &Client{client: goprscClient}
s := service{client: goprscClient} // Reuse the same structure instead of allocating one for each service
c.Auth = (*AuthService)(&s)
c.Domains = (*DomainService)(&s)
c.Accounts = (*AccountService)(&s)
c.Aliases = (*AliasService)(&s)
// Allocate separate structs for the BCC services as they have different state
c.InputBccs = NewInputBccService(goprscClient)
c.OutputBccs = NewOutputBccService(goprscClient)
return c, nil
}
func newGoprscClient() (*goprsc.Client, error) {
host := viper.GetString("host")
port := viper.GetString("port")
useHTTPS := viper.GetBool("https")
login := viper.GetString("login")
authToken := viper.GetString("authToken")
refreshToken := viper.GetString("refreshToken")
var options []goprsc.ClientOption
options = append(options, goprsc.UserAgentOption("emailctl"))
options = append(options, goprsc.HostOption(host))
options = append(options, goprsc.PortOption(port))
if useHTTPS {
options = append(options, goprsc.HTTPSProtocolOption())
}
if len(authToken) > 0 {
options = append(options, goprsc.AuthOption(login, authToken, refreshToken))
}
return goprsc.NewClientWithOptions(nil, options...)
}