Skip to content

Commit

Permalink
fix(types): Correct field order breaking requests
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Dec 23, 2024
1 parent 1470f2c commit 0861908
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
33 changes: 24 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"math/rand/v2"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -189,17 +190,17 @@ func (c *client) apiRequest(ctx context.Context, actions []action) (map[string]r
nonce := generateNonce()
authKey := c.generateAuthKey(nonce, requestID)

slog.DebugContext(ctx, "request",
slog.Int("session_id", c.sessionID),
slog.Int("request_id", requestID),
)

u := "http://" + c.host + apiEndpoint

sessID := c.sessionID
if sessID == -1 {
sessID = 0
}

payload := map[string]requestBody{
"request": {
ID: requestID,
SessionID: c.sessionID,
SessionID: strconv.Itoa(sessID),
Priority: false,
Actions: actions,
Cnonce: nonce,
Expand All @@ -216,12 +217,21 @@ func (c *client) apiRequest(ctx context.Context, actions []action) (map[string]r
values := url.Values{}
values.Set("req", string(body))

slog.DebugContext(ctx, "request",
slog.Int("session_id", sessID),
slog.Int("request_id", requestID),
slog.String("host", c.host),
slog.String("url", u),
slog.String("values", values.Encode()),
)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, strings.NewReader(values.Encode()))
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")

resp, err := c.hc.Do(req)
if err != nil {
Expand All @@ -234,6 +244,8 @@ func (c *client) apiRequest(ctx context.Context, actions []action) (map[string]r
return nil, fmt.Errorf("readAll: %w", err)
}

span.SetAttributes(attribute.String("respBody", string(respBody)))

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status %d: %s", resp.StatusCode, respBody)
}
Expand Down Expand Up @@ -280,7 +292,7 @@ func loginAction(username string) action {
Method: "logIn",
Parameters: map[string]any{
"user": username,
"persistent": true,
"persistent": false,
"session-options": sessionOptions{
Nss: []nss{
{
Expand All @@ -292,6 +304,7 @@ func loginAction(username string) action {
ContextFlags: contextFlags{
GetContentName: true,
LocalTime: true,
NoDefault: false,
},
CapabilityDepth: 2,
CapabilityFlags: capabilityFlags{
Expand Down Expand Up @@ -415,7 +428,9 @@ func (c *client) Logout(ctx context.Context) error {
// GetValue - port of python's get_value_by_xpath
// Retrieve raw value from router using XPath.
func (c *client) GetValue(ctx context.Context, xpath string) (*ValueResponse, error) {
ctx, span := tracer.Start(ctx, "SagemcomClient.GetValue")
ctx, span := tracer.Start(ctx, "SagemcomClient.GetValue", trace.WithAttributes(
attribute.String("xpath", xpath),
))
defer span.End()

actions := []action{
Expand Down Expand Up @@ -511,7 +526,7 @@ func (c *client) GetValues(ctx context.Context, xpaths map[string]string) (map[s
XPath: url.PathEscape(xpath),
})

fmt.Printf("actions: %#v\n", actions)
slog.Debug("actions", slog.Any("actions", actions))
}

_, err := c.apiRequestWithRefresh(ctx, actions)
Expand Down
33 changes: 26 additions & 7 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,36 @@ import (
"time"
)

// requestBody - represents the body of a request to the API
//
// note: order of fields is significant - the API will reject requests if the
// order is not maintained
//
//nolint:govet
type requestBody struct {
AuthKey string `json:"auth-key"`
Actions []action `json:"actions"`
ID int `json:"id"`
SessionID int `json:"session-id"`
Cnonce int `json:"cnonce"`
SessionID string `json:"session-id"`
Priority bool `json:"priority"`
Actions []action `json:"actions"`
Cnonce int `json:"cnonce"`
AuthKey string `json:"auth-key"`
}

// action - represents a single action to be performed
//
// note: order of fields is significant - the API will reject requests if the
// order is not maintained
//
//nolint:govet
type action struct {
Parameters map[string]any `json:"parameters,omitempty"`
ID int `json:"id"`
Method string `json:"method"`
Parameters map[string]any `json:"parameters,omitempty"`
XPath string `json:"xpath,omitempty"`
ID int `json:"id,omitempty"`
}

type sessionOptions struct {
Language string `json:"language"`
Language string `json:"language,omitempty"` // seems not to be getting sent anymore?
TimeFormat string `json:"time-format"`
WriteOnlyString string `json:"write-only-string"`
UndefinedWriteOnlyString string `json:"undefined-write-only-string"`
Expand All @@ -44,6 +56,7 @@ type nss struct {
type contextFlags struct {
GetContentName bool `json:"get-content-name"`
LocalTime bool `json:"local-time"`
NoDefault bool `json:"no-default"`
}

type capabilityFlags struct {
Expand All @@ -53,6 +66,12 @@ type capabilityFlags struct {
Description bool `json:"description"`
}

type compatibilityFlags struct {

Check failure on line 69 in client/types.go

View workflow job for this annotation

GitHub Actions / lint

type `compatibilityFlags` is unused (unused)
Flags bool `json:"flags"`
DefaultValue bool `json:"default-value"`
Type bool `json:"type"`
}

type responseBody struct {
Error *xmoError `json:"error"`
Actions []actionResp `json:"actions"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
go.opentelemetry.io/otel/sdk/log v0.9.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.33.0 // indirect
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1
go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down

0 comments on commit 0861908

Please sign in to comment.