Skip to content

Commit

Permalink
Moving addToQuery to dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jul 9, 2024
1 parent 359fd79 commit ed148f8
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ 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)
}

if stringVal == "" {
return nil
}

q.Add(key, stringVal)

return nil
}

for queryName, datastoreKey := range request.QueryParamsFromStore {
skipOnError := false
if len(datastoreKey) > 0 && datastoreKey[0] == '?' {
Expand All @@ -135,23 +150,17 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) {
return nil, fmt.Errorf("could not get '%s' from Datastore: %s", datastoreKey, err)
}

stringVal, err := util.GetStringFromInterface(queryParamInterface)
err = addToQuery(queryName, queryParamInterface)
if err != nil {
return req, fmt.Errorf("error GetStringFromInterface: %s", err)
}

if stringVal == "" {
continue
return req, err
}
q.Add(queryName, stringVal)
}

for key, val := range request.QueryParams {
stringVal, err := util.GetStringFromInterface(val)
err = addToQuery(key, val)
if err != nil {
return req, fmt.Errorf("error GetStringFromInterface: %s", err)
return req, err
}
q.Set(key, stringVal)
}

req.URL.RawQuery = q.Encode()
Expand Down

0 comments on commit ed148f8

Please sign in to comment.