-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientopts.go
128 lines (116 loc) · 3.19 KB
/
clientopts.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
package client
import (
"crypto/tls"
"io"
"net/http"
"net/url"
"strings"
"time"
// Namespace imports
. "github.com/djthorpe/go-errors"
)
// OptEndpoint sets the endpoint for all requests.
func OptEndpoint(value string) ClientOpt {
return func(client *Client) error {
if url, err := url.Parse(value); err != nil {
return err
} else if url.Scheme == "" || url.Host == "" {
return ErrBadParameter.Withf("endpoint: %q", value)
} else if url.Scheme != "http" && url.Scheme != "https" {
return ErrBadParameter.Withf("endpoint: %q", value)
} else {
client.endpoint = url
}
return nil
}
}
// OptTimeout sets the timeout on any request. By default, a timeout
// of 10 seconds is used if OptTimeout is not set
func OptTimeout(value time.Duration) ClientOpt {
return func(client *Client) error {
client.Client.Timeout = value
return nil
}
}
// OptUserAgent sets the user agent string on each API request
// It is set to the default if empty string is passed
func OptUserAgent(value string) ClientOpt {
return func(client *Client) error {
value = strings.TrimSpace(value)
if value == "" {
client.ua = DefaultUserAgent
} else {
client.ua = value
}
return nil
}
}
// OptTrace allows you to be the "man in the middle" on any
// requests so you can see traffic move back and forth.
// Setting verbose to true also displays the JSON response
func OptTrace(w io.Writer, verbose bool) ClientOpt {
return func(client *Client) error {
client.Client.Transport = newLogTransport(w, client.Client.Transport, verbose)
return nil
}
}
// OptStrict turns on strict content type checking on anything returned
// from the API
func OptStrict() ClientOpt {
return func(client *Client) error {
client.strict = true
return nil
}
}
// OptRateLimit sets the limit on number of requests per second
// and the API will sleep when exceeded. For account tokens this is 1 per second
func OptRateLimit(value float32) ClientOpt {
return func(client *Client) error {
if value < 0.0 {
return ErrBadParameter.With("OptRateLimit")
} else {
client.rate = value
return nil
}
}
}
// OptReqToken sets a request token for all client requests. This can be
// overridden by the client for individual requests using OptToken.
func OptReqToken(value Token) ClientOpt {
return func(client *Client) error {
client.token = value
return nil
}
}
// OptSkipVerify skips TLS certificate domain verification
func OptSkipVerify() ClientOpt {
return func(client *Client) error {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
return nil
}
}
// OptHeader appends a custom header to each request
func OptHeader(key, value string) ClientOpt {
return func(client *Client) error {
if client.headers == nil {
client.headers = make(map[string]string, 2)
}
if key == "" {
return ErrBadParameter.With("OptHeader")
}
client.headers[key] = value
return nil
}
}
// OptParent sets the parent client for this client, which is
// used for setting additional client options in the parent
func OptParent(v any) ClientOpt {
return func(client *Client) error {
if v == nil {
return ErrBadParameter.With("OptParent")
} else {
client.Parent = v
}
return nil
}
}