httplib_base_service_gateway.go
4.2 KB
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
package serviceGateway
import (
"crypto/tls"
"encoding/json"
"github.com/beego/beego/v2/client/httplib"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
"io/ioutil"
"net/http"
"reflect"
"strings"
"time"
)
type httpLibBaseServiceGateway struct {
baseURL string
connectTimeout time.Duration
readWriteTimeout time.Duration
request *httplib.BeegoHTTPRequest
params map[string]interface{}
body string
}
func (client *httpLibBaseServiceGateway) CreateRequest(method, url string) {
client.request = httplib.NewBeegoRequest(client.baseURL+url, strings.ToUpper(method))
client.request.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
// 设置请求参数
func (client *httpLibBaseServiceGateway) SetParams(params map[string]string) {
if len(params) > 0 {
for key, value := range params {
client.request.Param(key, value)
client.params[key] = value
}
}
}
// 设置请求参数
func (client *httpLibBaseServiceGateway) SetParam(key, value string) {
client.request.Param(key, value)
client.params[key] = value
}
// 设置body
func (client *httpLibBaseServiceGateway) SetBody(body interface{}) {
//判断是否为string
ty := reflect.TypeOf(body)
if ty.Name() == "string" {
client.request.Body(body)
client.body = body.(string)
} else {
mBytes, _ := json.Marshal(body)
client.request.Body(mBytes)
client.body = string(mBytes)
}
client.request.Header("Content-Type", "application/json")
client.request.Header("Accept", "application/json")
}
// 设置头信息
func (client *httpLibBaseServiceGateway) SetHeader(key, value string) {
client.request.Header(key, value)
}
// 设置多个头部信息
func (client *httpLibBaseServiceGateway) SetHeaders(headers map[string]string) {
if len(headers) > 0 {
for key, value := range headers {
client.request.Header(key, value)
}
}
}
// 设置超时时间
func (client *httpLibBaseServiceGateway) SetTimeout(connectTimeout, readWriteTimeout time.Duration) {
client.request.SetTimeout(connectTimeout, readWriteTimeout)
}
// 设置cookie
func (client *httpLibBaseServiceGateway) SetCookie(cookie *http.Cookie) {
client.request.SetCookie(cookie)
}
// 设置UserAgent
func (client *httpLibBaseServiceGateway) SetUserAgent(userAgent string) {
client.request.SetUserAgent(userAgent)
}
// 请求结果返回结构体
func (client *httpLibBaseServiceGateway) ToJson(result interface{}) error {
response, err := client.request.Response()
if err != nil {
return err
}
client.addOptionLog(response)
mBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = json.Unmarshal(mBytes, result)
//增加返回数据日志
log.Logger.Debug(response.Request.Method+" "+response.Request.URL.String()+"----response----", tool_funs.SimpleStructToMap(result))
return err
}
// 请求结果返回string
func (client *httpLibBaseServiceGateway) ToString() (string, error) {
response, err := client.request.Response()
if err != nil {
return "", err
}
client.addOptionLog(response)
mBytes, err := ioutil.ReadAll(response.Body)
//增加返回数据日志
log.Logger.Debug(response.Request.Method + " " + response.Request.URL.String() + "----response----" + string(mBytes))
return string(mBytes), err
}
// 请求结果返回bytes
func (client *httpLibBaseServiceGateway) ToBytes() ([]byte, error) {
response, err := client.request.Response()
if err != nil {
return nil, err
}
client.addOptionLog(response)
mBytes, _ := ioutil.ReadAll(response.Body)
//增加返回数据日志
log.Logger.Debug(response.Request.Method + " " + response.Request.URL.String() + "----response----" + string(mBytes))
return mBytes, nil
}
// 添加options日志
func (client *httpLibBaseServiceGateway) addOptionLog(response *http.Response) {
logTxt := response.Request.Method + " " + response.Request.URL.String()
if response.Request.Method != http.MethodGet {
contentType := client.request.GetRequest().Header.Get("Content-Type")
if strings.Contains(strings.ToLower(contentType), "json") {
log.Logger.Debug(logTxt+" ----options----", map[string]interface{}{"body": client.body})
} else {
log.Logger.Debug(logTxt+" ----options----", map[string]interface{}{"params": client.params})
}
}
}