-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresponse.go
57 lines (45 loc) · 1008 Bytes
/
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
package response
import (
"io"
"github.com/henomis/restclientgo"
)
type Response struct {
Code int `json:"-"`
Time float64 `json:"time"`
RawStatus any `json:"status"`
Status string `json:"-"`
RawBody *string `json:"-"`
}
type Detail struct {
TypeURL *string `json:"typeUrl,omitempty"`
Value *string `json:"value,omitempty"`
}
func (r *Response) IsSuccess() bool {
return (r.Code >= 200 && r.Code < 300)
}
func (r *Response) SetStatusCode(code int) error {
r.Code = code
return nil
}
func (r *Response) SetBody(body io.Reader) error {
b, err := io.ReadAll(body)
if err != nil {
return err
}
s := string(b)
r.RawBody = &s
return nil
}
func (r *Response) SetHeaders(headers restclientgo.Headers) error {
return nil
}
func (r *Response) SetStatusMessage() {
switch status := r.RawStatus.(type) {
case string:
r.Status = status
case map[string]interface{}:
if errorMessage, ok := status["error"].(string); ok {
r.Status = errorMessage
}
}
}