forked from tomcfoglio/mycenae
/
http.go
171 lines (133 loc) · 3.63 KB
/
http.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package tools
import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type httpTool struct {
hostname string
port string
timeout time.Duration
client *http.Client
}
type User struct {
Name string
PassWord string
}
type Headers struct {
User *User
Header map[string]string
Cookie *bytes.Buffer
}
func (hT *httpTool) Init(hostname string, port string, timeout time.Duration) {
hT.hostname = hostname
hT.port = port
hT.timeout = timeout
hT.client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: hT.timeout,
}
}
func (hT *httpTool) POSTstring(url string, payload string) (statusCode int, respData []byte) {
statusCode, respData, _ = hT.POST(url, []byte(payload))
return
}
func (hT *httpTool) POSTjson(url string, postData interface{}, respData interface{}) (statusCode int) {
payload, err := json.Marshal(postData)
if err != nil {
log.Println("HTTPclient:", "postJSON:", "Marshal:", err)
return
}
statusCode, respBytes, _ := hT.POST(url, payload)
if len(respBytes) > 0 {
err = json.Unmarshal(respBytes, respData)
if err != nil {
log.Println("HTTPclient:", "POSTjson:", "Unmarshal:", err)
}
}
return
}
func (hT *httpTool) GETjson(url string, respData interface{}) (statusCode int) {
statusCode, resp, _ := hT.GET(url)
err := json.Unmarshal(resp, respData)
if err != nil {
log.Println("HTTPclient:", "GETjson:", "Unmarshal:", err)
}
return
}
func (hT *httpTool) GET(url string) (statusCode int, respData []byte, err error) {
var payload []byte
return hT.request("GET", url, payload, false)
}
func (hT *httpTool) POST(url string, payload []byte) (statusCode int, respData []byte, err error) {
statusCode, respData, err = hT.request("POST", url, payload, false)
return
}
func (hT *httpTool) POSTgziped(url string, payload []byte) (statusCode int, respData []byte, err error) {
statusCode, respData, err = hT.request("POST", url, payload, true)
return
}
func (hT *httpTool) PUT(url string, payload []byte) (statusCode int, respData []byte, err error) {
statusCode, respData, err = hT.request("PUT", url, payload, false)
return
}
func (hT *httpTool) DELETE(url string) (statusCode int, respData []byte, err error) {
var payload []byte
statusCode, respData, err = hT.request("DELETE", url, payload, false)
return
}
func (hT *httpTool) request(method string, url string, payload []byte, gzipit bool) (statusCode int, respData []byte, err error) {
fullPath := ""
if hT.port == "" {
fullPath = fmt.Sprintf("%v/%v", hT.hostname, url)
} else {
fullPath = fmt.Sprintf("%v:%v/%v", hT.hostname, hT.port, url)
}
var req *http.Request
if len(payload) == 0 {
req, err = http.NewRequest(method, fullPath, nil)
} else {
if gzipit {
var bb bytes.Buffer
gzw := gzip.NewWriter(&bb)
if _, err = gzw.Write(payload); err != nil {
log.Println("HTTPclient:", method, "gzip:", err)
return
}
if err = gzw.Close(); err != nil {
log.Println("HTTPclient:", method, "gzip:", err)
return
}
req, err = http.NewRequest(method, fullPath, &bb)
req.Header.Add("Content-Encoding", "gzip")
} else {
req, err = http.NewRequest(method, fullPath, bytes.NewBuffer(payload))
}
}
if err != nil {
log.Println("HTTPclient:", method, "NewRequest:", err)
return
}
resp, err := hT.client.Do(req)
if err != nil {
log.Println("HTTPclient:", method, "Do:", err)
return
}
defer resp.Body.Close()
statusCode = resp.StatusCode
respData, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("HTTPclient:", method, "ReadAll:", err)
}
return
}