-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
52 lines (45 loc) · 1.35 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Package generic contains a gRPC based generic client.
package generic
import (
"context"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/generic/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
)
// client implements GenericServiceClient.
type client struct {
name string
conn rpc.ClientConn
client pb.GenericServiceClient
logger golog.Logger
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) Generic {
c := pb.NewGenericServiceClient(conn)
return &client{
name: name,
conn: conn,
client: c,
logger: logger,
}
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return DoFromConnection(ctx, c.conn, c.name, cmd)
}
// DoFromConnection is a helper to allow Do() calls from other component clients.
func DoFromConnection(ctx context.Context, conn rpc.ClientConn, name string, cmd map[string]interface{}) (map[string]interface{}, error) {
gclient := pb.NewGenericServiceClient(conn)
command, err := protoutils.StructToStructPb(cmd)
if err != nil {
return nil, err
}
resp, err := gclient.DoCommand(ctx, &pb.DoCommandRequest{
Name: name,
Command: command,
})
if err != nil {
return nil, err
}
return resp.Result.AsMap(), nil
}