github.com/MontFerret/ferret@v0.18.0/pkg/drivers/http/options.go (about) 1 package http 2 3 import ( 4 stdhttp "net/http" 5 "time" 6 7 "github.com/gobwas/glob" 8 "github.com/sethgrid/pester" 9 10 "github.com/MontFerret/ferret/pkg/drivers" 11 ) 12 13 var ( 14 DefaultConcurrency = 1 15 DefaultMaxRetries = 5 16 DefaultTimeout = time.Second * 30 17 ) 18 19 type ( 20 Option func(opts *Options) 21 22 compiledStatusCodeFilter struct { 23 URL glob.Glob 24 Code int 25 } 26 27 Options struct { 28 *drivers.Options 29 Backoff pester.BackoffStrategy 30 MaxRetries int 31 Concurrency int 32 HTTPCodesFilter []compiledStatusCodeFilter 33 HTTPTransport *stdhttp.Transport 34 Timeout time.Duration 35 } 36 ) 37 38 func NewOptions(setters []Option) *Options { 39 opts := new(Options) 40 opts.Options = new(drivers.Options) 41 opts.Name = DriverName 42 opts.Backoff = pester.ExponentialBackoff 43 opts.Concurrency = DefaultConcurrency 44 opts.MaxRetries = DefaultMaxRetries 45 opts.Timeout = DefaultTimeout 46 opts.HTTPCodesFilter = make([]compiledStatusCodeFilter, 0, 5) 47 48 for _, setter := range setters { 49 setter(opts) 50 } 51 52 return opts 53 } 54 55 func WithDefaultBackoff() Option { 56 return func(opts *Options) { 57 opts.Backoff = pester.DefaultBackoff 58 } 59 } 60 61 func WithLinearBackoff() Option { 62 return func(opts *Options) { 63 opts.Backoff = pester.LinearBackoff 64 } 65 } 66 67 func WithExponentialBackoff() Option { 68 return func(opts *Options) { 69 opts.Backoff = pester.ExponentialBackoff 70 } 71 } 72 73 func WithMaxRetries(value int) Option { 74 return func(opts *Options) { 75 opts.MaxRetries = value 76 } 77 } 78 79 func WithConcurrency(value int) Option { 80 return func(opts *Options) { 81 opts.Concurrency = value 82 } 83 } 84 85 func WithProxy(address string) Option { 86 return func(opts *Options) { 87 drivers.WithProxy(address)(opts.Options) 88 } 89 } 90 91 func WithUserAgent(value string) Option { 92 return func(opts *Options) { 93 drivers.WithUserAgent(value)(opts.Options) 94 } 95 } 96 97 func WithCustomName(name string) Option { 98 return func(opts *Options) { 99 drivers.WithCustomName(name)(opts.Options) 100 } 101 } 102 103 func WithHeader(name string, value []string) Option { 104 return func(opts *Options) { 105 drivers.WithHeader(name, value)(opts.Options) 106 } 107 } 108 109 func WithHeaders(headers *drivers.HTTPHeaders) Option { 110 return func(opts *Options) { 111 drivers.WithHeaders(headers)(opts.Options) 112 } 113 } 114 115 func WithCookie(cookie drivers.HTTPCookie) Option { 116 return func(opts *Options) { 117 drivers.WithCookie(cookie)(opts.Options) 118 } 119 } 120 121 func WithCookies(cookies []drivers.HTTPCookie) Option { 122 return func(opts *Options) { 123 drivers.WithCookies(cookies)(opts.Options) 124 } 125 } 126 127 func WithAllowedHTTPCode(httpCode int) Option { 128 return func(opts *Options) { 129 opts.HTTPCodesFilter = append(opts.HTTPCodesFilter, compiledStatusCodeFilter{ 130 Code: httpCode, 131 }) 132 } 133 } 134 135 func WithAllowedHTTPCodes(httpCodes []int) Option { 136 return func(opts *Options) { 137 for _, code := range httpCodes { 138 opts.HTTPCodesFilter = append(opts.HTTPCodesFilter, compiledStatusCodeFilter{ 139 Code: code, 140 }) 141 } 142 } 143 } 144 145 func WithCustomTransport(transport *stdhttp.Transport) Option { 146 return func(opts *Options) { 147 opts.HTTPTransport = transport 148 } 149 } 150 151 func WithTimeout(duration time.Duration) Option { 152 return func(opts *Options) { 153 opts.Timeout = duration 154 } 155 }