kos/util/fetch/options.go

63 lines
863 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package fetch
import "net/http"
type (
Options struct {
Url string
Method string
Header map[string]string
Params map[string]string
Data any
2023-04-26 15:37:35 +08:00
Human bool
2023-04-23 17:57:36 +08:00
}
Option func(o *Options)
)
func WithUrl(s string) Option {
return func(o *Options) {
o.Url = s
}
}
func WithMethod(s string) Option {
return func(o *Options) {
o.Method = s
}
}
func WithHeader(h map[string]string) Option {
return func(o *Options) {
2023-04-26 15:37:35 +08:00
if o.Header == nil {
o.Header = h
}
for k, v := range o.Header {
o.Header[k] = v
}
}
}
func WithHuman() Option {
return func(o *Options) {
o.Human = true
2023-04-23 17:57:36 +08:00
}
}
func WithParams(h map[string]string) Option {
return func(o *Options) {
o.Params = h
}
}
func WithData(v any) Option {
return func(o *Options) {
o.Data = v
}
}
func newOptions() *Options {
return &Options{
Method: http.MethodGet,
}
}