-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresponse.go
68 lines (57 loc) · 1.39 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
package httpclient
import (
"net/http"
"time"
resty "github.com/go-resty/resty/v2"
)
type Response struct {
statusCode int
body []byte
header http.Header
cookies []*http.Cookie
request *Request
responseTime time.Duration
}
// StatusCode returns the response status code.
func (r Response) StatusCode() int {
return r.statusCode
}
// Body returns the response body.
func (r Response) Body() []byte {
return r.body
}
// Header returns the response header.
func (r Response) Header() http.Header {
return r.header
}
// Cookies returns the response cookies.
func (r Response) Cookies() []*http.Cookie {
return r.cookies
}
// Cookie finds a cookie by a name and returns it.
func (r Response) Cookie(name string) *http.Cookie {
for _, c := range r.cookies {
if c.Name == name {
return c
}
}
return nil
}
// Request returns the received request.
func (r Response) Request() *Request {
return r.request
}
// ResponseTime returns the request response time.
func (r Response) ResponseTime() time.Duration {
return r.responseTime
}
func wrapResponse(request *Request, restyResponse *resty.Response) *Response {
return &Response{
statusCode: restyResponse.StatusCode(),
header: restyResponse.Header(),
body: restyResponse.Body(),
cookies: restyResponse.Cookies(),
request: request,
responseTime: time.Since(request.startTime),
}
}