mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-07 21:17:17 +08:00
fix: fix the callback example
This commit is contained in:
parent
ca0bfe9411
commit
b99ebe0058
@ -16,6 +16,7 @@ package group
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/tools/log"
|
||||
"time"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
@ -66,24 +67,14 @@ func CallbackBeforeCreateGroup(ctx context.Context, req *group.CreateGroupReq) (
|
||||
config.Config.Callback.CallbackBeforeCreateGroup,
|
||||
)
|
||||
if err != nil {
|
||||
if err == errs.ErrCallbackContinue {
|
||||
if errs.Unwrap(err) == errs.ErrCallbackContinue {
|
||||
log.ZWarn(ctx, "callback failed but continue", err, "url", config.Config.Callback.CallbackUrl)
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
utils.NotNilReplace(&req.GroupInfo.GroupID, resp.GroupID)
|
||||
utils.NotNilReplace(&req.GroupInfo.GroupName, resp.GroupName)
|
||||
utils.NotNilReplace(&req.GroupInfo.Notification, resp.Notification)
|
||||
utils.NotNilReplace(&req.GroupInfo.Introduction, resp.Introduction)
|
||||
utils.NotNilReplace(&req.GroupInfo.FaceURL, resp.FaceURL)
|
||||
utils.NotNilReplace(&req.GroupInfo.OwnerUserID, resp.OwnerUserID)
|
||||
utils.NotNilReplace(&req.GroupInfo.Ex, resp.Ex)
|
||||
utils.NotNilReplace(&req.GroupInfo.Status, resp.Status)
|
||||
utils.NotNilReplace(&req.GroupInfo.CreatorUserID, resp.CreatorUserID)
|
||||
utils.NotNilReplace(&req.GroupInfo.GroupType, resp.GroupType)
|
||||
utils.NotNilReplace(&req.GroupInfo.NeedVerification, resp.NeedVerification)
|
||||
utils.NotNilReplace(&req.GroupInfo.LookMemberInfo, resp.LookMemberInfo)
|
||||
|
||||
utils.StructFieldNotNilReplace(req, resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -92,7 +83,7 @@ func CallbackAfterCreateGroup(ctx context.Context, req *group.CreateGroupReq) (e
|
||||
return nil
|
||||
}
|
||||
cbReq := &callbackstruct.CallbackAfterCreateGroupReq{
|
||||
CallbackCommand: "callbackAfterCreateGroupCommand",
|
||||
CallbackCommand: constant.CallbackAfterCreateGroupCommand,
|
||||
OperationID: mcontext.GetOperationID(ctx),
|
||||
GroupInfo: req.GroupInfo,
|
||||
}
|
||||
@ -121,9 +112,6 @@ func CallbackAfterCreateGroup(ctx context.Context, req *group.CreateGroupReq) (e
|
||||
config.Config.Callback.CallbackAfterCreateGroup,
|
||||
)
|
||||
if err != nil {
|
||||
if err == errs.ErrCallbackContinue {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -303,6 +303,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbgroup.CreateGroupR
|
||||
MemberUserIDs: userIDs,
|
||||
GroupInfo: resp.GroupInfo,
|
||||
OwnerUserID: req.OwnerUserID,
|
||||
AdminUserIDs: req.AdminUserIDs,
|
||||
}
|
||||
|
||||
if err := CallbackAfterCreateGroup(ctx, reqCallBackAfter); err != nil {
|
||||
|
||||
@ -16,6 +16,10 @@ package callbackstruct
|
||||
|
||||
import "github.com/OpenIMSDK/tools/errs"
|
||||
|
||||
const (
|
||||
Next = 1
|
||||
)
|
||||
|
||||
type CommonCallbackReq struct {
|
||||
SendID string `json:"sendID"`
|
||||
CallbackCommand string `json:"callbackCommand"`
|
||||
@ -57,7 +61,7 @@ type CommonCallbackResp struct {
|
||||
}
|
||||
|
||||
func (c CommonCallbackResp) Parse() error {
|
||||
if c.ActionCode != errs.NoError || c.NextCode == 1 {
|
||||
if c.ActionCode != errs.NoError || c.NextCode == Next {
|
||||
return errs.NewCodeError(int(c.ErrCode), c.ErrMsg).WithDetail(c.ErrDlt)
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -57,7 +57,8 @@ func Get(url string) (response []byte, err error) {
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func Post(ctx context.Context, url string, header map[string]string, data interface{}, timeout int) (content []byte, err error) {
|
||||
func Post(ctx context.Context, url string, header map[string]string, data interface{}, timeout int) (result []byte, err error) {
|
||||
defer log.ZDebug(ctx, "callbackPost", "url", url, "header", header, "input", data, "output", result, "timeout", timeout, "output")
|
||||
if timeout > 0 {
|
||||
var cancel func()
|
||||
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
|
||||
@ -88,7 +89,7 @@ func Post(ctx context.Context, url string, header map[string]string, data interf
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
result, err := io.ReadAll(resp.Body)
|
||||
result, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -106,8 +107,8 @@ func PostReturn(ctx context.Context, url string, header map[string]string, input
|
||||
}
|
||||
|
||||
func callBackPostReturn(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, "input", input, "output", output, "timeout", callbackConfig.CallbackTimeOut)
|
||||
|
||||
//v := urllib.Values{}
|
||||
//v.Set(constant.CallbackCommand, command)
|
||||
//url = url + "/" + v.Encode()
|
||||
@ -116,16 +117,14 @@ func callBackPostReturn(ctx context.Context, url, command string, input interfac
|
||||
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)
|
||||
return errs.ErrCallbackContinue
|
||||
return errs.ErrCallbackContinue.Wrap(err.Error())
|
||||
}
|
||||
return errs.ErrNetwork.Wrap(err.Error())
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
||||
return errs.ErrCallbackContinue
|
||||
return errs.ErrCallbackContinue.Wrap(err.Error())
|
||||
}
|
||||
return errs.ErrData.Wrap(err.Error())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user