-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
148 lines (123 loc) · 3.08 KB
/
receiver.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
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
package outgoing
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/go-leo/gox/convx"
"github.com/go-leo/gox/encodingx/jsonx"
"github.com/go-leo/gox/encodingx/xmlx"
"github.com/go-leo/gox/errorx"
"google.golang.org/protobuf/proto"
"io"
"net/http"
)
type UnmarshalError struct {
Body []byte
Err error
}
func (e UnmarshalError) Error() string {
return fmt.Sprintf("failed to unmarshal body")
}
type ResponseReceiver interface {
Response() *http.Response
Status() string
StatusCode() int
Proto() string
ProtoMajor() int
ProtoMinor() int
ContentLength() int64
TransferEncoding() []string
Headers() http.Header
Trailers() http.Header
Cookies() []*http.Cookie
BytesBody() ([]byte, error)
TextBody() (string, error)
ObjectBody(body any, unmarshal func([]byte, any) error) error
JSONBody(body any) error
XMLBody(body any) error
ProtobufBody(body proto.Message) error
GobBody(body any) error
WriterBody(file io.Writer) error
}
type receiver struct {
resp *http.Response
}
func (r *receiver) Response() *http.Response {
return r.resp
}
func (r *receiver) Status() string {
return r.resp.Status
}
func (r *receiver) StatusCode() int {
return r.resp.StatusCode
}
func (r *receiver) Proto() string {
return r.resp.Proto
}
func (r *receiver) ProtoMajor() int {
return r.resp.ProtoMajor
}
func (r *receiver) ProtoMinor() int {
return r.resp.ProtoMinor
}
func (r *receiver) ContentLength() int64 {
return r.resp.ContentLength
}
func (r *receiver) TransferEncoding() []string {
return r.resp.TransferEncoding
}
func (r *receiver) Headers() http.Header {
return r.resp.Header
}
func (r *receiver) Trailers() http.Header {
return r.resp.Trailer
}
func (r *receiver) Cookies() []*http.Cookie {
return r.resp.Cookies()
}
func (r *receiver) BytesBody() ([]byte, error) {
body, err := io.ReadAll(r.resp.Body)
if err != nil {
return nil, err
}
defer errorx.Silence(r.resp.Body.Close())
return body, nil
}
func (r *receiver) TextBody() (string, error) {
bodyBytes, err := r.BytesBody()
if err != nil {
return "", err
}
return convx.BytesToString(bodyBytes), nil
}
func (r *receiver) ObjectBody(body any, unmarshal func([]byte, any) error) error {
bodyBytes, err := r.BytesBody()
if err != nil {
return err
}
if err := unmarshal(bodyBytes, body); err != nil {
return UnmarshalError{Body: bodyBytes, Err: err}
}
return nil
}
func (r *receiver) JSONBody(body any) error {
return r.ObjectBody(body, jsonx.Unmarshal)
}
func (r *receiver) XMLBody(body any) error {
return r.ObjectBody(body, xmlx.Unmarshal)
}
func (r *receiver) ProtobufBody(body proto.Message) error {
unmarshal := func(data []byte, v any) error { return proto.Unmarshal(data, v.(proto.Message)) }
return r.ObjectBody(body, unmarshal)
}
func (r *receiver) GobBody(body any) error {
unmarshal := func(data []byte, v any) error { return gob.NewDecoder(bytes.NewReader(data)).Decode(v) }
return r.ObjectBody(body, unmarshal)
}
func (r *receiver) WriterBody(file io.Writer) error {
_, err := io.Copy(file, r.resp.Body)
return err
}
func Receiver(resp *http.Response) ResponseReceiver {
return &receiver{resp: resp}
}