-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
47 lines (38 loc) · 897 Bytes
/
options.go
File metadata and controls
47 lines (38 loc) · 897 Bytes
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
package patch
import (
"fmt"
"net/http"
"time"
)
// DefaultTimeout is the default time limit for requests made by the client.
const DefaultTimeout = 30 * time.Second
// DefaultStatusValidator returns true for 2xx statuses, otherwise false.
var DefaultStatusValidator = func(status int) bool {
return status >= 200 && status < 300
}
type Option func(c *Client)
func WithBaseURL(url string) Option {
return func(c *Client) {
c.BaseURL = url
}
}
func WithTimeout(d time.Duration) Option {
return func(c *Client) {
switch bc := c.BaseClient.(type) {
case *http.Client:
bc.Timeout = d
return
}
panic(fmt.Errorf("cannot set timeout on base client of type %T", c))
}
}
func WithStatusValidator(f func(int) bool) Option {
return func(c *Client) {
c.StatusValidator = f
}
}
func WithEncoder(enc Encoder) Option {
return func(c *Client) {
c.DefaultEncoder = enc
}
}