-
Notifications
You must be signed in to change notification settings - Fork 14
/
http_client.go
110 lines (102 loc) · 2.85 KB
/
http_client.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
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
package bcsgo
import (
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"strconv"
"time"
)
type HttpClient struct {
client *http.Client
}
func NewHttpClient() *HttpClient {
return &HttpClient{&http.Client{}}
}
func (this *HttpClient) Get(url string) (*http.Response, []byte, error) {
return this.createAndDoRequestForResult(GET, url, nil, nil)
}
func (this *HttpClient) Put(url string, data io.Reader, size int64, modifyHeader func(header *http.Header)) (*http.Response, []byte, error) {
customRequest := func(req *http.Request) {
req.ContentLength = size
if modifyHeader != nil {
modifyHeader(&req.Header)
}
}
return this.createAndDoRequestForResult(PUT, url, data, customRequest)
}
func (this *HttpClient) Head(url string) (*http.Response, []byte, error) {
return this.createAndDoRequestForResult(HEAD, url, nil, nil)
}
func (this *HttpClient) Delete(url string) (*http.Response, []byte, error) {
return this.createAndDoRequestForResult(DELETE, url, nil, nil)
}
func (this *HttpClient) dumpRequest(req *http.Request) {
if DEBUG {
dump, dumpErr := httputil.DumpRequestOut(req, DEBUG_REQUEST_BODY)
if dumpErr != nil {
log.Println("error when dump request:", dumpErr)
}
log.Println("*** request dump ***")
log.Println(string(dump))
}
}
func (this *HttpClient) createAndDoRequestForResult(method string, url string, data io.Reader, customRequest func(*http.Request)) (*http.Response, []byte, error) {
req, err := http.NewRequest(method, url, data)
if err != nil {
return nil, nil, err
}
if customRequest != nil {
customRequest(req)
}
this.dumpRequest(req)
var oldTime time.Time
if DEBUG {
oldTime = time.Now()
}
resp, err := this.client.Do(req)
if DEBUG {
log.Println(time.Now().Sub(oldTime))
}
respData, err := this.handleResponseContent(resp, err)
return resp, respData, err
}
func (this *HttpClient) handleResponseContent(resp *http.Response, err error) ([]byte, error) {
if DEBUG {
dump, dumpErr := httputil.DumpResponse(resp, true)
if dumpErr != nil {
log.Println("error when dump response:", dumpErr)
}
log.Println("*** response dump ***")
log.Println(string(dump))
}
if err != nil {
return nil, err
} else {
return readAllResponseBodyWithError(resp)
}
}
func mergeResponseError(err error, resp *http.Response) error {
if err != nil {
return err
} else if resp.StatusCode != http.StatusOK {
return errors.New("request not ok, status: " + strconv.Itoa(resp.StatusCode) +
", body: " + string(readAllResponseBodyIgnoreError(resp)))
} else {
return nil
}
}
func readAllResponseBodyIgnoreError(resp *http.Response) []byte {
respData, _ := readAllResponseBodyWithError(resp)
return respData
}
func readAllResponseBodyWithError(resp *http.Response) ([]byte, error) {
// defer resp.Body.Close()
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
return respData, err
}