-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_option.go
63 lines (52 loc) · 1.79 KB
/
client_option.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
package cassh
import (
"fmt"
"net/http"
"runtime"
"time"
"github.com/krostar/httpclient"
)
// ClientOption defines the signature of all options usable on NewClient.
type ClientOption func(o *clientOptions)
type clientOptions struct {
serverTimezone *time.Location
httpDoer httpclient.Doer
httpDefaultHeaders http.Header
tolerateInsecureProtocol bool
}
func clientOptionsDefaults() *clientOptions {
clientVersion := fmt.Sprintf("krostar/cassh-v1_" + runtime.Version())
defaultHeaders := make(http.Header)
defaultHeaders.Set("User-Agent", "CASSH-CLIENT "+clientVersion)
defaultHeaders.Set("CLIENT_VERSION", clientVersion)
return &clientOptions{
httpDoer: http.DefaultClient,
httpDefaultHeaders: defaultHeaders,
serverTimezone: time.UTC,
tolerateInsecureProtocol: false,
}
}
// ClientOptionServerTimezone sets the timezone of the CASSH server for time response to be received correctly.
func ClientOptionServerTimezone(serverTimezone *time.Location) ClientOption {
return func(o *clientOptions) {
o.serverTimezone = serverTimezone
}
}
// ClientOptionHTTPClient sets the http client used on each request made to the CASSH server.
func ClientOptionHTTPClient(httpDoer httpclient.Doer) ClientOption {
return func(o *clientOptions) {
o.httpDoer = httpDoer
}
}
// ClientOptionHTTPHeader sets some headers used by default on all http requests.
func ClientOptionHTTPHeader(httpDefaultHeaders http.Header) ClientOption {
return func(o *clientOptions) {
o.httpDefaultHeaders = httpDefaultHeaders
}
}
// ClientOptionTolerateInsecureProtocols allows the CASSH server to be join using http instead of https.
func ClientOptionTolerateInsecureProtocols() ClientOption {
return func(o *clientOptions) {
o.tolerateInsecureProtocol = true
}
}