Files
open-im-server/pkg/common/http/http_client.go
T

139 lines
3.9 KiB
Go
Raw Normal View History

// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package http
2023-06-29 22:35:31 +08:00
import (
"bytes"
2023-06-30 09:45:02 +08:00
"context"
2023-06-29 22:35:31 +08:00
"encoding/json"
2023-06-30 09:45:02 +08:00
"io"
2023-06-29 22:35:31 +08:00
"net/http"
"time"
2023-06-30 09:45:02 +08:00
"github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/log"
2023-08-04 21:38:30 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2023-06-29 22:35:31 +08:00
)
2023-11-05 18:37:25 +08:00
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
}
2023-06-30 09:45:02 +08:00
2023-06-29 22:35:31 +08:00
func Get(url string) (response []byte, err error) {
2023-11-05 18:37:25 +08:00
hclient := http.Client{Timeout: 5 * time.Second}
resp, err := hclient.Get(url)
2023-06-29 22:35:31 +08:00
if err != nil {
return nil, err
}
2023-11-05 18:37:25 +08:00
2023-06-29 22:35:31 +08:00
defer resp.Body.Close()
2023-10-24 20:28:22 +08:00
body, err := io.ReadAll(resp.Body)
if err != nil {
2023-06-29 22:35:31 +08:00
return nil, err
}
return body, nil
}
func Post(ctx context.Context, url string, header map[string]string, data any, timeout int) (content []byte, err error) {
2023-06-30 09:45:02 +08:00
if timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
defer cancel()
}
2023-11-05 18:37:25 +08:00
2023-06-29 22:35:31 +08:00
jsonStr, err := json.Marshal(data)
if err != nil {
return nil, err
}
2023-11-05 18:37:25 +08:00
2023-06-30 09:45:02 +08:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonStr))
2023-06-29 22:35:31 +08:00
if err != nil {
return nil, err
}
2023-11-05 18:37:25 +08:00
2023-06-30 09:45:02 +08:00
if operationID, _ := ctx.Value(constant.OperationID).(string); operationID != "" {
req.Header.Set(constant.OperationID, operationID)
}
for k, v := range header {
req.Header.Set(k, v)
}
2023-06-29 22:35:31 +08:00
req.Header.Add("content-type", "application/json; charset=utf-8")
2023-11-05 18:37:25 +08:00
2023-06-29 22:35:31 +08:00
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2023-11-05 18:37:25 +08:00
2023-06-30 09:45:02 +08:00
result, err := io.ReadAll(resp.Body)
2023-06-29 22:35:31 +08:00
if err != nil {
return nil, err
}
2023-11-05 18:37:25 +08:00
2023-06-29 22:35:31 +08:00
return result, nil
}
func PostReturn(ctx context.Context, url string, header map[string]string, input, output any, timeOutSecond int) error {
2023-06-30 09:45:02 +08:00
b, err := Post(ctx, url, header, input, timeOutSecond)
2023-06-29 22:35:31 +08:00
if err != nil {
return err
}
2023-06-30 09:45:02 +08:00
err = json.Unmarshal(b, output)
return err
}
2023-11-05 18:37:25 +08:00
func callBackPostReturn(ctx context.Context, url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
2023-11-28 15:26:46 +08:00
defer log.ZDebug(ctx, "callback", "url", url, "command", command, "input", input, "output", output, "callbackConfig", callbackConfig)
//
//v := urllib.Values{}
//v.Set(constant.CallbackCommand, command)
//url = url + "/" + v.Encode()
url = url + "/" + command
2023-06-30 09:45:02 +08:00
b, err := Post(ctx, url, nil, input, callbackConfig.CallbackTimeOut)
if err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
2023-11-28 15:26:46 +08:00
return nil
2023-06-30 09:45:02 +08:00
}
return errs.ErrNetwork.Wrap(err.Error())
}
2023-12-26 10:15:15 +08:00
defer log.ZDebug(ctx, "callback", "data", string(b))
2023-11-05 18:37:25 +08:00
2023-06-29 22:35:31 +08:00
if err = json.Unmarshal(b, output); err != nil {
2023-06-30 09:45:02 +08:00
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
2023-11-28 15:26:46 +08:00
return nil
2023-06-30 09:45:02 +08:00
}
return errs.ErrData.WithDetail(err.Error() + "response format error")
2023-06-29 22:35:31 +08:00
}
2023-11-05 18:37:25 +08:00
2023-06-30 09:45:02 +08:00
return output.Parse()
}
2023-11-05 18:37:25 +08:00
func CallBackPostReturn(ctx context.Context, url string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
2023-06-30 09:45:02 +08:00
return callBackPostReturn(ctx, url, req.GetCallbackCommand(), req, resp, callbackConfig)
2023-06-29 22:35:31 +08:00
}