-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
80 lines (66 loc) · 1.79 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
package tinyclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// Response is a wrapper of http.Response which provide some extra features
type Response struct {
client *Client
Request *Request
Response *http.Response
bodyBytes []byte
ReceivedAt time.Time
}
// ReadBody reads the http.Response bodyBytes and assigns it to the r.Body
func (response *Response) ReadBody() ([]byte, error) {
// If r.bodyBytes already set then return r.bodyBytes
if response.bodyBytes != nil {
return response.bodyBytes, nil
}
// Check if Response.resp (*http.Response) is nil
if response.Response == nil {
err := fmt.Errorf("http.Response is nil")
response.client.ErrorLogger.Println(err)
return nil, err
}
// Check if Response.resp.Body (*http.Response.Body) is nil
if response.Response.Body == nil {
err := fmt.Errorf("http.Response's Body is nil")
response.client.ErrorLogger.Println(err)
return nil, err
}
// Read response bodyBytes
b, err := ioutil.ReadAll(response.Response.Body)
if err != nil {
response.client.ErrorLogger.Printf("Can't read http.Response bodyBytes Error: %v!", err)
return nil, err
}
// Set response readBody
response.bodyBytes = b
if len(response.bodyBytes) == 0 {
response.client.InfoLogger.Println("Response body is empty")
}
// Close response bodyBytes
err = response.Response.Body.Close()
if err != nil {
response.client.ErrorLogger.Printf("Can't close http.Response body Error: %v!", err)
return nil, err
}
return b, nil
}
func (response *Response) BodyUnmarshall(v interface{}) error {
resBody, err := response.ReadBody()
if err != nil {
return err
}
//if len(resBody) == 0 can be handled later
err = json.Unmarshal(resBody, v)
if err != nil {
response.client.ErrorLogger.Println(err)
return err
}
return nil
}