-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
108 lines (98 loc) · 3.05 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
package client
import (
"context"
"net"
"net/http"
"time"
"github.com/americanas-go/log"
)
// NewClientWithConfigPath returns a http client with options from config path.
func NewClientWithConfigPath(ctx context.Context, path string) (*http.Client, error) {
options, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewClientWithOptions(ctx, options), nil
}
// NewClientWithOptions returns a new http client with options.
func NewClientWithOptions(ctx context.Context, options *Options) *http.Client {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: options.DialTimeout,
Deadline: time.Time{},
// LocalAddr: nil,
// FallbackDelay: 0,
KeepAlive: options.KeepAlive,
// Resolver: nil,
// Control: nil,
}).DialContext,
/*
TLSClientConfig: &tls.Config{
Rand: nil,
Time: nil,
Certificates: []tls.Certificate{
{
Certificate: nil,
PrivateKey: nil,
SupportedSignatureAlgorithms: nil,
OCSPStaple: nil,
SignedCertificateTimestamps: nil,
Leaf: nil,
},
},
NameToCertificate: nil,
GetCertificate: nil,
GetClientCertificate: nil,
GetConfigForClient: nil,
VerifyPeerCertificate: nil,
VerifyConnection: nil,
RootCAs: nil,
NextProtos: nil,
ServerName: "",
ClientAuth: 0,
ClientCAs: nil,
InsecureSkipVerify: false,
CipherSuites: nil,
PreferServerCipherSuites: false,
SessionTicketsDisabled: false,
SessionTicketKey: [32]byte{},
ClientSessionCache: nil,
MinVersion: 0,
MaxVersion: 0,
CurvePreferences: nil,
DynamicRecordSizingDisabled: false,
Renegotiation: 0,
KeyLogWriter: nil,
},
*/
TLSHandshakeTimeout: options.TLSHandshakeTimeout,
DisableKeepAlives: options.DisableKeepAlives,
DisableCompression: options.DisableCompression,
MaxIdleConns: options.MaxIdleConn,
MaxIdleConnsPerHost: options.MaxIdleConnPerHost,
MaxConnsPerHost: options.MaxConnsPerHost,
IdleConnTimeout: options.IdleConnTimeout,
// ResponseHeaderTimeout: 0,
ExpectContinueTimeout: options.ExpectContinueTimeout,
// MaxResponseHeaderBytes: 0,
// WriteBufferSize: 0,
// ReadBufferSize: 0,
ForceAttemptHTTP2: options.ForceHTTP2,
}
return &http.Client{
Transport: tr,
// CheckRedirect: nil,
// Jar: nil,
Timeout: options.Timeout,
}
}
// NewClient returns a new http client with default options.
func NewClient(ctx context.Context) *http.Client {
logger := log.FromContext(ctx)
o, err := NewOptions()
if err != nil {
logger.Fatalf(err.Error())
}
return NewClientWithOptions(ctx, o)
}