options

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProgressReader

func ProgressReader(reader io.Reader, total int64, onProgress func(read, total int64)) *progressReader

func ProgressWriter

func ProgressWriter(writer io.WriteCloser, total int64, onProgress func(written, total int64)) *progressWriter

ProgressWriter creates a new ProgressWriter

Types

type CompressionType

type CompressionType string
const (
	CompressionNone    CompressionType = ""
	CompressionGzip    CompressionType = "gzip"
	CompressionDeflate CompressionType = "deflate"
	CompressionBrotli  CompressionType = "br"
	CompressionCustom  CompressionType = "custom"
)

type Option

type Option struct {
	Verbose                  bool                                           // Whether logging should be verbose or not
	Logger                   slog.Logger                                    // Logging - default uses the slog TextHandler
	Header                   http.Header                                    // Headers to be included in the request
	Cookies                  []*http.Cookie                                 // Cookies to be included in the request
	ProtocolScheme           string                                         // define a custom protocol scheme. It defaults to https
	Compression              CompressionType                                // CompressionType to use: none, gzip, deflate or brotli
	CustomCompressionType    CompressionType                                //
	CustomCompressor         func(w *io.PipeWriter) (io.WriteCloser, error) // Function for custom compression
	UserAgent                string                                         // User Agent to send with requests
	FollowRedirect           bool                                           // Disable or enable redirects. Default is true i.e.: follow redirects
	PreserveMethodOnRedirect bool                                           // Default is true
	UniqueIdentifierType     UniqueIdentifierType                           // Internal trace or identifier for the request
	Transport                *http.Transport                                // Create our own default transport
	ResponseWriter           ResponseWriter                                 // Define the type of response writer
	UploadBufferSize         *int                                           // Control the size of the buffer when uploading a file
	DownloadBufferSize       *int                                           // Control the size of the buffer when downloading a file
	OnUploadProgress         func(bytesRead, totalBytes int64)              // To monitor and track progress when uploading
	OnDownloadProgress       func(bytesRead, totalBytes int64)              // To monitor and track progress when downloading
}

RequestOptions represents additional options for the HTTP request. DisableRedirect - Determines if redirects should be followed or not. The default option is false which means redirects will be followed.

func New

func New(opts ...*Option) *Option

func (*Option) AddCookie

func (opt *Option) AddCookie(cookie *http.Cookie)

AddCookie adds a new cookie to the RequestOptions.

func (*Option) AddHeader

func (opt *Option) AddHeader(key string, value string)

Kept for backwards compatability

func (*Option) ClearCookies

func (opt *Option) ClearCookies()

ClearCookies clears all cookies in the RequestOptions.

func (*Option) ClearHeaders

func (opt *Option) ClearHeaders()

ClearHeaders clears all headers in the RequestOptions.

func (*Option) DisableLogging

func (opt *Option) DisableLogging()

func (*Option) DisablePreserveMethodOnRedirect

func (opt *Option) DisablePreserveMethodOnRedirect()

func (*Option) DisableRedirects

func (opt *Option) DisableRedirects()

func (*Option) EnableLogging

func (opt *Option) EnableLogging()

func (*Option) EnablePreserveMethodOnRedirect

func (opt *Option) EnablePreserveMethodOnRedirect()

func (*Option) EnableRedirects

func (opt *Option) EnableRedirects()

func (*Option) GenerateIdentifier

func (opt *Option) GenerateIdentifier() string

func (*Option) GetCompressor

func (opt *Option) GetCompressor(w *io.PipeWriter) (io.WriteCloser, error)

func (*Option) GetWriter

func (opt *Option) GetWriter() io.WriteCloser

GetWriter returns the underlying io.WriteCloser

func (*Option) InitialiseWriter

func (opt *Option) InitialiseWriter() (io.WriteCloser, error)

InitialiseWriter sets up the appropriate writer based on the ResponseWriter configuration

func (*Option) ListCookies

func (opt *Option) ListCookies()

ListCookies prints out the list of cookies in the RequestOptions.

func (*Option) ListHeaders

func (opt *Option) ListHeaders()

ListHeaders prints out the list of headers in the RequestOptions.

func (*Option) LogVerbose

func (opt *Option) LogVerbose(msg string, args ...any)

func (*Option) Merge

func (opt *Option) Merge(src *Option)

func (*Option) SetBufferOutput

func (opt *Option) SetBufferOutput()

SetBufferOutput configures the response writer to write to an in-memory buffer

func (*Option) SetCompression

func (opt *Option) SetCompression(compressionType CompressionType)

func (*Option) SetDownloadBufferSize

func (opt *Option) SetDownloadBufferSize(sizeInBytes int)

SetDownloadBufferSize allows custom buffer size if needed

func (*Option) SetFileOutput

func (opt *Option) SetFileOutput(filepath string)

SetFileOutput configures the response writer to write to a file

func (*Option) SetLogger

func (opt *Option) SetLogger(logger *slog.Logger)

func (*Option) SetOutput

func (opt *Option) SetOutput(writerType ResponseWriterType, filepath ...string) error

SetOutput configures how the response should be written For WriteToBuffer: opt.SetOutput(WriteToBuffer) For WriteToFile: opt.SetOutput(WriteToFile, "path/to/file.txt")

func (*Option) SetProtocolScheme

func (opt *Option) SetProtocolScheme(scheme string)

func (*Option) SetTransport

func (opt *Option) SetTransport(transport *http.Transport)

func (*Option) UseJsonLogger

func (opt *Option) UseJsonLogger()

func (*Option) UseTextLogger

func (opt *Option) UseTextLogger()

type ResponseWriter

type ResponseWriter struct {
	// Type indicates whether to write to buffer or file
	Type ResponseWriterType
	// FilePath is only used when Type is WriteToFile
	FilePath string
	// contains filtered or unexported fields
}

ResponseWriter contains configuration for how to handle the HTTP response body

type ResponseWriterType

type ResponseWriterType string
const (
	WriteToBuffer ResponseWriterType = "buffer"
	WriteToFile   ResponseWriterType = "file"
)

type UniqueIdentifierType

type UniqueIdentifierType string
const (
	IdentifierNone UniqueIdentifierType = ""
	IdentifierUUID UniqueIdentifierType = "uuid"
	IdentifierULID UniqueIdentifierType = "ulid"
)

type UploadType

type UploadType string

type WriteCloserBuffer

type WriteCloserBuffer struct {
	*bytes.Buffer
}

func (*WriteCloserBuffer) Close

func (wcb *WriteCloserBuffer) Close() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL