/
updateResource.go
49 lines (43 loc) · 1.07 KB
/
updateResource.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
package client
import (
"context"
"fmt"
"github.com/plgd-dev/cloud/grpc-gateway/pb"
codecOcf "github.com/plgd-dev/kit/codec/ocf"
)
// UpdateResource updates content in OCF-CBOR format.
func (c *Client) UpdateResource(
ctx context.Context,
deviceID string,
href string,
request interface{},
response interface{},
opts ...UpdateOption,
) error {
cfg := updateOptions{
codec: codecOcf.VNDOCFCBORCodec{},
}
for _, o := range opts {
cfg = o.applyOnUpdate(cfg)
}
data, err := cfg.codec.Encode(request)
if err != nil {
return err
}
r := pb.UpdateResourceValuesRequest{
ResourceId: &pb.ResourceId{
DeviceId: deviceID,
Href: href,
},
ResourceInterface: cfg.resourceInterface,
Content: &pb.Content{
Data: data,
ContentType: cfg.codec.ContentFormat().String(),
},
}
resp, err := c.gateway.UpdateResourcesValues(ctx, &r)
if err != nil {
return fmt.Errorf("cannot update resource /%v/%v: %w", deviceID, href, err)
}
return DecodeContentWithCodec(cfg.codec, resp.GetContent().GetContentType(), resp.GetContent().GetData(), response)
}