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: add dynamic form and query param support #73

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Changes from all commits
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
67 changes: 53 additions & 14 deletions https/callBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"strconv"
"strings"
"time"

"github.com/apimatic/go-core-runtime/utilities"
)

// Constants for commonly used HTTP headers and content types.
Expand Down Expand Up @@ -48,11 +46,14 @@ type CallBuilder interface {
Header(name string, value any)
CombineHeaders(headersToMerge map[string]string)
QueryParam(name string, value any)
QueryParams(parameters map[string]any)
QueryParamWithArraySerializationOption(name string, value any, option ArraySerializationOption)
QueryParamsWithArraySerializationOption(parameters map[string]any, option ArraySerializationOption)
validateQueryParams() error
QueryParams(parameters map[string]any)
FormParam(name string, value any)
FormParams(parameters map[string]any)
FormParamWithArraySerializationOption(name string, value any, opt ArraySerializationOption)
FormParamsWithArraySerializationOption(parameters map[string]any, opt ArraySerializationOption)
validateFormParams() error
FormData(fields FormParams)
validateFormData() error
Expand Down Expand Up @@ -274,11 +275,19 @@ func (cb *defaultCallBuilder) QueryParam(
name string,
value any,
) {
cb.queryParams.add(formParam{name, value, nil, cb.arraySerializationOption})
cb.QueryParamWithArraySerializationOption(name, value, cb.arraySerializationOption)
}

// QueryParams adds multiple query parameters to the API call.
// It takes the map as query parameter as arguments.
func (cb *defaultCallBuilder) QueryParams(
parameters map[string]any,
) {
cb.QueryParamsWithArraySerializationOption(parameters, cb.arraySerializationOption)
}

// QueryParamWithArraySerializationOption adds a query parameter to the API call.
// It takes the name and value of the query parameter as arguments.
// It takes the name, value and array serialization of the query parameter as arguments.
func (cb *defaultCallBuilder) QueryParamWithArraySerializationOption(
name string,
value any,
Expand All @@ -287,6 +296,20 @@ func (cb *defaultCallBuilder) QueryParamWithArraySerializationOption(
cb.queryParams.add(formParam{name, value, nil, option})
}

// QueryParamsWithArraySerializationOption adds a query parameter to the API call.
// It takes the map and array serialization of the query parameter as arguments.
func (cb *defaultCallBuilder) QueryParamsWithArraySerializationOption(
parameters map[string]any,
option ArraySerializationOption,
) {
if parameters == nil {
return
}
for key, value := range parameters {
cb.QueryParamWithArraySerializationOption(key, value, option)
}
}

// validateQueryParams validates the query parameters in the CallBuilder.
func (cb *defaultCallBuilder) validateQueryParams() error {
if len(cb.queryParams) != 0 {
Expand All @@ -301,23 +324,25 @@ func (cb *defaultCallBuilder) validateQueryParams() error {
return nil
}

// QueryParams sets multiple query parameters for the API call.
// It takes a map of string keys and any values representing the query parameters.
func (cb *defaultCallBuilder) QueryParams(parameters map[string]any) {
cb.query = utilities.PrepareQueryParams(cb.query, parameters)
}

// FormParam adds a form parameter to the API call.
// It takes the name and value of the form parameter as arguments.
func (cb *defaultCallBuilder) FormParam(
name string,
value any,
) {
cb.formParams.add(formParam{name, value, nil, cb.arraySerializationOption})
cb.FormParamWithArraySerializationOption(name, value, cb.arraySerializationOption)
}

// FormParamWithArraySerializationOption adds a form parameter to the API call.
// It takes the name and value of the form parameter as arguments.
// FormParams adds multiple form parameters to the API call.
// It takes map as form parameters as argument.
func (cb *defaultCallBuilder) FormParams(
parameters map[string]any,
) {
cb.FormParamsWithArraySerializationOption(parameters, cb.arraySerializationOption)
}

// FormParamWithArraySerializationOption adds a form parameter with customized serialization to the API call.
// It takes the name, value and array serialization option of the form parameter as arguments.
func (cb *defaultCallBuilder) FormParamWithArraySerializationOption(
name string,
value any,
Expand All @@ -326,6 +351,20 @@ func (cb *defaultCallBuilder) FormParamWithArraySerializationOption(
cb.formParams.add(formParam{name, value, nil, option})
}

// FormParamsWithArraySerializationOption adds form parameters to the API call.
// It takes map and array serialization option of the form parameters as arguments.
func (cb *defaultCallBuilder) FormParamsWithArraySerializationOption(
parameters map[string]any,
option ArraySerializationOption,
) {
if parameters == nil {
return
}
for key, value := range parameters {
cb.FormParamWithArraySerializationOption(key, value, option)
}
}

// validateFormParams validates the form parameters in the CallBuilder.
// Additionally, it sets the "Content-Type" header to "application/x-www-form-urlencoded" if not already set.
func (cb *defaultCallBuilder) validateFormParams() error {
Expand Down
Loading