-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
71 lines (58 loc) · 1.45 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
package uhttp
import (
"context"
"crypto/tls"
"net/http"
"go.uber.org/zap"
)
type tlsClientConfigOption struct {
config *tls.Config
}
func (o tlsClientConfigOption) Apply(c *Transport) {
c.tlsClientConfig = o.config
}
// WithTLSClientConfig returns an Option that sets the TLS client configuration.
// `tlsConfig` is a structure that is used to configure a TLS client or server.
func WithTLSClientConfig(tlsConfig *tls.Config) Option {
return tlsClientConfigOption{config: tlsConfig}
}
type loggerOption struct {
log bool
logger *zap.Logger
}
func (o loggerOption) Apply(c *Transport) {
c.log = o.log
c.logger = o.logger
}
// WithLogger sets a logger options to the transport layer.
func WithLogger(log bool, logger *zap.Logger) Option {
return loggerOption{
log: log,
logger: logger,
}
}
type userAgentOption struct {
userAgent string
}
func (o userAgentOption) Apply(c *Transport) {
c.userAgent = o.userAgent
}
// WithUserAgent sets a user agent option to the transport layer.
func WithUserAgent(userAgent string) Option {
return userAgentOption{
userAgent: userAgent,
}
}
type Option interface {
Apply(*Transport)
}
// NewClient creates a new HTTP client that uses the given context and options to create a new transport layer.
func NewClient(ctx context.Context, options ...Option) (*http.Client, error) {
t, err := NewTransport(ctx, options...)
if err != nil {
return nil, err
}
return &http.Client{
Transport: t,
}, nil
}