-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
38 lines (35 loc) · 955 Bytes
/
do.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
package shelly
import (
"context"
"encoding/json"
"fmt"
"github.com/mongoose-os/mos/common/mgrpc"
"github.com/mongoose-os/mos/common/mgrpc/frame"
)
func Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
req RPCRequestBody,
resp any,
) (*frame.Response, error) {
args, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("marshalling shelly rpc request: %w", err)
}
command := &frame.Command{
Cmd: req.Method(),
Args: json.RawMessage(args),
}
rawResp, err := c.Call(ctx, "", command, credsCallback)
if err != nil {
return rawResp, fmt.Errorf("making shelly rpc request: %w", err)
}
if rawResp.Status != 0 {
return rawResp, &BadStatusWithMessageError{Status: ShellyErrorCode(rawResp.Status), Msg: rawResp.StatusMsg}
}
if err := json.Unmarshal(rawResp.Response, resp); err != nil {
return rawResp, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return rawResp, err
}