Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle url limit in rest #3186

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/tools/rest/href_builder.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package rest

import (
"fmt"
"slices"
"strconv"
"strings"
)

const URLMaxLimit = 8 * 1024

type HrefBuilder struct {
apiPath string
fields []string
Expand Down Expand Up @@ -76,6 +79,10 @@ func (b *HrefBuilder) Build() string {

href.WriteString("?return_records=true")

if len(strings.Join(b.fields, ",")) > URLMaxLimit {
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("converting to * due to URL max limit")
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
b.fields = []string{"*"}
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
}
// Sort fields so that the href is deterministic
slices.Sort(b.fields)

Expand Down
45 changes: 45 additions & 0 deletions cmd/tools/rest/href_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package rest

import (
"strconv"
"testing"
)

func TestBuild(t *testing.T) {

testQuery := "storage/volumes"
testFields := []string{"name", "svm"}
testFilter := []string{""}
testReturnTimeout := 10
isIgnoreUnknownFieldsEnabled := true
expectedHrefTest1 := "api/storage/volumes?return_records=true&fields=name,svm&return_timeout=10&ignore_unknown_fields=true"
hrefTest1 := NewHrefBuilder().
APIPath(testQuery).
Fields(testFields).
Filter(testFilter).
ReturnTimeout(&testReturnTimeout).
IsIgnoreUnknownFieldsEnabled(isIgnoreUnknownFieldsEnabled).
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
Build()

if hrefTest1 != expectedHrefTest1 {
t.Errorf("hrefTest1 should be %s but got %s", expectedHrefTest1, hrefTest1)
}

testFields = make([]string, 0)
for i := range 100000000 {
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
testFields = append(testFields, "Test"+strconv.Itoa(i))
}

expectedHrefTest2 := "api/storage/volumes?return_records=true&fields=*&return_timeout=10&ignore_unknown_fields=true"
hrefTest2 := NewHrefBuilder().
APIPath(testQuery).
Fields(testFields).
Filter(testFilter).
ReturnTimeout(&testReturnTimeout).
IsIgnoreUnknownFieldsEnabled(isIgnoreUnknownFieldsEnabled).
Hardikl marked this conversation as resolved.
Show resolved Hide resolved
Build()

if hrefTest2 != expectedHrefTest2 {
t.Errorf("hrefTest2 should be %s but got %s", expectedHrefTest2, hrefTest2)
}
}
Loading