-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
148 lines (117 loc) · 4.32 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package client
import (
"context"
"fmt"
"io"
"net/http"
)
// NewClient returns an opionanted HTTP client which can be
// optionally augmented with TransportWrappers which add
// features such as retries with exponential backoff.
func NewClient(opts ...ClientOption) *Client {
var cfg ClientConfig
cfg.Option(opts...)
cfg.Default()
var client http.Client
cfg.Wrap(&client)
return &Client{
cfg: cfg,
client: &client,
}
}
type Client struct {
cfg ClientConfig
client *http.Client
}
// Get performs a HTTP GET request against the provided URL.
func (c *Client) Get(ctx context.Context, url string) (*http.Response, error) {
return c.requestWithoutBody(ctx, http.MethodGet, url)
}
// Head performs a HTTP HEAD request against the provided URL.
func (c *Client) Head(ctx context.Context, url string) (*http.Response, error) {
return c.requestWithoutBody(ctx, http.MethodHead, url)
}
// Post performs a HTTP POST request against the provided URL with the given body.
func (c *Client) Post(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.requestWithBody(ctx, http.MethodPost, url, nil)
}
// Put performs a HTTP PUT request against the provided URL with the given body.
func (c *Client) Put(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.requestWithBody(ctx, http.MethodPut, url, nil)
}
// Patch performs a HTTP PATCH request against the provided URL with the given body.
func (c *Client) Patch(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.requestWithBody(ctx, http.MethodPatch, url, nil)
}
// Delete performs a HTTP DELETE request against the provided URL.
func (c *Client) Delete(ctx context.Context, url string) (*http.Response, error) {
return c.requestWithoutBody(ctx, http.MethodDelete, url)
}
// Connect performs a HTTP CONNECT request against the provided URL with the given body.
func (c *Client) Connect(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.requestWithBody(ctx, http.MethodConnect, url, nil)
}
// Options performs a HTTP OPTIONS request against the provided URL.
func (c *Client) Options(ctx context.Context, url string) (*http.Response, error) {
return c.requestWithoutBody(ctx, http.MethodOptions, url)
}
// Trace performs a HTTP TRACE request against the provided URL.
func (c *Client) Trace(ctx context.Context, url string) (*http.Response, error) {
return c.requestWithoutBody(ctx, http.MethodTrace, url)
}
func (c *Client) requestWithoutBody(ctx context.Context, method, url string) (*http.Response, error) {
return c.requestWithBody(ctx, method, url, nil)
}
func (c *Client) requestWithBody(ctx context.Context, method, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, fmt.Errorf("constructing request: %w", err)
}
return c.client.Do(req)
}
type ClientConfig struct {
Transport http.RoundTripper
Wrappers []TransportWrapper
}
func (c *ClientConfig) Option(opts ...ClientOption) {
for _, opt := range opts {
opt.ConfigureClient(c)
}
}
func (c *ClientConfig) Default() {
if c.Transport == nil {
c.Transport = http.DefaultTransport
}
}
func (c *ClientConfig) Wrap(client *http.Client) {
tp := c.Transport
for _, w := range c.Wrappers {
w.Wrap(tp)
}
client.Transport = tp
}
type ClientOption interface {
ConfigureClient(*ClientConfig)
}
// WithTransport configures a Client instance with the given
// http.RoundTripper instance.
type WithTransport struct{ http.RoundTripper }
func (t WithTransport) ConfigureClient(c *ClientConfig) {
c.Transport = t.RoundTripper
}
// WithWrapper configures a Client instance with the given
// TransportWrapper. This option can be provided multiple
// times to apply several TransportWrappers. The order in
// which the TransportWrappers is applied is important!
type WithWrapper struct{ TransportWrapper }
func (ww WithWrapper) ConfigureClient(c *ClientConfig) {
c.Wrappers = append(c.Wrappers, ww.TransportWrapper)
}
// TransportWrapper adds functionality to a http.RoundTripper
// by adding pre and post call execution steps.
type TransportWrapper interface {
// Wrap return a http.RoundTripper which has been
// wrapped with the pre and post functionality the
// TransportWrapper provides.
Wrap(http.RoundTripper) http.RoundTripper
}