This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
response.go
306 lines (268 loc) · 5.37 KB
/
response.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package wego
import (
"bytes"
"encoding/xml"
"github.com/godcong/wego/util"
"github.com/json-iterator/go"
"golang.org/x/text/transform"
"golang.org/x/xerrors"
"io"
"net/http"
"os"
"strings"
)
/*Responder Responder */
type Responder interface {
io.ReadCloser
BodyReader
Type() BodyType
}
// Response ...
type Response struct {
bytes []byte
reader io.ReadCloser
err error
}
// Type ...
func (r *Response) Type() BodyType {
return BodyTypeNone
}
// Read ...
func (r *Response) Read(p []byte) (n int, err error) {
return r.reader.Read(p)
}
// Close ...
func (r *Response) Close() error {
return r.reader.Close()
}
// xmlResponse ...
type xmlResponse struct {
Response
data util.Map
}
// Type ...
func (r *xmlResponse) Type() BodyType {
return BodyTypeXML
}
// XMLResponseReader ...
func XMLResponseReader(reader io.ReadCloser) Responder {
return &xmlResponse{
Response: Response{
reader: reader,
},
}
}
// XMLResponse ...
func XMLResponse(bytes []byte) Responder {
return &xmlResponse{
Response: Response{
bytes: bytes,
},
}
}
// ToMap ...
func (r *xmlResponse) ToMap() util.Map {
maps, e := r.Result()
if e != nil {
return nil
}
return maps
}
// Unmarshal ...
func (r *xmlResponse) Unmarshal(v interface{}) error {
return xml.Unmarshal(r.bytes, v)
}
// Result ...
func (r *xmlResponse) Result() (util.Map, error) {
if r.data != nil {
return r.data, nil
}
r.data = make(util.Map)
e := r.Unmarshal(&r.data)
return r.data, e
}
// Error ...
func (r *xmlResponse) Error() error {
var e ErrRes
if r.err != nil {
return r.err
}
_ = jsoniter.Unmarshal(r.bytes, &e)
if e.ErrCode != 0 {
return xerrors.Errorf("code:%d,msg:%s", e.ErrCode, e.ErrMsg)
}
return nil
}
// jsonResponse ...
type jsonResponse struct {
Response
data util.Map
}
// Type ...
func (r *jsonResponse) Type() BodyType {
return BodyTypeJSON
}
// JSONResponseReader ...
func JSONResponseReader(reader io.ReadCloser) Responder {
return &jsonResponse{
Response: Response{
reader: reader,
},
}
}
// JSONResponse ...
func JSONResponse(bytes []byte) Responder {
return &jsonResponse{
Response: Response{
bytes: bytes,
},
}
}
// ToMap ...
func (r *jsonResponse) ToMap() util.Map {
maps, e := r.Result()
if e != nil {
return nil
}
return maps
}
// Unmarshal ...
func (r *jsonResponse) Unmarshal(v interface{}) error {
return jsoniter.Unmarshal(r.bytes, v)
}
// Result ...
func (r *jsonResponse) Result() (util.Map, error) {
r.data = make(util.Map)
e := r.Unmarshal(&r.data)
return r.data, e
}
// Error ...
func (r *jsonResponse) Error() error {
var e ErrRes
if r.err != nil {
return r.err
}
_ = jsoniter.Unmarshal(r.bytes, &e)
if e.ErrCode != 0 {
return xerrors.Errorf("code:%d,msg:%s", e.ErrCode, e.ErrMsg)
}
return nil
}
// ToMap ...
func (r *Response) ToMap() util.Map {
return nil
}
// Unmarshal ...
func (r *Response) Unmarshal(v interface{}) error {
return r.err
}
// Result ...
func (r *Response) Result() (util.Map, error) {
return nil, r.err
}
// ErrResponder ...
func ErrResponder(err error) Responder {
return &Response{
bytes: nil,
err: err,
}
}
// Bytes ...
func (r *Response) Bytes() []byte {
return r.bytes
}
// ErrRes ...
type ErrRes struct {
ErrCode int64
ErrMsg string
}
// Error ...
func (r *Response) Error() error {
return r.err
}
// ResponseWriter ...
func ResponseWriter(w http.ResponseWriter, responder Responder) error {
w.WriteHeader(http.StatusOK)
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
switch responder.Type() {
case BodyTypeXML:
header["Content-Type"] = []string{"application/xml; charset=utf-8"}
case BodyTypeJSON:
header["Content-Type"] = []string{"application/json; charset=utf-8"}
}
}
b := responder.Bytes()
if b == nil {
return nil
}
_, err := w.Write(b)
if err != nil {
log.Error(err)
return err
}
return nil
}
// BuildResponder ...
func BuildResponder(resp *http.Response) Responder {
ct := resp.Header.Get("Content-Type")
body, err := readBody(resp.Body)
if err != nil {
log.Error(body, err)
return ErrResponder(err)
}
log.Info("response:", string(body[:128]), len(body)) //max 128 char
if resp.StatusCode == 200 {
if strings.Index(ct, "xml") != -1 ||
bytes.Index(body, []byte("<xml")) != -1 {
return XMLResponse(body)
}
return JSONResponse(body)
}
log.Error("error with " + resp.Status)
return ErrResponder(xerrors.New("error with code " + resp.Status))
}
// SaveTo ...
func SaveTo(response Responder, path string) error {
var err error
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_SYNC, os.ModePerm)
if err != nil {
log.Debug("Responder|ToFile", err)
return err
}
defer func() {
err = file.Close()
}()
if _, err = io.Copy(file, response); err != nil {
return err
}
//_, err = file.Write(response.Bytes())
//if err != nil {
// return err
//}
return nil
}
// SaveEncodingTo ...
func SaveEncodingTo(response Responder, path string, t transform.Transformer) (err error) {
var file *os.File
file, err = os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_SYNC, os.ModePerm)
if err != nil {
log.Debug("Responder|ToFile", err)
return err
}
defer func() {
err = file.Close()
}()
writer := transform.NewWriter(file, t)
if _, err = io.Copy(writer, response); err != nil {
return err
}
//_, err = writer.Write(response.Bytes())
//if err != nil {
// return err
//}
defer func() {
err = writer.Close()
}()
return nil
}