-
Notifications
You must be signed in to change notification settings - Fork 0
/
httputil.go
61 lines (53 loc) · 1.25 KB
/
httputil.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
package requestUtil
import (
"time"
"github.com/imroc/req"
)
//type HttpUtil struct {
//}
//
//type RespBody struct {
// Status int `json:"status"`
// Data interface{} `json:"data"`
// Msg string `json:"msg"`
//}
//func New() *HttpUtil {
// return &HttpUtil{}
//}
//
//func GenResponseStruct(v interface{}) *RespBody {
// if v == nil {
// return &RespBody{}
// }
// return &RespBody{
// Data: v,
// }
//}
func Get(url string, param req.Param, timeoutSecond int, authorizationToken string) (*req.Resp, error) {
header := req.Header{}
if authorizationToken != "" {
header = req.Header{
"Authorization": "Basic " + authorizationToken,
}
}
r := req.New()
if timeoutSecond > 0 {
r.SetTimeout(time.Duration(timeoutSecond) * time.Second)
}
return r.Get(url, header, param)
}
func Post(url string, param req.Param, bodyJson interface{}, timeoutSecond int, authorizationToken string) (*req.Resp, error) {
header := req.Header{
"Accept": "application/json",
}
if authorizationToken != "" {
header = req.Header{
"Authorization": "Basic " + authorizationToken,
}
}
r := req.New()
if timeoutSecond > 0 {
r.SetTimeout(time.Duration(timeoutSecond) * time.Second)
}
return r.Post(url, header, param, req.BodyJSON(bodyJson))
}