Skip to content

Commit

Permalink
Adding support for array query params
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jul 9, 2024
1 parent ed148f8 commit 2aa7a03
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httputil"
"net/url"
"path"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -116,17 +117,34 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) {

q := req.URL.Query()

addToQuery := func(key string, v any) error {
stringVal, err := util.GetStringFromInterface(v)
if err != nil {
return fmt.Errorf("error GetStringFromInterface: %s", err)
}
addToQuery := func(key string, val any) error {
var vs []any

refval := reflect.ValueOf(val)

if stringVal == "" {
return nil
switch refval.Kind() {
case reflect.Array, reflect.Slice:
l := refval.Len()
vs = make([]any, l)
for i := 0; i < l; i++ {
vs[i] = refval.Index(i).Interface()
}
default:
vs = []any{val}
}

q.Add(key, stringVal)
for _, v := range vs {
stringVal, err := util.GetStringFromInterface(v)
if err != nil {
return fmt.Errorf("error GetStringFromInterface: %s", err)
}

if stringVal == "" {
return nil
}

q.Add(key, stringVal)
}

return nil
}
Expand Down

0 comments on commit 2aa7a03

Please sign in to comment.