diff options
Diffstat (limited to 'http_params_converter.go')
| -rw-r--r-- | http_params_converter.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/http_params_converter.go b/http_params_converter.go new file mode 100644 index 0000000..af7745b --- /dev/null +++ b/http_params_converter.go @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + +package gorankusu + +import libhttp "github.com/shuLhan/share/lib/http" + +// HTTPParamsConverter is a handler that will be called inside the Run handler +// to convert the Params values to type that will be send as request. +type HTTPParamsConverter func(target *HTTPTarget) (any, error) + +// DefaultParamsConverter define default function to convert +// [HTTPTarget.Params] to its equivalent parameters in HTTP, either as query +// in URL or as stream of bytes in body. +func DefaultParamsConverter() HTTPParamsConverter { + return func(target *HTTPTarget) (params any, err error) { + switch target.RequestType { + case libhttp.RequestTypeJSON: + params = target.Params.ToJSONObject() + case libhttp.RequestTypeMultipartForm: + params = target.Params.ToMultipartFormData() + default: + params = target.Params.ToURLValues() + } + return params, nil + } +} |
