|
|
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})
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|