github.com/MontFerret/ferret@v0.18.0/pkg/drivers/options.go (about)

     1  package drivers
     2  
     3  type (
     4  	globalOptions struct {
     5  		defaultDriver string
     6  	}
     7  
     8  	GlobalOption func(drv Driver, opts *globalOptions)
     9  
    10  	Options struct {
    11  		Name      string
    12  		Proxy     string
    13  		UserAgent string
    14  		Headers   *HTTPHeaders
    15  		Cookies   *HTTPCookies
    16  	}
    17  
    18  	Option func(opts *Options)
    19  )
    20  
    21  func AsDefault() GlobalOption {
    22  	return func(drv Driver, opts *globalOptions) {
    23  		opts.defaultDriver = drv.Name()
    24  	}
    25  }
    26  
    27  func WithProxy(address string) Option {
    28  	return func(opts *Options) {
    29  		opts.Proxy = address
    30  	}
    31  }
    32  
    33  func WithUserAgent(value string) Option {
    34  	return func(opts *Options) {
    35  		opts.UserAgent = value
    36  	}
    37  }
    38  
    39  func WithCustomName(name string) Option {
    40  	return func(opts *Options) {
    41  		opts.Name = name
    42  	}
    43  }
    44  
    45  func WithHeader(name string, value []string) Option {
    46  	return func(opts *Options) {
    47  		if opts.Headers == nil {
    48  			opts.Headers = NewHTTPHeaders()
    49  		}
    50  
    51  		opts.Headers.SetArr(name, value)
    52  	}
    53  }
    54  
    55  func WithHeaders(headers *HTTPHeaders) Option {
    56  	return func(opts *Options) {
    57  		if opts.Headers == nil {
    58  			opts.Headers = NewHTTPHeaders()
    59  		}
    60  
    61  		headers.ForEach(func(value []string, key string) bool {
    62  			opts.Headers.SetArr(key, value)
    63  
    64  			return true
    65  		})
    66  	}
    67  }
    68  
    69  func WithCookie(cookie HTTPCookie) Option {
    70  	return func(opts *Options) {
    71  		if opts.Cookies == nil {
    72  			opts.Cookies = NewHTTPCookies()
    73  		}
    74  
    75  		opts.Cookies.Set(cookie)
    76  	}
    77  }
    78  
    79  func WithCookies(cookies []HTTPCookie) Option {
    80  	return func(opts *Options) {
    81  		if opts.Cookies == nil {
    82  			opts.Cookies = NewHTTPCookies()
    83  		}
    84  
    85  		for _, c := range cookies {
    86  			opts.Cookies.Set(c)
    87  		}
    88  	}
    89  }