-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
196 lines (167 loc) · 4.92 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
package vrest
import (
"fmt"
"io"
"net/http"
"reflect"
)
type Response struct {
Raw *http.Response
Error error
Body interface{}
ErrorBody interface{}
ForceJSON bool
ForceXML bool
BodyBytes []byte
BodyLimit int64
TraceBody bool
DoUnmarshal bool
ContentLengthPtr *int64
SuccessStatusCodes []int
}
func (req *Request) processHTTPResponse(rawResp *http.Response, err error) error {
req.Response.Raw = rawResp
if err != nil {
return fmt.Errorf("http request %s %s failed: %w", req.Raw.Method, req.Raw.URL, err)
}
if rawResp == nil {
return fmt.Errorf("http request %s %s returned no response and no error", req.Raw.Method, req.Raw.URL)
}
if req.Response.ContentLengthPtr != nil {
*req.Response.ContentLengthPtr = req.Response.Raw.ContentLength
}
err = req.readResponseBody()
if err != nil {
return fmt.Errorf("http request %s %s failed to read response body: %w", req.Raw.Method, req.Raw.URL, err)
}
success := req.Overridable.IsSuccess(req)
if req.Response.HasEmptyBody() {
if !success {
return fmt.Errorf("http request %s %s failed with status code %d", req.Raw.Method, req.Raw.URL, req.Response.StatusCode())
}
return nil
}
responseValue := req.Response.Body
if !success && req.Response.DoUnmarshal {
if req.Response.ErrorBody == nil && req.Client.ErrorType != nil {
req.Response.ErrorBody = reflect.New(req.Client.ErrorType).Interface()
}
responseValue = req.Response.ErrorBody
}
didUnmarshal, err := req.unmarshalResponseBody(responseValue)
if success && err != nil {
// treat unmarshaling error as a failure only if the response is successful
return fmt.Errorf("http request %s %s failed to unmarshal response body: %w", req.Raw.Method, req.Raw.URL, err)
}
if !success {
if didUnmarshal {
switch e := responseValue.(type) {
case error:
return fmt.Errorf("http request %s %s failed: %w", req.Raw.Method, req.Raw.URL, e)
default:
return fmt.Errorf("http request %s %s failed: %s", req.Raw.Method, req.Raw.URL, responseValue)
}
} else {
msg := string(req.Response.BodyBytes)
return fmt.Errorf("http request %s %s failed: %s", req.Raw.Method, req.Raw.URL, msg)
}
}
return nil
}
func (req *Request) readResponseBody() error {
if req.Response.Raw.Body == nil {
return nil
}
if req.Overridable.IsSuccess(req) {
// check if we should just return the response ReadCloser
if responseReadCloser, ok := req.Response.Body.(*io.ReadCloser); ok && responseReadCloser != nil {
*responseReadCloser = req.Response.Raw.Body
req.Response.DoUnmarshal = false
return nil
}
}
var r io.Reader = req.Response.Raw.Body
if req.Response.BodyLimit > 0 {
r = io.LimitReader(r, req.Response.BodyLimit)
}
var err error
req.Response.BodyBytes, err = io.ReadAll(r)
if len(req.Response.BodyBytes) > 0 && req.Response.WantsRawByteArray() {
if responseBytesPointer, ok := req.Response.Body.(*[]byte); ok && responseBytesPointer != nil {
*responseBytesPointer = req.Response.BodyBytes
req.Response.DoUnmarshal = false
}
}
return err
}
// unmarshalResponseBody unmarshals the response body into the given value.
// It returns true if the response body was unmarshaled, false otherwise.
// If there is a body and it was not unmarshaled, an error is returned.
func (req *Request) unmarshalResponseBody(value interface{}) (bool, error) {
if !req.Response.DoUnmarshal {
return false, nil
}
if value == nil {
// seems like the caller is not interested in the result value
// just exit without error
return false, nil
}
var err error
if req.Response.ForceJSON || IsJSONContentType(req.Response.ContentType()) {
err = req.Overridable.JSONUnmarshal(req, req.Response.BodyBytes, value)
if err != nil {
return false, err
}
return true, nil
}
if req.Response.ForceXML || IsXMLContentType(req.Response.ContentType()) {
err = req.Overridable.XMLUnmarshal(req, req.Response.BodyBytes, value)
if err != nil {
return false, err
}
return true, nil
}
if len(req.Response.BodyBytes) > 0 {
return false, ErrResponseNotUnmarshaled
}
return false, nil
}
func (resp *Response) WantsRawByteArray() bool {
if resp == nil || resp.Body == nil {
return false
}
if responseBytesPointer, ok := resp.Body.(*[]byte); ok && responseBytesPointer != nil {
return true
}
return false
}
func (resp *Response) WantsReadCloser() bool {
if resp == nil || resp.Body == nil {
return false
}
if responseReadCloserPointer, ok := resp.Body.(*io.ReadCloser); ok && responseReadCloserPointer != nil {
return true
}
return false
}
func (resp *Response) HasEmptyBody() bool {
if resp.Raw == nil {
return false
}
return resp.Raw.Body == nil
}
func (resp *Response) StatusCode() int {
if resp.Raw == nil {
return 0
}
return resp.Raw.StatusCode
}
func (resp *Response) ContentType() string {
return resp.Header().Get("Content-Type")
}
func (resp *Response) Header() http.Header {
if resp.Raw == nil {
return http.Header{}
}
return resp.Raw.Header
}