-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
133 lines (112 loc) · 3.12 KB
/
response.go
File metadata and controls
133 lines (112 loc) · 3.12 KB
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
package cumi
import (
"encoding/json"
"encoding/xml"
"net/http"
"strings"
"time"
)
// Response represents an HTTP response
type Response struct {
Request *Request
Response *http.Response
body []byte
size int64
receivedAt time.Time
duration time.Duration
state ResultState
Err error
// Embedded from http.Response for direct access
Status string
StatusCode int
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
}
// Body returns the response body as bytes
func (r *Response) Body() []byte {
return r.body
}
// String returns the response body as a string
func (r *Response) String() string {
return string(r.body)
}
// JSON unmarshals the response body into the provided interface using JSON
func (r *Response) JSON(v interface{}) error {
if len(r.body) == 0 {
return nil
}
return json.Unmarshal(r.body, v)
}
// XML unmarshals the response body into the provided interface using XML
func (r *Response) XML(v interface{}) error {
if len(r.body) == 0 {
return nil
}
return xml.Unmarshal(r.body, v)
}
// IsSuccess returns true if the response is successful (2xx status code)
func (r *Response) IsSuccess() bool {
return r.state == SuccessState
}
// IsError returns true if the response is an error (4xx or 5xx status code)
func (r *Response) IsError() bool {
return r.state == ErrorState
}
// Time returns the time when the response was received
func (r *Response) Time() time.Time {
return r.receivedAt
}
// Duration returns the time taken for the request
func (r *Response) Duration() time.Duration {
return r.duration
}
// Size returns the size of the response body in bytes
func (r *Response) Size() int64 {
return r.size
}
// ResultState returns the state of the response
func (r *Response) ResultState() ResultState {
return r.state
}
// Error returns the error if any occurred during the request
func (r *Response) Error() error {
return r.Err
}
// ContentType returns the Content-Type header value
func (r *Response) ContentType() string {
return r.Header.Get("Content-Type")
}
// IsJSON returns true if the response content type is JSON
func (r *Response) IsJSON() bool {
contentType := r.ContentType()
return strings.Contains(contentType, "application/json")
}
// IsXML returns true if the response content type is XML
func (r *Response) IsXML() bool {
contentType := r.ContentType()
return strings.Contains(contentType, "application/xml") ||
strings.Contains(contentType, "text/xml")
}
// IsHTML returns true if the response content type is HTML
func (r *Response) IsHTML() bool {
contentType := r.ContentType()
return strings.Contains(contentType, "text/html")
}
// IsText returns true if the response content type is plain text
func (r *Response) IsText() bool {
contentType := r.ContentType()
return strings.Contains(contentType, "text/plain")
}
// Cookies returns the cookies set by the server
func (r *Response) Cookies() []*http.Cookie {
if r.Response == nil {
return nil
}
return r.Response.Cookies()
}
// Location returns the Location header value (useful for redirects)
func (r *Response) Location() string {
return r.Header.Get("Location")
}