-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.go
94 lines (75 loc) · 1.59 KB
/
httpclient.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
package slack
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
)
func (e *Engine) do(method string, path string, param []byte) ([]byte, error) {
var (
req *http.Request
res *http.Response
reqURL *url.URL
reqBody io.Reader
resBody []byte
err error
)
reqURL, err = url.Parse(path)
if err != nil {
return resBody, err
}
reqBody = bytes.NewBuffer(param)
req, err = http.NewRequest(method, reqURL.String(), reqBody)
if err != nil {
return resBody, err
}
res, err = e.client.Do(req)
if err != nil {
return resBody, err
}
defer res.Body.Close()
resBody, err = ioutil.ReadAll(res.Body)
if err != nil {
return resBody, err
}
return resBody, nil
}
func (e *Engine) doJSON(method string, path string, param, response interface{}) error {
var (
jsonByte []byte
resBody []byte
err error
)
jsonByte, err = json.Marshal(param)
if err != nil {
return err
}
resBody, err = e.do(method, path, jsonByte)
if err != nil {
return err
}
err = json.Unmarshal(resBody, response)
return err
}
// doString function will perform http request with json param (if any)
// and return string response (for not json response) like slack only response with " ok"
func (e *Engine) doString(method string, path string, param interface{}) (string, error) {
var (
jsonByte []byte
resBody []byte
err error
resp string
)
jsonByte, err = json.Marshal(param)
if err != nil {
return resp, err
}
resBody, err = e.do(method, path, jsonByte)
if err != nil {
return resp, err
}
resp = string(resBody)
return resp, err
}