-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
59 lines (46 loc) · 1.61 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
package simpleresty
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"net/url"
)
// Response represents the response after executing a HTTP request.
type Response struct {
// Request representss the response's original request.
Request *resty.Request
// RequestURL is the request URL.
RequestURL string
// RequestMethod is the request method such as GET.
RequestMethod string
// Request body is the request body in JSON string format.
RequestBody string
// Resp represents the entire HTTP response.
Resp *resty.Response
// Status is the response status in string format such as '200 OK'.
Status string
// StatusCode is response status in integer format such as 200.
StatusCode int
// Body is the response body in JSON String format.
Body string
}
// checkResponse parses the HTTP response and returns the response and an error if applicable.
func checkResponse(resp *resty.Response) (*Response, error) {
path, _ := url.QueryUnescape(resp.Request.URL)
r := &Response{Status: resp.Status(), StatusCode: resp.StatusCode(),
Body: string(resp.Body()), Resp: resp, RequestURL: path,
RequestMethod: resp.Request.Method, Request: resp.Request}
// Convert the request body to a string.
reqBody, marshallErr := json.Marshal(resp.Request.Body)
if marshallErr != nil {
return nil, marshallErr
}
r.RequestBody = string(reqBody)
// If response is any of the below, return early.
switch r.StatusCode {
case 200, 201, 202, 204, 304:
return r, nil
}
// Otherwise, return the response along with the error.
return r, fmt.Errorf("%s %s: %d %s", r.RequestMethod, r.RequestURL, r.StatusCode, r.Body)
}