options.go 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package fetch
  2. import "net/http"
  3. type (
  4. Options struct {
  5. Url string
  6. Method string
  7. Header map[string]string
  8. Params map[string]string
  9. Data any
  10. Human bool
  11. }
  12. Option func(o *Options)
  13. )
  14. func WithUrl(s string) Option {
  15. return func(o *Options) {
  16. o.Url = s
  17. }
  18. }
  19. func WithMethod(s string) Option {
  20. return func(o *Options) {
  21. o.Method = s
  22. }
  23. }
  24. func WithHeader(h map[string]string) Option {
  25. return func(o *Options) {
  26. if o.Header == nil {
  27. o.Header = h
  28. }
  29. for k, v := range o.Header {
  30. o.Header[k] = v
  31. }
  32. }
  33. }
  34. func WithHuman() Option {
  35. return func(o *Options) {
  36. o.Human = true
  37. }
  38. }
  39. func WithParams(h map[string]string) Option {
  40. return func(o *Options) {
  41. o.Params = h
  42. }
  43. }
  44. func WithData(v any) Option {
  45. return func(o *Options) {
  46. o.Data = v
  47. }
  48. }
  49. func newOptions() *Options {
  50. return &Options{
  51. Method: http.MethodGet,
  52. }
  53. }