-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_with.go
57 lines (45 loc) · 1.1 KB
/
call_with.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
package withttp
import (
"bufio"
"fmt"
"io"
)
func (c *Call[T]) WithLogger(l logger) *Call[T] {
c.logger = l
return c
}
func (c *Call[T]) Log(w io.Writer) {
buf := bufio.NewWriter(w)
_, _ = buf.WriteString(c.Req.Method())
_, _ = buf.WriteString(" ")
_, _ = buf.WriteString(c.Req.URL().String())
_, _ = buf.WriteString("\n")
c.Req.RangeHeaders(func(key string, value string) {
_, _ = buf.WriteString(key)
_ = buf.WriteByte(':')
_ = buf.WriteByte(' ')
_, _ = buf.WriteString(value)
_ = buf.WriteByte('\n')
})
if !c.ReqIsStream && len(c.ReqBodyRaw) > 0 {
_ = buf.WriteByte('\n')
_, _ = buf.Write(c.ReqBodyRaw)
_ = buf.WriteByte('\n')
}
_ = buf.WriteByte('\n')
// TODO: print text repr of status code
_, _ = buf.WriteString(fmt.Sprintf("%d %s", c.Res.Status(), ""))
_ = buf.WriteByte('\n')
c.Res.RangeHeaders(func(key string, value string) {
_, _ = buf.WriteString(key)
_ = buf.WriteByte(':')
_ = buf.WriteByte(' ')
_, _ = buf.WriteString(value)
_ = buf.WriteByte('\n')
})
if len(c.BodyRaw) > 0 {
_, _ = buf.Write(c.BodyRaw)
}
_ = buf.WriteByte('\n')
_ = buf.Flush()
}