Skip to content

Commit

Permalink
backend: commands-api: fix client URL usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rnovatorov committed Sep 6, 2023
1 parent 95f9d68 commit 9e43b08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 14 additions & 5 deletions pkg/commandsapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commandsapi
import (
"context"
"errors"
"fmt"
"net/http"
"time"

Expand All @@ -16,11 +17,13 @@ type Client interface {
}

type client struct {
apiURL string
token string
timeout time.Duration
}

type ClientParams struct {
APIURL string
Token string
Timeout time.Duration
}
Expand All @@ -33,6 +36,7 @@ func NewClient(p ClientParams) Client {
}

return &client{
apiURL: p.APIURL,
token: p.Token,
timeout: p.Timeout,
}
Expand All @@ -56,7 +60,12 @@ type CommandResponse struct {
}

func (c *client) Execute(ctx context.Context, p ExecuteParams) (CommandResponse, error) {
resp, err := c.enapterHTTP(p.User).Commands.Execute(ctx, enapterhttp.CommandQuery{
enapterHTTPClient, err := c.newEnapterHTTPClient(p.User)
if err != nil {
return CommandResponse{}, fmt.Errorf("new Enapter HTTP client: %w", err)
}

resp, err := enapterHTTPClient.Commands.Execute(ctx, enapterhttp.CommandQuery{
DeviceID: p.Request.DeviceID,
HardwareID: p.Request.HardwareID,
CommandName: p.Request.CommandName,
Expand All @@ -66,7 +75,7 @@ func (c *client) Execute(ctx context.Context, p ExecuteParams) (CommandResponse,
if respErr := (enapterhttp.ResponseError{}); errors.As(err, &respErr) {
return CommandResponse{}, c.respErrorToMultiError(respErr)
}
return CommandResponse{}, err
return CommandResponse{}, fmt.Errorf("do: %w", err)
}

return CommandResponse{
Expand Down Expand Up @@ -96,7 +105,7 @@ func (c *client) respErrorToMultiError(respErr enapterhttp.ResponseError) error
return multiErr
}

func (c *client) enapterHTTP(user string) *enapterhttp.Client {
func (c *client) newEnapterHTTPClient(user string) (*enapterhttp.Client, error) {
transport := http.DefaultTransport

if c.token != "" {
Expand All @@ -107,8 +116,8 @@ func (c *client) enapterHTTP(user string) *enapterhttp.Client {
transport = enapterhttp.NewAuthUserTransport(transport, user)
}

return enapterhttp.NewClient(&http.Client{
return enapterhttp.NewClientWithURL(&http.Client{
Timeout: c.timeout,
Transport: transport,
})
}, c.apiURL)
}
3 changes: 2 additions & 1 deletion pkg/plugin/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func newDataSource(logger hclog.Logger, settings backend.DataSourceInstanceSetti
}

commandsAPIClient := commandsapi.NewClient(commandsapi.ClientParams{
Token: apiToken,
APIURL: apiURL,
Token: apiToken,
})

queryDataHandler := handlers.NewQueryData(
Expand Down

0 comments on commit 9e43b08

Please sign in to comment.