-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
186 lines (157 loc) · 3.15 KB
/
Copy pathclient.go
File metadata and controls
186 lines (157 loc) · 3.15 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package httpc
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const defaultTimeout = 15 * time.Second
type Client struct {
client *http.Client
url *url.URL
name string
decode func(data []byte, v interface{}) error
headers []Value
logFunc func(
ctx context.Context,
name string,
method string,
url string,
dur time.Duration,
statusCode int,
v any,
b []byte,
err error,
)
}
func New(u *url.URL, opts ...Option) *Client {
c := &Client{
url: u,
client: &http.Client{
Timeout: defaultTimeout,
},
name: resolveName(u),
decode: json.Unmarshal,
}
for _, opt := range opts {
opt(c)
}
if c.client.Transport == nil {
c.client.Transport = http.DefaultTransport
}
return c
}
func Must(base string, opts ...Option) *Client {
u, err := url.Parse(base)
if err != nil {
panic(err)
}
return New(u, opts...)
}
type Option func(*Client)
func WithTimeout(t time.Duration) Option {
return func(c *Client) {
c.client.Timeout = t
}
}
func WithTransport(t http.RoundTripper) Option {
return func(c *Client) {
c.client.Transport = t
}
}
func WithLogFunc(logFunc func(
ctx context.Context,
name string,
method string,
url string,
dur time.Duration,
statusCode int,
v any,
b []byte,
err error,
),
) Option {
return func(c *Client) {
c.logFunc = logFunc
}
}
func WithDecode(d func(data []byte, v interface{}) error) Option {
return func(c *Client) {
c.decode = d
}
}
func WithHeaders(h ...Value) Option {
return func(c *Client) {
c.headers = h
}
}
func WithName(n string) Option {
return func(c *Client) {
c.name = n
}
}
func (c *Client) Name() string {
return c.name
}
func (c *Client) Do(ctx context.Context, r Request, v interface{}, customParseFunc func(resp *http.Response, b []byte) error) (err error) {
r = r.WithBase(c.url)
start := time.Now().UTC()
var (
url string
rawResponseBytes []byte
statusCode int
)
defer func() {
if c.logFunc != nil {
c.logFunc(
ctx,
c.Name(),
r.Method(),
url,
time.Since(start),
statusCode,
v,
rawResponseBytes,
err,
)
}
}()
req, err := r.WithHeader(c.headers...).HTTP(ctx)
if err != nil {
return fmt.Errorf("%s: create http request error: %w", c.Name(), err)
}
url = req.URL.String()
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("%s: do request error: %w", c.Name(), err)
}
defer resp.Body.Close()
rawResponseBytes, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%s: read body error: %w", c.Name(), err)
}
statusCode = resp.StatusCode
if customParseFunc != nil {
return customParseFunc(resp, rawResponseBytes)
}
if resp.StatusCode >= 400 {
if r.errorHandler != nil {
return r.errorHandler(resp.StatusCode, rawResponseBytes)
}
return fmt.Errorf("%s: http status code: %v", c.Name(), resp.StatusCode)
}
if len(rawResponseBytes) > 1 && c.decode != nil && v != nil {
return c.decode(rawResponseBytes, v)
}
return nil
}
func resolveName(u *url.URL) string {
if idx := strings.Index(u.Hostname(), "."); idx > 0 {
return strings.ToLower(u.Hostname()[:idx])
}
return strings.ToLower(u.Hostname())
}