-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
57 lines (47 loc) · 1.29 KB
/
client.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
package httpclient
import (
"net/http"
"time"
)
// Client is an http.Client wrapper
type Client struct {
client *http.Client
baseURL string
headers []header
}
// header is a struct that contains a key and a value
type header struct{ key, value string }
// New creates a new Client reference given a client timeout
func New() *Client {
return &Client{
client: &http.Client{},
headers: []header{},
}
}
// WithClient sets the http client on the Client
func (c *Client) WithClient(client *http.Client) *Client {
c.client = client
return c
}
// WithTimeout sets the timeout on the http Client
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.Timeout = timeout
return c
}
// WithTransport sets teh transport on the http Client
func (c *Client) WithTransport(transport http.RoundTripper) *Client {
c.client.Transport = transport
return c
}
// WithBaseURL sets the baseURL on the Client
func (c *Client) WithBaseURL(url string) *Client {
c.baseURL = url
return c
}
// WithHeader sets the headers on the Client
func (c *Client) WithHeader(key, value string) *Client {
c.headers = append(c.headers, header{key: key, value: value})
return c
}
// Client is a getter that returns a reference to the underlying http Client
func (c *Client) Client() *http.Client { return c.client }