Skip to content

Commit

Permalink
properly encoding query params (omg, duh)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Oct 7, 2020
1 parent ff411ca commit 9da2018
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
41 changes: 12 additions & 29 deletions api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

const (
uriPathParameterSearchFormat = "{%s}"
uriQueryParameterPrefixFormat = "%s?"
uriQueryParameterFormat = "%s?%s"
uriQueryParameterAddNoValueFormat = "%s%s&"
uriQueryParameterAddValueFormat = "%s%s=%s&"
uriQueryParameterCutSet = "&"
Expand All @@ -38,7 +38,7 @@ type APIRequest struct {
method string
uri string

queryParameters map[string][]string
queryParameters url.Values
pathParameters map[string]string
headers url.Values
cookies []*http.Cookie
Expand All @@ -53,7 +53,7 @@ func NewAPIRequest(method, requestURL string) *APIRequest {
id: atomic.AddUint64(&apiRequestID, 1),
method: method,
uri: requestURL,
queryParameters: make(map[string][]string),
queryParameters: make(url.Values),
pathParameters: make(map[string]string),
headers: make(url.Values),
cookies: make([]*http.Cookie, 0),
Expand Down Expand Up @@ -131,25 +131,18 @@ func (r *APIRequest) Cookies() []*http.Cookie {
}

// AddQueryParameter will add a value to the specified param
func (r *APIRequest) AddQueryParameter(param string, value []string) {
if _, ok := r.queryParameters[param]; !ok {
r.queryParameters[param] = make([]string, 0)
}
r.queryParameters[param] = append(r.queryParameters[param], value...)
func (r *APIRequest) AddQueryParameter(param string, value string) {
r.queryParameters.Add(param, value)
}

// SetQueryParameter will set a query param to a specific value, overriding any previously set value
func (r *APIRequest) SetQueryParameter(param string, value []string) {
delete(r.queryParameters, param)
r.AddQueryParameter(param, value)
func (r *APIRequest) SetQueryParameter(param string, value string) {
r.queryParameters.Set(param, value)
}

// SetQueryParameters will override any / all existing query parameters
func (r *APIRequest) SetQueryParameters(params map[string][]string) {
r.queryParameters = make(map[string][]string)
for k, v := range params {
r.AddQueryParameter(k, v)
}
func (r *APIRequest) SetQueryParameters(params url.Values) {
r.queryParameters = params
}

// RemoveQueryParameter will attempt to delete all values for a specific query parameter from this request.
Expand All @@ -158,7 +151,7 @@ func (r *APIRequest) RemoveQueryParameter(param string) {
}

// QueryParameters will return all values of currently set query parameters
func (r *APIRequest) QueryParameters() map[string][]string {
func (r *APIRequest) QueryParameters() url.Values {
return r.queryParameters
}

Expand Down Expand Up @@ -302,22 +295,12 @@ func (r *APIRequest) CompiledURI() string {
uri := r.uri
if len(pathParams) > 0 {
for k, v := range pathParams {
uri = strings.Replace(uri, fmt.Sprintf(uriPathParameterSearchFormat, k), v, 1)
uri = strings.Replace(uri, fmt.Sprintf(uriPathParameterSearchFormat, k), url.PathEscape(v), 1)
}
}
// TODO: could probably be made more efficient.
if len(queryParams) > 0 {
uri = fmt.Sprintf(uriQueryParameterPrefixFormat, uri)
for param, values := range queryParams {
for _, value := range values {
if value == "" {
uri = fmt.Sprintf(uriQueryParameterAddNoValueFormat, uri, param)
} else {
uri = fmt.Sprintf(uriQueryParameterAddValueFormat, uri, param, value)
}
}
}
uri = strings.TrimRight(uri, uriQueryParameterCutSet)
uri = fmt.Sprintf(uriQueryParameterFormat, uri, queryParams.Encode())
}
return uri
}
Expand Down
4 changes: 2 additions & 2 deletions mutators.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func buildQueryMutator(k string, v, def interface{}, override, requiredValued bo
return nil
}
if override {
r.SetQueryParameter(k, []string{value})
r.SetQueryParameter(k, value)
} else {
r.AddQueryParameter(k, []string{value})
r.AddQueryParameter(k, value)
}
return nil
}
Expand Down

0 comments on commit 9da2018

Please sign in to comment.