-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
57 lines (50 loc) · 1.29 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
53
54
55
56
57
// Package generic contains a gRPC based generic client.
package generic
import (
"context"
commonpb "go.viam.com/api/common/v1"
genericpb "go.viam.com/api/component/generic/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
// client implements GenericServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client genericpb.GenericServiceClient
logger logging.Logger
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (resource.Resource, error) {
c := genericpb.NewGenericServiceClient(conn)
return &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: c,
logger: logger,
}, nil
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
command, err := protoutils.StructToStructPb(cmd)
if err != nil {
return nil, err
}
resp, err := c.client.DoCommand(ctx, &commonpb.DoCommandRequest{
Name: c.name,
Command: command,
})
if err != nil {
return nil, err
}
return resp.Result.AsMap(), nil
}