github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/test/api.go (about) 1 package test 2 3 import ( 4 "github.com/volts-dev/volts/client" 5 "github.com/volts-dev/volts/codec" 6 "github.com/volts-dev/volts/logger" 7 ) 8 9 type ( 10 ArithCli struct { 11 client.IClient 12 } 13 ) 14 15 func NewArithCli(cli client.IClient) *ArithCli { 16 return &ArithCli{ 17 IClient: cli, 18 } 19 } 20 21 func (self ArithCli) Mul(arg *Args) (result *Reply, err error) { 22 service := "Arith.Mul" 23 endpoint := "Test.Endpoint" 24 address := "127.0.0.1:35999" 25 26 cli := client.NewRpcClient() 27 req, _ := cli.NewRequest(service, endpoint, arg, 28 client.WithCodec(codec.MsgPack), // 默认传输编码 29 ) 30 //req.SetContentType(codec.MsgPack) // 也可以在Req中制定传输编码 31 //req.Write(arg) 32 33 // test calling remote address 34 rsp, err := cli.Call(req, client.WithAddress(address)) 35 if err != nil { 36 logger.Err("call with address error:", err) 37 return nil, err 38 } 39 40 rsp.Body().Decode(&result) 41 return result, nil 42 }