-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresty.go
76 lines (68 loc) · 1.97 KB
/
resty.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
package giu
import (
"net/http"
"time"
"github.com/go-resty/resty/v2"
"go.uber.org/zap"
)
type RestyParams struct {
// Timeout is the amount of time to wait for a response.
Timeout time.Duration
// RetryTimes is the number of times to retry.
RetryTimes int
// DebugMode is the flag to enable/disable debug mode. It will print the request/response details.
// It will print in debug level.
DebugMode bool
// StructLog is the flag to enable/disable simple request&response struct log. It's only work when resty is init with zap logger.
// When it's enabled, it will set debug mode to true. Struct log will print in info level.
StructLog bool
}
var _defaultRestyParams = &RestyParams{
Timeout: 5 * time.Second,
RetryTimes: 0,
DebugMode: false,
StructLog: false,
}
func NewResty(options *RestyParams) *resty.Client {
client := resty.New()
if options == nil {
return client
}
if options.Timeout != 0 {
client.SetTimeout(options.Timeout)
}
if options.RetryTimes != 0 {
client.SetRetryCount(options.RetryTimes)
}
if options.DebugMode {
client.SetDebug(true)
}
return client
}
func DefaultResty() *resty.Client {
return NewResty(_defaultRestyParams)
}
func NewRestyWithLogger(options *RestyParams, logger *zap.Logger) *resty.Client {
client := NewResty(options)
client.SetLogger(logger.With(zap.String("module", "resty")).Sugar())
if options.StructLog {
client.SetDebug(true)
client.OnRequestLog(func(rl *resty.RequestLog) error {
logger.Info("[Resty Http Request]", restyLogToZapFields(rl.Header, rl.Body)...)
return nil
})
client.OnResponseLog(func(rl *resty.ResponseLog) error {
logger.Info("[Resty Http Response]", restyLogToZapFields(rl.Header, rl.Body)...)
return nil
})
}
return client
}
func restyLogToZapFields(headers http.Header, body string) []zap.Field {
var fields []zap.Field
for k, v := range headers {
fields = append(fields, zap.Strings("HEADER: "+k, v))
}
fields = append(fields, zap.String("BODY", body))
return fields
}