mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
9b762d32ee
* Added default headers and cookies * wip * Added tests * Added default headers and cookies to HTTP driver * Removed unused struct prop
128 lines
2.2 KiB
Go
128 lines
2.2 KiB
Go
package http
|
|
|
|
import (
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
|
"github.com/sethgrid/pester"
|
|
)
|
|
|
|
type (
|
|
Option func(opts *Options)
|
|
|
|
Options struct {
|
|
Name string
|
|
Backoff pester.BackoffStrategy
|
|
MaxRetries int
|
|
Concurrency int
|
|
Proxy string
|
|
UserAgent string
|
|
Headers drivers.HTTPHeaders
|
|
Cookies drivers.HTTPCookies
|
|
}
|
|
)
|
|
|
|
func newOptions(setters []Option) *Options {
|
|
opts := new(Options)
|
|
opts.Name = DriverName
|
|
opts.Backoff = pester.ExponentialBackoff
|
|
opts.Concurrency = 3
|
|
opts.MaxRetries = 5
|
|
|
|
for _, setter := range setters {
|
|
setter(opts)
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
func WithDefaultBackoff() Option {
|
|
return func(opts *Options) {
|
|
opts.Backoff = pester.DefaultBackoff
|
|
}
|
|
}
|
|
|
|
func WithLinearBackoff() Option {
|
|
return func(opts *Options) {
|
|
opts.Backoff = pester.LinearBackoff
|
|
}
|
|
}
|
|
|
|
func WithExponentialBackoff() Option {
|
|
return func(opts *Options) {
|
|
opts.Backoff = pester.ExponentialBackoff
|
|
}
|
|
}
|
|
|
|
func WithMaxRetries(value int) Option {
|
|
return func(opts *Options) {
|
|
opts.MaxRetries = value
|
|
}
|
|
}
|
|
|
|
func WithConcurrency(value int) Option {
|
|
return func(opts *Options) {
|
|
opts.Concurrency = value
|
|
}
|
|
}
|
|
|
|
func WithProxy(address string) Option {
|
|
return func(opts *Options) {
|
|
opts.Proxy = address
|
|
}
|
|
}
|
|
|
|
func WithUserAgent(value string) Option {
|
|
return func(opts *Options) {
|
|
opts.UserAgent = value
|
|
}
|
|
}
|
|
|
|
func WithCustomName(name string) Option {
|
|
return func(opts *Options) {
|
|
opts.Name = name
|
|
}
|
|
}
|
|
|
|
func WithHeader(name string, value []string) Option {
|
|
return func(opts *Options) {
|
|
if opts.Headers == nil {
|
|
opts.Headers = make(drivers.HTTPHeaders)
|
|
}
|
|
|
|
opts.Headers[name] = value
|
|
}
|
|
}
|
|
|
|
func WithHeaders(headers drivers.HTTPHeaders) Option {
|
|
return func(opts *Options) {
|
|
if opts.Headers == nil {
|
|
opts.Headers = make(drivers.HTTPHeaders)
|
|
}
|
|
|
|
for k, v := range headers {
|
|
opts.Headers[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithCookie(cookie drivers.HTTPCookie) Option {
|
|
return func(opts *Options) {
|
|
if opts.Cookies == nil {
|
|
opts.Cookies = make(drivers.HTTPCookies)
|
|
}
|
|
|
|
opts.Cookies[cookie.Name] = cookie
|
|
}
|
|
}
|
|
|
|
func WithCookies(cookies []drivers.HTTPCookie) Option {
|
|
return func(opts *Options) {
|
|
if opts.Cookies == nil {
|
|
opts.Cookies = make(drivers.HTTPCookies)
|
|
}
|
|
|
|
for _, c := range cookies {
|
|
opts.Cookies[c.Name] = c
|
|
}
|
|
}
|
|
}
|