minerva/gateway/pkg/grpc/v1/client.go

42 lines
882 B
Go

package v1
import (
"context"
"fmt"
"google.golang.org/grpc"
"minerva.bing89.com/gateway/pkg/minerva"
)
//Client 通用rpc client
type Client struct {
m *minerva.Minerva
conn grpc.ClientConnInterface
}
//NewClient
func NewClient(m *minerva.Minerva, opts ...grpc.DialOption)(*Client, error){
conn, err := grpc.Dial(m.Uri, opts...)
if err != nil {
return nil, err
}
c := Client{
m: m,
conn: conn,
}
return &c, nil
}
//InsecureClient
func NewInsecureClient(m *minerva.Minerva)(*Client, error){
return NewClient(m, grpc.WithInsecure())
}
//Call 调用Minerva并返回结果
func (c *Client)Call(req *minerva.Request, callOpts ...grpc.CallOption)(*minerva.Response, error){
resp := minerva.Response{}
method := fmt.Sprintf("/%s/%s", c.m.ModelName, c.m.CallName)
err := c.conn.Invoke(context.Background(), method, req, &resp, callOpts...)
return &resp, err
}