mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-10 03:56:00 +08:00
v3 - main to cut out
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
** description("").
|
||||
** copyright('open-im,www.open-im.io').
|
||||
** author("fg,Gordon@tuoyun.net").
|
||||
** time(2021/5/27 10:31).
|
||||
*/
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"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
|
||||
func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) {
|
||||
jsonStr, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Close = true
|
||||
req.Header.Add("content-type", "application/json; charset=utf-8")
|
||||
|
||||
client := &http.Client{Timeout: time.Duration(timeOutSecond) * time.Second}
|
||||
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
|
||||
}
|
||||
|
||||
func PostReturn(url string, input, output interface{}, timeOut int) error {
|
||||
b, err := Post(url, input, timeOut)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
//"Open_IM/pkg/cms_api_struct"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type BaseResp struct {
|
||||
Code int32 `json:"code"`
|
||||
ErrMsg string `json:"err_msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func RespHttp200(ctx *gin.Context, err error, data interface{}) {
|
||||
var resp BaseResp
|
||||
switch e := err.(type) {
|
||||
case constant.ErrInfo:
|
||||
resp.Code = e.ErrCode
|
||||
resp.ErrMsg = e.ErrMsg
|
||||
default:
|
||||
s, ok := status.FromError(err)
|
||||
if !ok {
|
||||
fmt.Println("need grpc format error")
|
||||
return
|
||||
}
|
||||
resp.Code = int32(s.Code())
|
||||
resp.ErrMsg = s.Message()
|
||||
}
|
||||
resp.Data = data
|
||||
ctx.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// warp error
|
||||
func WrapError(err constant.ErrInfo) error {
|
||||
return status.Error(codes.Code(err.ErrCode), err.ErrMsg)
|
||||
}
|
||||
Reference in New Issue
Block a user