mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 08:05:58 +08:00
api2rpc
This commit is contained in:
+23
-65
@@ -1,5 +1,12 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"OpenIM/internal/api2rpc"
|
||||
"OpenIM/pkg/apistruct"
|
||||
"OpenIM/pkg/proto/group"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//import (
|
||||
// common "OpenIM/internal/api_to_rpc"
|
||||
// api "OpenIM/pkg/apistruct"
|
||||
@@ -23,73 +30,24 @@ package group
|
||||
// jsonData "OpenIM/internal/utils"
|
||||
//)
|
||||
|
||||
// @Summary 把用户踢出群组
|
||||
// @Description 把用户踢出群组
|
||||
// @Tags 群组相关
|
||||
// @ID KickGroupMember
|
||||
// @Accept json
|
||||
// @Param token header string true "im token"
|
||||
// @Param req body api.KickGroupMemberReq true "GroupID为要操作的群ID <br> kickedUserIDList为要踢出的群用户ID <br> reason为原因"
|
||||
// @Produce json
|
||||
// @Success 0 {object} api.KickGroupMemberResp "result为结果码, -1为失败, 0为成功"
|
||||
// @Failure 500 {object} api.Swagger500Resp "errCode为500 一般为服务器内部错误"
|
||||
// @Failure 400 {object} api.Swagger400Resp "errCode为400 一般为参数输入错误, token未带上等"
|
||||
// @Router /group/kick_group [post]
|
||||
//func KickGroupMember(c *gin.Context) {
|
||||
// params := api.KickGroupMemberReq{}
|
||||
// if err := c.BindJSON(¶ms); err != nil {
|
||||
// log.NewError("0", "BindJSON failed ", err.Error())
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
// if len(params.KickedUserIDList) > constant.MaxNotificationNum {
|
||||
// errMsg := params.OperationID + " too many members " + utils.Int32ToString(int32(len(params.KickedUserIDList)))
|
||||
// log.Error(params.OperationID, errMsg)
|
||||
// c.JSON(http.StatusOK, gin.H{"errCode": 400, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// req := &rpc.KickGroupMemberReq{}
|
||||
// utils.CopyStructFields(req, ¶ms)
|
||||
// var ok bool
|
||||
// var errInfo string
|
||||
// ok, req.OpUserID, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
// if !ok {
|
||||
// errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// log.NewInfo(req.OperationID, "KickGroupMember args ", req.String())
|
||||
//
|
||||
// etcdConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
// if etcdConn == nil {
|
||||
// errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// client := rpc.NewGroupClient(etcdConn)
|
||||
// RpcResp, err := client.KickGroupMember(context.Background(), req)
|
||||
// if err != nil {
|
||||
// log.NewError(req.OperationID, "FindGroupMemberAll failed ", err.Error(), req.String())
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// var memberListResp api.KickGroupMemberResp
|
||||
// memberListResp.ErrMsg = RpcResp.CommonResp.ErrMsg
|
||||
// memberListResp.ErrCode = RpcResp.CommonResp.ErrCode
|
||||
// for _, v := range RpcResp.Id2ResultList {
|
||||
// memberListResp.UserIDResultList = append(memberListResp.UserIDResultList, &api.UserIDResult{UserID: v.UserID, Result: v.Result})
|
||||
// }
|
||||
// if len(memberListResp.UserIDResultList) == 0 {
|
||||
// memberListResp.UserIDResultList = []*api.UserIDResult{}
|
||||
// }
|
||||
//
|
||||
// log.NewInfo(req.OperationID, "KickGroupMember api return ", memberListResp)
|
||||
// c.JSON(http.StatusOK, memberListResp)
|
||||
// api2rpc.NewRpc(api2rpc.NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), group.NewGroupClient, group.GroupClient.KickGroupMember).Name("group").Call()
|
||||
//}
|
||||
|
||||
func KickGroupMember(c *gin.Context) {
|
||||
// 默认 全部自动
|
||||
api := api2rpc.NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c)
|
||||
api2rpc.NewRpc(api, group.NewGroupClient, group.GroupClient.KickGroupMember).Name("group").Call()
|
||||
|
||||
//// 可以自定义编辑请求和响应
|
||||
//a := NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
//a.Before(func(apiReq *apistruct.KickGroupMemberReq, rpcReq *group.KickGroupMemberReq, bind func() error) error {
|
||||
// return bind()
|
||||
//}).After(func(rpcResp *group.KickGroupMemberResp, apiResp *apistruct.KickGroupMemberResp, bind func() error) error {
|
||||
// return bind()
|
||||
//}).Name("group").Call()
|
||||
}
|
||||
|
||||
//
|
||||
//// @Summary 获取群成员信息
|
||||
//// @Description 获取群成员信息
|
||||
|
||||
@@ -1,32 +1,23 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
common "OpenIM/internal/api2rpc"
|
||||
"OpenIM/pkg/apistruct"
|
||||
"OpenIM/pkg/proto/group"
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Ignore struct{}
|
||||
|
||||
func temp(client group.GroupClient, ctx context.Context, in *group.KickGroupMemberReq, opts ...grpc.CallOption) (*group.KickGroupMemberResp, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type ApiBind[A, B any] interface {
|
||||
OperationID() string
|
||||
OpUserID() (string, error)
|
||||
Bind(*A) error
|
||||
Error(error)
|
||||
Write(*B)
|
||||
Context() context.Context
|
||||
Resp(resp *B, err error)
|
||||
}
|
||||
|
||||
func NewApiBind[A, B any](c *gin.Context) ApiBind[A, B] {
|
||||
func NewGin[A, B any](c *gin.Context) ApiBind[A, B] {
|
||||
return &ginApiBind[A, B]{
|
||||
c: c,
|
||||
}
|
||||
@@ -48,6 +39,14 @@ func (g *ginApiBind[A, B]) Bind(a *A) error {
|
||||
return g.c.BindJSON(a)
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Resp(resp *B, err error) {
|
||||
if err == nil {
|
||||
g.Write(resp)
|
||||
} else {
|
||||
g.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Error(err error) {
|
||||
//TODO implement me
|
||||
}
|
||||
@@ -56,71 +55,117 @@ func (g *ginApiBind[A, B]) Write(b *B) {
|
||||
//TODO implement me
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
//var bind ApiBind[int, int]
|
||||
//NewRpc(bind, "", group.NewGroupClient, temp)
|
||||
|
||||
var c *gin.Context
|
||||
NewRpc(NewApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
|
||||
func (g *ginApiBind[A, B]) Context() context.Context {
|
||||
return g.c
|
||||
}
|
||||
|
||||
//func TestName(t *testing.T) {
|
||||
// //var bind ApiBind[int, int]
|
||||
// //NewRpc(bind, "", group.NewGroupClient, temp)
|
||||
//
|
||||
// var c *gin.Context
|
||||
// NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
//}
|
||||
|
||||
func KickGroupMember(c *gin.Context) {
|
||||
// 默认 全部自动
|
||||
NewRpc(NewApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember).Execute()
|
||||
NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), group.NewGroupClient, group.GroupClient.KickGroupMember).Call()
|
||||
// 可以自定义编辑请求和响应
|
||||
a := NewRpc(NewApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
a := NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
a.Before(func(apiReq *apistruct.KickGroupMemberReq, rpcReq *group.KickGroupMemberReq, bind func() error) error {
|
||||
return bind()
|
||||
}).After(func(rpcResp *group.KickGroupMemberResp, apiResp *apistruct.KickGroupMemberResp, bind func() error) error {
|
||||
return bind()
|
||||
}).Execute()
|
||||
}).Call()
|
||||
}
|
||||
|
||||
func NewRpc[A, B any, C, D any, Z any](bind ApiBind[A, B], name string, client func(conn *grpc.ClientConn) Z, rpc func(client Z, ctx context.Context, req C, options ...grpc.CallOption) (D, error)) *RpcRun[A, B, C, D, Z] {
|
||||
return &RpcRun[A, B, C, D, Z]{
|
||||
// NewRpc A: apiReq B: apiResp C: rpcReq D: rpcResp Z: rpcClient (group.GroupClient)
|
||||
func NewRpc[A, B any, C, D any, Z any](bind ApiBind[A, B], client func(conn *grpc.ClientConn) Z, rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) *Rpc[A, B, C, D, Z] {
|
||||
return &Rpc[A, B, C, D, Z]{
|
||||
bind: bind,
|
||||
name: name,
|
||||
client: client,
|
||||
rpc: rpc,
|
||||
}
|
||||
}
|
||||
|
||||
type RpcRun[A, B any, C, D any, Z any] struct {
|
||||
type Rpc[A, B any, C, D any, Z any] struct {
|
||||
bind ApiBind[A, B]
|
||||
name string
|
||||
client func(conn *grpc.ClientConn) Z
|
||||
rpc func(client Z, ctx context.Context, req C, options ...grpc.CallOption) (D, error)
|
||||
before func(apiReq *A, rpcReq C, bind func() error) error
|
||||
after func(rpcResp D, apiResp *B, bind func() error) error
|
||||
rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)
|
||||
before func(apiReq *A, rpcReq *C, bind func() error) error
|
||||
after func(rpcResp *D, apiResp *B, bind func() error) error
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) Before(fn func(apiReq *A, rpcReq C, bind func() error) error) *RpcRun[A, B, C, D, Z] {
|
||||
func (a *Rpc[A, B, C, D, Z]) Name(name string) *Rpc[A, B, C, D, Z] {
|
||||
a.name = name
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) *Rpc[A, B, C, D, Z] {
|
||||
a.before = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) After(fn func(rpcResp D, apiResp *B, bind func() error) error) *RpcRun[A, B, C, D, Z] {
|
||||
func (a *Rpc[A, B, C, D, Z]) After(fn func(rpcResp *D, apiResp *B, bind func() error) error) *Rpc[A, B, C, D, Z] {
|
||||
a.after = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) execute() (*B, error) {
|
||||
func (a *Rpc[A, B, C, D, Z]) defaultCopyReq(apiReq *A, rpcReq *C) error {
|
||||
common.CopyAny(apiReq, rpcReq)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) defaultCopyResp(rpcResp *D, apiResp *B) error {
|
||||
common.CopyAny(rpcResp, apiResp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) GetGrpcConn() (*grpc.ClientConn, error) {
|
||||
return nil, nil // todo
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) execute() (*B, error) {
|
||||
var apiReq A
|
||||
if err := a.bind.Bind(&apiReq); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opID := a.bind.OperationID()
|
||||
userID, err := a.bind.OpUserID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opID := a.bind.OperationID()
|
||||
var rpcReq C // C type => *Struct
|
||||
rpcReq = reflect.New(reflect.TypeOf(rpcReq).Elem()).Interface().(C)
|
||||
|
||||
return nil, nil
|
||||
_, _ = opID, userID
|
||||
var rpcReq C
|
||||
if a.before == nil {
|
||||
err = a.defaultCopyReq(&apiReq, &rpcReq)
|
||||
} else {
|
||||
err = a.before(&apiReq, &rpcReq, func() error { return a.defaultCopyReq(&apiReq, &rpcReq) })
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := a.GetGrpcConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rpcResp, err := a.rpc(a.client(conn), a.bind.Context(), &rpcReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var apiResp B
|
||||
if a.after == nil {
|
||||
err = a.defaultCopyResp(rpcResp, &apiResp)
|
||||
} else {
|
||||
err = a.after(rpcResp, &apiResp, func() error { return a.defaultCopyResp(rpcResp, &apiResp) })
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResp, nil
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) Execute() {
|
||||
|
||||
}
|
||||
|
||||
func GetGrpcConn(name string) (*grpc.ClientConn, error) {
|
||||
return nil, errors.New("todo")
|
||||
func (a *Rpc[A, B, C, D, Z]) Call() {
|
||||
a.bind.Resp(a.execute())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user