forked from ppmathis/cloudns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
95 lines (79 loc) · 2.32 KB
/
options.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
package cloudns
import (
"net/http"
"strings"
)
// Option represents functional options which can be specified when instantiating a new API client
type Option func(api *Client) error
// BaseURL modifies the base URL of the API client
func BaseURL(baseURL string) Option {
return func(api *Client) error {
api.baseURL = strings.TrimRight(baseURL, "/")
return nil
}
}
// Headers adds a set of headers to every sent API request. These headers can be overridden by request-specific headers.
func Headers(headers http.Header) Option {
return func(api *Client) error {
api.headers = headers
return nil
}
}
// Params adds a set of parameters (either GET or POST) to every sent API request. These are overridden by auth as well
// as request-specific parameters.
func Params(params HTTPParams) Option {
return func(api *Client) error {
api.params = params
return nil
}
}
// HTTPClient overrides the HTTPClient used by the API client, useful for mocking in unit tests.
func HTTPClient(httpClient *http.Client) Option {
return func(api *Client) error {
api.httpClient = httpClient
return nil
}
}
// UserAgent overrides the default user agent of cloudns-go.
func UserAgent(userAgent string) Option {
return func(api *Client) error {
api.userAgent = userAgent
return nil
}
}
// AuthUserID setups user-id based authentication against the ClouDNS API
func AuthUserID(id int, password string) Option {
return func(api *Client) error {
if api.auth.Type != AuthTypeNone {
return ErrMultipleCredentials
}
api.auth.Type = AuthTypeUserID
api.auth.UserID = id
api.auth.Password = password
return nil
}
}
// AuthSubUserID setups sub-user-id based authentication against the ClouDNS API
func AuthSubUserID(id int, password string) Option {
return func(api *Client) error {
if api.auth.Type != AuthTypeNone {
return ErrMultipleCredentials
}
api.auth.Type = AuthTypeSubUserID
api.auth.SubUserID = id
api.auth.Password = password
return nil
}
}
// AuthSubUserName setups the sub-user-name based authentication against the ClouDNS API
func AuthSubUserName(user string, password string) Option {
return func(api *Client) error {
if api.auth.Type != AuthTypeNone {
return ErrMultipleCredentials
}
api.auth.Type = AuthTypeSubUserName
api.auth.SubUserName = user
api.auth.Password = password
return nil
}
}