-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Enapter/rnovatorov/dev
Handle Command Requests
- Loading branch information
Showing
13 changed files
with
556 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package commandsapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
enapterhttp "github.com/Enapter/http-api-go-client/pkg/client" | ||
|
||
"github.com/Enapter/telemetry-grafana-datasource-plugin/pkg/httperr" | ||
) | ||
|
||
type Client interface { | ||
Execute(ctx context.Context, p ExecuteParams) (CommandResponse, error) | ||
} | ||
|
||
type client struct { | ||
token string | ||
timeout time.Duration | ||
} | ||
|
||
type ClientParams struct { | ||
Token string | ||
Timeout time.Duration | ||
} | ||
|
||
const DefaultTimeout = 15 * time.Second | ||
|
||
func NewClient(p ClientParams) Client { | ||
if p.Timeout == 0 { | ||
p.Timeout = DefaultTimeout | ||
} | ||
|
||
return &client{ | ||
token: p.Token, | ||
timeout: p.Timeout, | ||
} | ||
} | ||
|
||
type ExecuteParams struct { | ||
User string | ||
Request CommandRequest | ||
} | ||
|
||
type CommandRequest struct { | ||
CommandName string | ||
CommandArgs map[string]interface{} | ||
DeviceID string | ||
HardwareID string | ||
} | ||
|
||
type CommandResponse struct { | ||
State string | ||
Payload map[string]interface{} | ||
} | ||
|
||
func (c *client) Execute(ctx context.Context, p ExecuteParams) (CommandResponse, error) { | ||
resp, err := c.enapterHTTP(p.User).Commands.Execute(ctx, enapterhttp.CommandQuery{ | ||
DeviceID: p.Request.DeviceID, | ||
HardwareID: p.Request.HardwareID, | ||
CommandName: p.Request.CommandName, | ||
Arguments: p.Request.CommandArgs, | ||
}) | ||
if err != nil { | ||
if respErr := (enapterhttp.ResponseError{}); errors.As(err, &respErr) { | ||
return CommandResponse{}, c.respErrorToMultiError(respErr) | ||
} | ||
return CommandResponse{}, err | ||
} | ||
|
||
return CommandResponse{ | ||
State: string(resp.State), | ||
Payload: resp.Payload, | ||
}, nil | ||
} | ||
|
||
func (c *client) respErrorToMultiError(respErr enapterhttp.ResponseError) error { | ||
if len(respErr.Errors) == 0 { | ||
return respErr | ||
} | ||
|
||
multiErr := new(httperr.MultiError) | ||
|
||
for _, e := range respErr.Errors { | ||
if len(e.Code) == 0 { | ||
e.Code = "<empty>" | ||
} | ||
multiErr.Errors = append(multiErr.Errors, httperr.Error{ | ||
Code: e.Code, | ||
Message: e.Message, | ||
Details: e.Details, | ||
}) | ||
} | ||
|
||
return multiErr | ||
} | ||
|
||
func (c *client) enapterHTTP(user string) *enapterhttp.Client { | ||
transport := http.DefaultTransport | ||
|
||
if c.token != "" { | ||
transport = enapterhttp.NewAuthTokenTransport(transport, c.token) | ||
} | ||
|
||
if user != "" { | ||
transport = enapterhttp.NewAuthUserTransport(transport, user) | ||
} | ||
|
||
return enapterhttp.NewClient(&http.Client{ | ||
Timeout: c.timeout, | ||
Transport: transport, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
pkg/plugin/internal/handlers/mock_commandsapi_client_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/Enapter/telemetry-grafana-datasource-plugin/pkg/commandsapi" | ||
) | ||
|
||
var _ commandsapi.Client = (*MockCommandsAPIClient)(nil) | ||
|
||
type MockCommandsAPIClient struct { | ||
suite *suite.Suite | ||
executeHandler func(commandsapi.ExecuteParams) ( | ||
commandsapi.CommandResponse, error) | ||
} | ||
|
||
func NewMockCommandsAPIClient(s *suite.Suite) *MockCommandsAPIClient { | ||
c := new(MockCommandsAPIClient) | ||
c.suite = s | ||
c.executeHandler = c.unexpectedCall | ||
return c | ||
} | ||
|
||
func (c *MockCommandsAPIClient) ExpectExecuteAndReturn( | ||
wantP commandsapi.ExecuteParams, | ||
cmdResp commandsapi.CommandResponse, err error, | ||
) { | ||
c.executeHandler = func(haveP commandsapi.ExecuteParams) ( | ||
commandsapi.CommandResponse, error, | ||
) { | ||
defer func() { c.executeHandler = c.unexpectedCall }() | ||
c.suite.Require().Equal(wantP, haveP) | ||
return cmdResp, err | ||
} | ||
} | ||
|
||
func (c *MockCommandsAPIClient) Execute( | ||
_ context.Context, p commandsapi.ExecuteParams, | ||
) (commandsapi.CommandResponse, error) { | ||
return c.executeHandler(p) | ||
} | ||
|
||
func (c *MockCommandsAPIClient) unexpectedCall(commandsapi.ExecuteParams) ( | ||
commandsapi.CommandResponse, error, | ||
) { | ||
c.suite.Require().FailNow("unexpected call") | ||
//nolint: nilnil // unreachable | ||
return commandsapi.CommandResponse{}, nil | ||
} | ||
|
||
func (c *MockCommandsAPIClient) Ready(context.Context) error { return nil } | ||
func (c *MockCommandsAPIClient) Close() {} |
Oops, something went wrong.