You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/http/http_client.go

95 lines
2.5 KiB

3 years ago
/*
** description("").
** copyright('open-im,www.open-im.io').
** author("fg,Gordon@tuoyun.net").
** time(2021/5/27 10:31).
*/
package http
import (
2 years ago
"OpenIM/pkg/callbackstruct"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
3 years ago
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
2 years ago
urlLib "net/url"
3 years ago
"time"
)
func Get(url string) (response []byte, err error) {
client := http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// application/json; charset=utf-8
2 years ago
func Post(url string, header map[string]string, data interface{}, timeout int) (content []byte, err error) {
jsonStr, err := json.Marshal(data)
if err != nil {
return nil, err
}
3 years ago
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
2 years ago
for k, v := range header {
req.Header.Set(k, v)
}
req.Close = true
req.Header.Add("content-type", "application/json; charset=utf-8")
2 years ago
client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
3 years ago
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return result, nil
}
3 years ago
2 years ago
func PostReturn(url string, header map[string]string, input, output interface{}, timeOutSecond int) error {
b, err := Post(url, header, input, timeOutSecond)
if err != nil {
return err
}
err = json.Unmarshal(b, output)
return err
}
func callBackPostReturn(url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
2 years ago
v := urlLib.Values{}
2 years ago
v.Set("callbackCommand", command)
2 years ago
url = url + "?" + v.Encode()
2 years ago
b, err := Post(url, nil, input, callbackConfig.CallbackTimeOut)
3 years ago
if err != nil {
2 years ago
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
2 years ago
return constant.ErrCallbackContinue
2 years ago
}
2 years ago
return constant.NewErrNetwork(err)
3 years ago
}
if err = json.Unmarshal(b, output); err != nil {
2 years ago
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
2 years ago
return constant.ErrCallbackContinue
2 years ago
}
2 years ago
return constant.NewErrData(err)
3 years ago
}
2 years ago
return output.Parse()
3 years ago
}
2 years ago
2 years ago
func CallBackPostReturn(url string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error {
2 years ago
return callBackPostReturn(url, req.GetCallbackCommand(), req, resp, callbackConfig)
2 years ago
}