-
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 #42 from Enapter/ym/panel-2.0
Enapter commands panel v2
- Loading branch information
Showing
63 changed files
with
3,442 additions
and
739 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ RUN --mount=type=cache,target=node_modules npm run build | |
FROM golang:1.20-alpine AS backend | ||
|
||
RUN apk add --no-cache --virtual .build-deps \ | ||
binutils-gold \ | ||
git \ | ||
build-base \ | ||
&& go install github.com/magefile/[email protected] \ | ||
|
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 assetsapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
enapterhttp "github.com/Enapter/http-api-go-client/pkg/client" | ||
|
||
"github.com/Enapter/grafana-plugins/pkg/httperr" | ||
) | ||
|
||
type ( | ||
Device = enapterhttp.Device | ||
ExpandDeviceParams = enapterhttp.ExpandDeviceParams | ||
) | ||
|
||
type Client interface { | ||
DeviceByID(context.Context, DeviceByIDParams) (*Device, error) | ||
} | ||
|
||
type client struct { | ||
apiURL string | ||
token string | ||
timeout time.Duration | ||
} | ||
|
||
type ClientParams struct { | ||
APIURL string | ||
Token string | ||
Timeout time.Duration | ||
} | ||
|
||
const DefaultTimeout = 15 * time.Second | ||
|
||
func NewClient(p ClientParams) Client { | ||
if p.Timeout == 0 { | ||
p.Timeout = DefaultTimeout | ||
} | ||
|
||
return &client{ | ||
apiURL: p.APIURL, | ||
token: p.Token, | ||
timeout: p.Timeout, | ||
} | ||
} | ||
|
||
type DeviceByIDParams struct { | ||
User string | ||
DeviceID string | ||
Expand ExpandDeviceParams | ||
} | ||
|
||
func (c *client) DeviceByID( | ||
ctx context.Context, p DeviceByIDParams, | ||
) (*Device, error) { | ||
enapterHTTPClient, err := c.newEnapterHTTPClient(p.User) | ||
if err != nil { | ||
return nil, fmt.Errorf("new Enapter HTTP client: %w", err) | ||
} | ||
|
||
resp, err := enapterHTTPClient.Assets.DeviceByID(ctx, enapterhttp.DeviceByIDQuery{ | ||
ID: p.DeviceID, | ||
Expand: p.Expand, | ||
}) | ||
if err != nil { | ||
if respErr := (enapterhttp.ResponseError{}); errors.As(err, &respErr) { | ||
return nil, c.respErrorToMultiError(respErr) | ||
} | ||
return nil, fmt.Errorf("do: %w", err) | ||
} | ||
|
||
return &resp.Device, 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) newEnapterHTTPClient(user string) (*enapterhttp.Client, error) { | ||
transport := http.DefaultTransport | ||
|
||
if c.token != "" { | ||
transport = enapterhttp.NewAuthTokenTransport(transport, c.token) | ||
} | ||
|
||
if user != "" { | ||
transport = enapterhttp.NewAuthUserTransport(transport, user) | ||
} | ||
|
||
return enapterhttp.NewClientWithURL(&http.Client{ | ||
Timeout: c.timeout, | ||
Transport: transport, | ||
}, c.apiURL) | ||
} |
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
53 changes: 53 additions & 0 deletions
53
enapter-api-datasource/pkg/plugin/internal/handlers/mock_assetsapi_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,53 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/Enapter/grafana-plugins/pkg/assetsapi" | ||
) | ||
|
||
var _ assetsapi.Client = (*MockAssetsAPIClient)(nil) | ||
|
||
type MockAssetsAPIClient struct { | ||
suite *suite.Suite | ||
deviceByIDHandler func(assetsapi.DeviceByIDParams) (*assetsapi.Device, error) | ||
} | ||
|
||
func NewMockAssetsAPIClient(s *suite.Suite) *MockAssetsAPIClient { | ||
c := new(MockAssetsAPIClient) | ||
c.suite = s | ||
c.deviceByIDHandler = c.unexpectedCall | ||
return c | ||
} | ||
|
||
func (c *MockAssetsAPIClient) ExpectDeviceByIDAndReturn( | ||
wantP assetsapi.DeviceByIDParams, | ||
device *assetsapi.Device, err error, | ||
) { | ||
c.deviceByIDHandler = func(haveP assetsapi.DeviceByIDParams) ( | ||
*assetsapi.Device, error, | ||
) { | ||
defer func() { c.deviceByIDHandler = c.unexpectedCall }() | ||
c.suite.Require().Equal(wantP, haveP) | ||
return device, err | ||
} | ||
} | ||
|
||
func (c *MockAssetsAPIClient) DeviceByID( | ||
_ context.Context, p assetsapi.DeviceByIDParams, | ||
) (*assetsapi.Device, error) { | ||
return c.deviceByIDHandler(p) | ||
} | ||
|
||
func (c *MockAssetsAPIClient) unexpectedCall(assetsapi.DeviceByIDParams) ( | ||
*assetsapi.Device, error, | ||
) { | ||
c.suite.Require().FailNow("unexpected call") | ||
//nolint: nilnil // unreachable | ||
return nil, nil | ||
} | ||
|
||
func (c *MockAssetsAPIClient) Ready(context.Context) error { return nil } | ||
func (c *MockAssetsAPIClient) Close() {} |
Oops, something went wrong.