-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathoption.go
61 lines (50 loc) · 1.09 KB
/
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
package gohttpclient
import (
"net/http"
"time"
)
type (
Option func(c *Client)
ClientOption Option
)
func WithCustomHttpClient(client *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = client
}
}
func WithDefaultHeaders() ClientOption {
return func(c *Client) {
if c.headers == nil {
c.headers = make(map[string]Header)
}
c.headers["Content-Type"] = Header{Value: "application/json", IsDefault: true}
c.headers["Accept"] = Header{Value: "application/json", IsDefault: true}
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(c *Client) {
c.timeout = timeout
c.httpClient.Timeout = timeout
}
}
func WithHeader(key, value string) Option {
return func(c *Client) {
if c.headers == nil {
c.headers = make(map[string]Header)
}
c.headers[key] = Header{Value: value, IsDefault: false}
}
}
func WithQuery(key, value string) Option {
return func(c *Client) {
if c.query == nil {
c.query = make(map[string]string)
}
c.query[key] = value
}
}
func WithBody(body []byte) Option {
return func(c *Client) {
c.body = body
}
}