|
|
@ -31,14 +31,25 @@ import (
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var client http.Client
|
|
|
|
var (
|
|
|
|
|
|
|
|
// define http client.
|
|
|
|
|
|
|
|
client = &http.Client{
|
|
|
|
|
|
|
|
Timeout: 15 * time.Second, // max timeout is 15s
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
// reset http default transport
|
|
|
|
|
|
|
|
http.DefaultTransport.(*http.Transport).MaxConnsPerHost = 100 // default: 2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Get(url string) (response []byte, err error) {
|
|
|
|
func Get(url string) (response []byte, err error) {
|
|
|
|
client := http.Client{Timeout: 5 * time.Second}
|
|
|
|
hclient := http.Client{Timeout: 5 * time.Second}
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
resp, err := hclient.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -47,26 +58,23 @@ func Get(url string) (response []byte, err error) {
|
|
|
|
return body, nil
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Post(
|
|
|
|
func Post(ctx context.Context, url string, header map[string]string, data interface{}, timeout int) (content []byte, err error) {
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
|
|
url string,
|
|
|
|
|
|
|
|
header map[string]string,
|
|
|
|
|
|
|
|
data interface{},
|
|
|
|
|
|
|
|
timeout int,
|
|
|
|
|
|
|
|
) (content []byte, err error) {
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
if timeout > 0 {
|
|
|
|
var cancel func()
|
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonStr, err := json.Marshal(data)
|
|
|
|
jsonStr, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonStr))
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonStr))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if operationID, _ := ctx.Value(constant.OperationID).(string); operationID != "" {
|
|
|
|
if operationID, _ := ctx.Value(constant.OperationID).(string); operationID != "" {
|
|
|
|
req.Header.Set(constant.OperationID, operationID)
|
|
|
|
req.Header.Set(constant.OperationID, operationID)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -74,25 +82,22 @@ func Post(
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.Header.Add("content-type", "application/json; charset=utf-8")
|
|
|
|
req.Header.Add("content-type", "application/json; charset=utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
result, err := io.ReadAll(resp.Body)
|
|
|
|
result, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func PostReturn(
|
|
|
|
func PostReturn(ctx context.Context, url string, header map[string]string, input, output interface{}, timeOutSecond int) error {
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
|
|
url string,
|
|
|
|
|
|
|
|
header map[string]string,
|
|
|
|
|
|
|
|
input, output interface{},
|
|
|
|
|
|
|
|
timeOutSecond int,
|
|
|
|
|
|
|
|
) error {
|
|
|
|
|
|
|
|
b, err := Post(ctx, url, header, input, timeOutSecond)
|
|
|
|
b, err := Post(ctx, url, header, input, timeOutSecond)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -101,17 +106,13 @@ func PostReturn(
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func callBackPostReturn(
|
|
|
|
func callBackPostReturn(ctx context.Context, url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
|
|
url, command string,
|
|
|
|
|
|
|
|
input interface{},
|
|
|
|
|
|
|
|
output callbackstruct.CallbackResp,
|
|
|
|
|
|
|
|
callbackConfig config.CallBackConfig,
|
|
|
|
|
|
|
|
) error {
|
|
|
|
|
|
|
|
defer log.ZDebug(ctx, "callback", "url", url, "command", command, "input", input, "callbackConfig", callbackConfig)
|
|
|
|
defer log.ZDebug(ctx, "callback", "url", url, "command", command, "input", input, "callbackConfig", callbackConfig)
|
|
|
|
|
|
|
|
|
|
|
|
v := urllib.Values{}
|
|
|
|
v := urllib.Values{}
|
|
|
|
v.Set(constant.CallbackCommand, command)
|
|
|
|
v.Set(constant.CallbackCommand, command)
|
|
|
|
url = url + "?" + v.Encode()
|
|
|
|
url = url + "?" + v.Encode()
|
|
|
|
|
|
|
|
|
|
|
|
b, err := Post(ctx, url, nil, input, callbackConfig.CallbackTimeOut)
|
|
|
|
b, err := Post(ctx, url, nil, input, callbackConfig.CallbackTimeOut)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
|
|
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
|
|
@ -120,6 +121,7 @@ func callBackPostReturn(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.ErrNetwork.Wrap(err.Error())
|
|
|
|
return errs.ErrNetwork.Wrap(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err = json.Unmarshal(b, output); err != nil {
|
|
|
|
if err = json.Unmarshal(b, output); err != nil {
|
|
|
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
|
|
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
|
|
|
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
|
|
|
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
|
|
@ -127,15 +129,10 @@ func callBackPostReturn(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.ErrData.Wrap(err.Error())
|
|
|
|
return errs.ErrData.Wrap(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return output.Parse()
|
|
|
|
return output.Parse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CallBackPostReturn(
|
|
|
|
func CallBackPostReturn(ctx context.Context, url string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
|
|
url string,
|
|
|
|
|
|
|
|
req callbackstruct.CallbackReq,
|
|
|
|
|
|
|
|
resp callbackstruct.CallbackResp,
|
|
|
|
|
|
|
|
callbackConfig config.CallBackConfig,
|
|
|
|
|
|
|
|
) error {
|
|
|
|
|
|
|
|
return callBackPostReturn(ctx, url, req.GetCallbackCommand(), req, resp, callbackConfig)
|
|
|
|
return callBackPostReturn(ctx, url, req.GetCallbackCommand(), req, resp, callbackConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|